salesforce lightning zero-based learning to get the field label information (xvi) of common components

We do a lot of projects are projects in multiple languages ​​for different countries need to show title different languages. We VF page can be described as the classic in handy, because the system has a good package we can get label / api name and other methods directly in VF. But we have found lightning aura in the development of this common feature and does not contain, well, since there is no readily available then we have to have a workaround way to get backstage. Cipian main components of a good package to achieve access to label certain object or some object related fields.

Then we started to develop this component, previously developed we need to think about the components of things, what is mass participation should return to what it should be, what functions should realize what pain point solutions. How to use better optimization. I think probably not particularly full, interest can be optimized.

1. object's API name should be required. It should be realized to obtain label information fields of multiple tables at the same time, we draw component, you may need to obtain the current object, label field and related parent object child object, so where mass participation should be done transfer list rather than a single object

api name list field of 2. object corresponding to the specified, this should be optional, non-mandatory. We all know that aura is now very slow to develop, and when we get label foreground, an object may have hundreds of fields, but we need only a few fields of information label in the page, if all check out a particular impact on the foreground view state, so we should support here can be queried by some of the fields specified. Because the object parameter passing is a list, so this parameter should Map <String, List <String >> way.

3. The return type must be Map <String, Map <String, String >> type, outer key is objectAPIName, map the inner key is fieldAPIName, map of the inner layer of value for our field label need

OK, the above has been sorted out, dry it would be finished.

I. common components to build

FieldLabelServiceController.cls for background build queries specified obj / field of value -> label information

 1 public with sharing class FieldLabelServiceController {
 2     /*
 3     * @param objApiNameList : object API name list. eg:['Account','Contact']
 4     * @param objApiName2FieldsMap: object API name 2 fields map. eg:{'Account':['Name','Type'],'Contact':['LastName','Phone']}
 5     * @return object API name 2 map of field API name -> label name. eg:{'Account':{'Type':'类型'},'Contact':{'LastName':'姓'}}
 6     */
 7     @AuraEnabled
 8     public static Map<String,Map<String,String>> getFieldLabelService(List<String> objApiNameList,Map<String,List<String>> objApiName2FieldsMap) {
 9         // key: object API name ; value : (Map: key:field API name, value: field label)
10         Map<String,Map<String,String>> object2FieldLabelMap = new Map<String,Map<String,String>>();
11         //get all sobject sObjectType map
12         Map<String,sObjectType> objName2ObjTypeMap = Schema.getGlobalDescribe();
13         for(String objApiName : objApiNameList) {
14 
15             //1. get specific object sObjectType
16             sObjectType objType = objName2ObjTypeMap.get(objApiName);
17             //2. get all of the fields map via specific object
18             Map<String,Schema.SObjectField> fieldsMap = objType.getDescribe().fields.getMap();
19 
20             //3. check if retrieve specific field list or all the fields mapping via object
21             Set<String> retrieveFieldList = new Set<String>();
22             if(objApiName2FieldsMap != null && objApiName2FieldsMap.containsKey(objApiName)) {
23                 retrieveFieldList = new Set<String>(objApiName2FieldsMap.get(objApiName));
24             }
25 
26             Map<String,String> fieldApiName2FieldLabelMap = new Map<String,String>();
27             //4. get all / specific field api name -> label name mapping
28             for(String fieldApiName : fieldsMap.keySet()){
29                 if(retrieveFieldList.size() > 0 && !retrieveFieldList.contains(fieldApiName)) {
30                     continue;
31                 }
32 
33                 String label = fieldsMap.get(fieldApiName).getDescribe().getLabel();
34                 fieldApiName2FieldLabelMap.put(String.valueOf(fieldsMap.get(fieldApiName)), label == null ? fieldApiName : label);
35             }
36 
37             object2FieldLabelMap.put(objApiName, fieldApiName2FieldLabelMap);
38         }
39         return object2FieldLabelMap;
40     }
41 }

FieldLabelService.cmp: sharing method for packaging

1 <aura:component access="global" description="Field label service" controller="FieldLabelServiceController">
2     <aura:method access="global" name="getFieldLabel" action="{!c.getFieldLabelAction}">
3         <aura:attribute type="List" name="objectAPINameList" required="true" description="object list to retrieve field label" />
4         <aura:attribute type="Map" name="objectFieldAPINameMap" description="specific fields need to retrieve via object api name"/>
5         <aura:attribute type="Function" name="callback" required="true" />
6     </aura:method>
7 </aura:component> 

FieldLabelServiceController.js: package corresponding controller js method for calling back acquisition result

 1 ({
 2     getFieldLabelAction : function(component, event, helper) {
 3         const params = event.getParam('arguments');
 4         const action = component.get('c.getFieldLabelService');
 5         action.setParams({
 6             "objApiNameList" : params.objectAPINameList,
 7             "objApiName2FieldsMap":params.objectFieldAPINameMap
 8         });
 9         action.setCallback(this, function(response) {
10             const state = response.getState();
11             if (state === 'SUCCESS') {
12                 params.callback(response.getReturnValue());
13             } else if (state === 'ERROR') {
14                 const errors = response.getError();
15                 if (errors) {
16                     console.error(JSON.stringify(errors));
17                 } else {
18                     console.error('Unknown error');
19                 }
20             }
21         });
22 
23         $A.enqueueAction(action);
24     }
25 })

Thus the package assembly is completed, the following is a calling part. Call part no UI, UI draw their own interest.

II. Public component testing

FieldLabelTestComponent: for introducing common components, and acquires the initialization Account / Contact of the field label.

<aura:component implements="flexipage:availableForAllPageTypes">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="accountFieldLabelMap" type="Map"/>
    <aura:attribute name="contactFieldLabelMap" type="Map"/>
    <c:FieldLabelService aura:id="service"/>
</aura:component>

FieldLabelTestComponentController.js: background method calls for common components, by value, is parsed to obtain the content they need for response. demo acquire name and value of the type of account only for obtaining label values ​​of all fields of contact.

 1 ({
 2     doInit : function(component, event, helper) {
 3         const service = component.find('service');
 4         let objectAPINameList = ['Account','Contact'];
 5         let objectFieldAPINameMap = {'Account':['name','type']};
 6         service.getFieldLabel(objectAPINameList,objectFieldAPINameMap,function(result) {
 7             console.log(JSON.stringify(result));
 8             component.set('v.accountFieldLabelMap',result['Account']);
 9             component.set('v.contactFieldLabelMap',result['Contact']);
10             console.log(JSON.stringify(result['Account']));
11             console.log(JSON.stringify(result.Account));
12         });
13     }
14 })

The results show: for the account gained only label specified field, Contact acquired all of the label information. You can use [] or manner. Manner for more content.

 Summary: The article is an explanation of the realization of common components to obtain field label for the circumstances under aura, because the background by the schema acquisition fieldmap get field api name to lowercase, so if we want to pass the second parameter is the need to ensure that the name of the field lowercase, so get different results and imagination. Articles in the wrong place welcome that, do not understand the message of welcome, there can optimize the place to welcome and encourage the exchange of optimization.

 

Guess you like

Origin www.cnblogs.com/zero-zyq/p/11992457.html