salesforce前端的Map屬性賦值問題

在Lightning前端畫面中,有Map屬性,從Apex取值之後,即使返回的是Map,也不能直接賦值,要循環變換一下賦值。

如有理解錯誤,請賜教哈。

 

代碼如下:

Lightning前端:

<aura:component implements="flexipage:availableForAllPageTypes,force:appHostable" access="global">

    <c:FieldLabelService aura:id="service" />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <aura:attribute name="accountFieldLabelMap" type="Map"  />

    Account:

    <aura:iteration items="{!v.accountFieldLabelMap}" var="rec" indexVar="key">

        <p>{!rec.key}:{!rec.value}</p>

    </aura:iteration>

</aura:component>   

 

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<String,Map<String,String>> getFieldLabelService(List<String> objApiNameList,Map<String,List<String>> objApiName2FieldsMap){

        //key:object API name, value:(Map--key:field API name,value:field label)

        Map<String,Map<String,String>> object2FieldLabelMap = new Map<String,Map<String,String>>();

 

        //get all sObject sObjectType map

        Map<String,sObjectType> objName2ObjTypeMap = Schema.getGlobalDescribe();

        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<String,Schema.SObjectField> fieldsMap = objType.getDescribe().fields.getMap();

            //3.check if retrieve specific field list or all the fields mapping via object

            Set<String> retrieveFieldList = new Set<String>();

            if(objApiName2FieldsMap !=null && objApiName2FieldsMap.containsKey(objApiName)){

                retrieveFieldList = new Set<String>(objApiName2FieldsMap.get(objApiName));

            }

 

            Map<String,String> fieldApiName2FieldLabelMap = new Map<String,String>();

 

            //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;

    }

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章