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;

    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值