Retrieve Dynamic picklist values in Lightning Component

By | November 11, 2019

In this scenario we have shown that, retrieving picklist values in Lightning Component

Apex Controller

public class GetPiklistValues {
    @AuraEnabled        
    public static List<String> getPickListValuesIntoList(String objectType, String selectedField){
        List<String> pickListValuesList = new List<String>();
        Schema.SObjectType convertToObj = Schema.getGlobalDescribe().get(objectType);
        Schema.DescribeSObjectResult res = convertToObj.getDescribe();
        Schema.DescribeFieldResult fieldResult = res.fields.getMap().get(selectedField).getDescribe();
        List<Schema.PicklistEntry> pickListEnterValuesList = fieldResult.getPicklistValues();
        pickListValuesList.add('--None--');
        for( Schema.PicklistEntry pickListVal : pickListEnterValuesList){
            pickListValuesList.add(pickListVal.getLabel());
        }     
        return pickListValuesList;       
    }
}

Lightning Component : GetPiklistValues

<aura:component controller="GetPiklistValues" access="global">
    <aura:attribute name="sObjectName" type="String" />
    <aura:attribute name="fieldName" type="String" />
    <aura:attribute name="GetpicklistVal" type="Object"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:component>

controller Js

({
    doInit : function(component) {
        var action = component.get("c.getPickListValuesIntoList");
        action.setParams({
            objectType: component.get("v.sObjectName"),
            selectedField: component.get("v.fieldName")
        });
        action.setCallback(this, function(response) {
            var list = response.getReturnValue();
            component.set("v.GetpicklistVal", list);
        })
        $A.enqueueAction(action);
    }
})

Application

<aura:application extends="force:slds">
    <aura:attribute name='lea' type='Lead' default="{sobjecttype:'Lead'}"/>
    
    <aura:attribute name="type" type="String" />    
    <c:GetPiklistValues sObjectName="Lead" fieldName="LeadSource" GetpicklistVal="{!v.type}" />  
    
    <lightning:select aura:id="type" label="LeadSource" value="{!v.lea.LeadSource}" >        
        <aura:iteration items="{!v.type}" var="item">            
            <option value="{!item}" >{!item}</option>
        </aura:iteration>
    </lightning:select>
    
</aura:application>

If you want to get picklist values in component then, write below application Logic in required Lightning Component…Here we have written logic directly in Lightning Application, but if we want to write the logic in Lightning Component then we need to write below logic in Lightning component.

<aura:component>
<aura:attribute name="type" type="String" />    
<c:GetPiklistValues sObjectName="Lead" fieldName="LeadSource" GetpicklistVal="{!v.type}" />  

<lightning:select aura:id="type" label="LeadSource" value="{!v.lea.LeadSource}" >        
    <aura:iteration items="{!v.type}" var="item">            
        <option value="{!item}" >{!item}</option>
    </aura:iteration>
</lightning:select>
</aura:component>

Output

Leave a Reply

Your email address will not be published. Required fields are marked *