Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

salesforce - Why Picklist Not Populating Data for Custom Dependent Picklist Field With lightning:select In Lightning Component

I am trying to replicate the below functionality of Custom Dependent Picklist Field With lightning:select In Lightning Component for Custom Object (Ref:- https://sfdcmonkey.com/2018/08/31/dependent-picklist-lightningselect-lightning-salesforce/) ,

Only difference here is i am using Custom object ERT_Case_Type_Data__c and its 2 picklist Level_1__c and Level_2__c instead of standard object Contact reference in the article

Below are ERT_Case_Type_Data__c details enter image description here

Below are ERT_Case_Type_Data__c data details

enter image description here

Here is TestApp which uses this component, Unfortunately, I don't see data in the dropdown Picklist

enter image description here

Please help me in knowing what is wrong I am doing here

Here is Apex class dependentPicklist_UpdateCtrl.apxc

            public class dependentPicklist_UpdateCtrl {
                @AuraEnabled 
                public static Map<String, List<String>> getDependentMap(sObject objDetail, string contrfieldApiName,string depfieldApiName) {
                    String controllingField = contrfieldApiName.toLowerCase();
                    String dependentField = depfieldApiName.toLowerCase();
                    
                    Map<String,List<String>> objResults = new Map<String,List<String>>();
                    
                    Schema.sObjectType objType = objDetail.getSObjectType();
                    if (objType==null){
                        return objResults;
                    }
                    
                    Map<String, Schema.SObjectField> objFieldMap = objType.getDescribe().fields.getMap();
                    
                    if (!objFieldMap.containsKey(controllingField) || !objFieldMap.containsKey(dependentField)){
                        return objResults;     
                    }
                    
                    Schema.SObjectField theField = objFieldMap.get(dependentField);
                    Schema.SObjectField ctrlField = objFieldMap.get(controllingField);
                    
                    List<Schema.PicklistEntry> contrEntries = ctrlField.getDescribe().getPicklistValues();
                    List<PicklistEntryWrapper> depEntries = wrapPicklistEntries(theField.getDescribe().getPicklistValues());
                    List<String> controllingValues = new List<String>();
                    
                    for (Schema.PicklistEntry ple : contrEntries) {
                        String label = ple.getLabel();
                        objResults.put(label, new List<String>());
                        controllingValues.add(label);
                    }
                    
                    for (PicklistEntryWrapper plew : depEntries) {
                        String label = plew.label;
                        String validForBits = base64ToBits(plew.validFor);
                        for (Integer i = 0; i < validForBits.length(); i++) {
                            String bit = validForBits.mid(i, 1);
                            if (bit == '1') {
                                objResults.get(controllingValues.get(i)).add(label);
                            }
                        }
                    }
                    return objResults;
                }
                
                public static String decimalToBinary(Integer val) {
                    String bits = '';
                    while (val > 0) {
                        Integer remainder = Math.mod(val, 2);
                        val = Integer.valueOf(Math.floor(val / 2));
                        bits = String.valueOf(remainder) + bits;
                    }
                    return bits;
                }
                
                public static String base64ToBits(String validFor) {
                    if (String.isEmpty(validFor)) return '';
                    
                    String validForBits = '';
                    
                    for (Integer i = 0; i < validFor.length(); i++) {
                        String thisChar = validFor.mid(i, 1);
                        Integer val = base64Chars.indexOf(thisChar);
                        String bits = decimalToBinary(val).leftPad(6, '0');
                        validForBits += bits;
                    }
                    
                    return validForBits;
                }
                
                private static final String base64Chars = '' +
                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
                    'abcdefghijklmnopqrstuvwxyz' +
                    '0123456789+/';
                
                
                private static List<PicklistEntryWrapper> wrapPicklistEntries(List<Schema.PicklistEntry> PLEs) {
                    return (List<PicklistEntryWrapper>)
                        JSON.deserialize(JSON.serialize(PLEs), List<PicklistEntryWrapper>.class);
                }
                
                public class PicklistEntryWrapper{
                    public String active {get;set;}
                    public String defaultValue {get;set;}
                    public String label {get;set;}
                    public String value {get;set;}
                    public String validFor {get;set;}
                    public PicklistEntryWrapper(){            
                    }
                    
                }
            }
            

Here is Component code dependentPicklist_UpdateCtrl.cmp

            <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"
                            access="global"
                            controller="dependentPicklist_UpdateCtrl">
                <!-- call doInit function on component load -->  
                <aura:handler name="init" value="this" action="{!c.doInit}"/>
                
                <!-- aura attributes-->  
                <aura:attribute name="listControllingValues" type="list" default="[]" description="to store controller field values"/>
                <aura:attribute name="listDependingValues" type="list" default="['--- None ---']" description="to store dependent field values"/>
                <aura:attribute name="depnedentFieldMap" type="map" description="map to store dependent values with controlling value"/>
                <aura:attribute name="bDisabledDependentFld" type="boolean" default="true"/> 
                
                <aura:attribute name="objDetail" type="ERT_Case_Type_Data__c" default="{'sobjectType' : 'ERT_Case_Type_Data__c'}"/>
                <aura:attribute name="controllingFieldAPI" type="string" default="Level_1__c" description="store field API name of Controller field"/>
                <aura:attribute name="dependingFieldAPI" type="string" default="Level_2__c" description="store field API name of dependent field"/>
                
                <!--Controller Field-->
                <lightning:layoutItem size="12" padding="around-small">    
                    <lightning:select name="controllerFld"
                                      value="{!v.objDetail.Level_1__c}"
                                      label="Level1"
                                      onchange="{!c.onControllerFieldChange}">
                        <aura:iteration items="{!v.listControllingValues}" var="val">
                            <option value="{!val}">{!val}</option>
                        </aura:iteration>
                    </lightning:select>
                </lightning:layoutItem>
                
                <!--Dependent Field-->
                <lightning:layoutItem size="12" padding="around-small">
                    <lightning:select name="dependentFld"
                                      value="{!v.objDetail.Level_2__c}"
                                      label="Level2"
                                      >
                        <aura:iteration items="{!v.listDependingValues}" var="val">
                            <option value="{!val}">{!val}</option>
                        </aura:iteration>
                    </lightning:select>
                </lightning:layoutItem>
                
            </aura:component>

Here is Controller code for dependentPicklist_UpdateCtrlController.js

            ({
                doInit : function(component, event, helper) { 
                    // get the fields API name and pass it to helper function  
                    var controllingFieldAPI = component.get("v.controllingFieldAPI");
                    var dependingFieldAPI = component.get("v.dependingFieldAPI");
                    var objDetails = component.get("v.objDetail");
                    // call the helper function
                    helper.fetchPicklistValues(component,objDetails,controllingFieldAPI, dependingFieldAPI);
                },
                
                onControllerFieldChange: function(component, event, helper) {     
                    var controllerValueKey = event.getSource().get("v.value"); // get selected controller field value
                    var depnedentFieldMap = component.get("v.depnedentFieldMap");
                    
                    if (controllerValueKey != '--- None ---') {
                        var ListOfDependentFields = depnedentFieldMap[controllerValueKey];
                        
                        if(ListOfDependentFields.length > 0){
                            component.set("v.bDisabledDependentFld" , false);  
                            helper.fetchDepValues(component, ListOfDependentFields);    
                        }else{
                            component.set("v.bDisabledDependentFld" , true); 
                            component.set("v.listDependingValues", ['--- None ---']);
                        }  
                        
                    } else {
                        component.set("v.listDependingValues", ['--- None ---']);
                        component.set("v.bDisabledDependentFld" , true);
                    }
                },
            })
            

Here is Helper code for dep


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...