在Lightning前端画面中,有Map属性,从Apex取值之后,即使返回的是Map,也不能直接赋值,要循环变换一下赋值。
如有理解错误,请赐教哈。
代码如下:
Lightning前端:
Account:
{!rec.key}:{!rec.value}
Lightning前端Controller.js:
({
doInit : function(component, event, helper) {
const service = component.find('service');
let objectAPINameList = ['Account','Contact'];
let objectFieldAPINameMap = {'Account':['Name','Type']};
service.getFieldLabel(objectAPINameList,objectFieldAPINameMap,function(result){
console.log(JSON.stringify(result)); -->这句就是要测试一下能不能返回下面那个Apex返回的Map
//本来是想这一句应该就可以给前端Map变量赋值了,但是死活赋值不上
//component.set('v.contactFieldLabelMap',result['Account']);
//后来在这个上面看到,说不能直接搞,要转换一下
//https://salesforce.stackexchange.com/questions/140608/how-to-iterate-over-map-in-salesforce-lightning-component
var accountFieldLabelMap = [];
var retAccount = result['Account'];
for(var key in retAccount){
accountFieldLabelMap.push({value:retAccount[key], key:key});
}
component.set('v.accountFieldLabelMap',accountFieldLabelMap);
});
}
})
Apex后端:
这个有点儿复杂哈,代码贴出来可能意义也不大。
大概就是返回一个这个的Map:
{
"Account": {
"Name": "Account Name",
"Type": "Account Type"
},
"Contact": {
"Id": "Contact ID",
"IsDeleted": "Deleted",
"MasterRecordId": "Master Record ID",
"AccountId": "Account ID",
"LastName": "Last Name",
"FirstName": "First Name",
"Salutation": "Salutation",
"Name": "Full Name",
"OtherStreet": "Other Street",
"OtherCity": "Other City",
"OtherState": "Other State/Province",
"OtherPostalCode": "Other Zip/Postal Code",
"OtherCountry": "Other Country",
"OtherLatitude": "Other Latitude",
"OtherLongitude": "Other Longitude",
"OtherGeocodeAccuracy": "Other Geocode Accuracy",
"OtherAddress": "Other Address",
"MailingStreet": "Mailing Street",
"MailingCity": "Mailing City",
"MailingState": "Mailing State/Province",
"MailingPostalCode": "Mailing Zip/Postal Code",
"MailingCountry": "Mailing Country",
"MailingLatitude": "Mailing Latitude",
"MailingLongitude": "Mailing Longitude",
"MailingGeocodeAccuracy": "Mailing Geocode Accuracy",
"MailingAddress": "Mailing Address",
"Phone": "Business Phone",
"Fax": "Business Fax",
"MobilePhone": "Mobile Phone",
"HomePhone": "Home Phone",
"OtherPhone": "Other Phone",
"AssistantPhone": "Asst. Phone",
"ReportsToId": "Reports To ID",
"Email": "Email",
"Title": "Title",
"Department": "Department",
"AssistantName": "Assistant's Name",
"LeadSource": "Lead Source",
"Birthdate": "Birthdate",
"Description": "Contact Description",
"OwnerId": "Owner ID",
"HasOptedOutOfEmail": "Email Opt Out",
"HasOptedOutOfFax": "Fax Opt Out",
"DoNotCall": "Do Not Call",
"CreatedDate": "Created Date",
"CreatedById": "Created By ID",
"LastModifiedDate": "Last Modified Date",
"LastModifiedById": "Last Modified By ID",
"SystemModstamp": "System Modstamp",
"LastActivityDate": "Last Activity",
"LastCURequestDate": "Last Stay-in-Touch Request Date",
"LastCUUpdateDate": "Last Stay-in-Touch Save Date",
"LastViewedDate": "Last Viewed Date",
"LastReferencedDate": "Last Referenced Date",
"EmailBouncedReason": "Email Bounced Reason",
"EmailBouncedDate": "Email Bounced Date",
"IsEmailBounced": "Is Email Bounced",
"PhotoUrl": "Photo URL",
"Jigsaw": "Data.com Key",
"JigsawContactId": "Jigsaw Contact ID",
"CleanStatus": "Clean Status",
"Level__c": "Level",
"Languages__c": "Languages",
"Location__Latitude__s": "Location (Latitude)",
"Location__Longitude__s": "Location (Longitude)",
"Location__c": "Location",
"Loan_Amount__c": "Loan Amount",
"Account_Number__c": "Account Number (取引先番号)",
"Days_Remaining__c": "Days Remaining",
"Stage__c": "Stage"
}
}
public with sharing class FieldLabelServiceController {
/*
* @param objApiNameList : object API name list. eg:['Account','Contact']
* @param objApiName2FieldsMap: object API name 2 fields map. eg:{'Account':['Name','Type'],'Contact':['LastName','Phone']}
* @return object API name 2 map of field API name -> label name. eg:{'Account':{'Type':'类型'},'Contact':{'LastName':'姓'}}
*/
@AuraEnabled
public static Map
//key:object API name, value:(Map--key:field API name,value:field label)
Map
//get all sObject sObjectType map
Map
for(String objApiName:objApiNameList){
//1.get specific object sObjctType
sObjectType objType = objName2ObjTypeMap.get(objApiName);
//2.get all of the fields map via specific object
Map
//3.check if retrieve specific field list or all the fields mapping via object
Set
if(objApiName2FieldsMap !=null && objApiName2FieldsMap.containsKey(objApiName)){
retrieveFieldList = new Set
}
Map
//4.get all/specific field api name -> label name mapping
for(String fieldApiName:fieldsMap.keySet()){
if(retrieveFieldList.size()>0 && !retrieveFieldList.contains(String.valueOf(fieldsMap.get(fieldApiName)))){
continue;
}
String label = fieldsMap.get(fieldApiName).getDescribe().getLabel();
fieldApiName2FieldLabelMap.put(String.valueOf(fieldsMap.get(fieldApiName)),label == null ? fieldApiName:label);
object2FieldLabelMap.put(objApiName,fieldApiName2FieldLabelMap);
}
}
return object2FieldLabelMap;
}
}