+ Start a Discussion
@ M  Coder@ M Coder 

Need display 3 Picklist Values dynamically Or Generic code using maps and displaying in lightning component Via Apex class.


Hi Friends i need to call these 3 fields on Account object .  i heard that there is an approach where we give the object and field name and it will give desired picklist value result using map . 

my picklist fields are 
1)Account.Rating__c and values = {'High','low','Medium' }
2)Account.Locality__c and values = {'Local','global','international' }
3)Account.generic__c and values = {'A','B','C','d','e' }
Best Answer chosen by @ M Coder
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Apex:
public class PicklistC {
    
    @AuraEnabled
    public static List<String> getPicklistvalues(String objectName, String field_apiname,Boolean nullRequired){
        List<String> optionlist = new List<String>();
        
        Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
        Map<String, Schema.SObjectField> field_map = gd.get(objectName.toLowerCase()).getDescribe().fields.getMap(); 
        
        List<Schema.PicklistEntry> picklistValues = field_map.get(field_apiname).getDescribe().getPickListValues();
        
        if(nullRequired == true){
            optionlist.add('--None--');
        }
        
        for (Schema.PicklistEntry pv : picklistValues) {
            optionlist.add(pv.getValue());
        }
        return optionlist;
    }
    
}

Component:
<aura:component controller="PicklistC"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="ObjectName" type="String" default="Account" access="global"/> <!-- Object Name as String-->
    <aura:attribute name="Rating" type="String" default="Rating__c" access="global"/>  <!-- Field Name as String-->
    <aura:attribute name="Locality" type="String" default="Locality__c" access="global"/> <!-- Field Name as String-->
    <aura:attribute name="Generic" type="String" default="Generic__c" access="global"/> <!-- Field Name as String-->
    <aura:attribute name="RatingPicklist" type="String[]" />  <!-- Picklist Values of Rating__c Field -->
    <aura:attribute name="LocalityPicklist" type="String[]" />  <!-- Picklist Values of Locality__c Field -->
    <aura:attribute name="GenericPicklist" type="String[]" />  <!-- Picklist Values of Generic__c Field -->
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <lightning:select label="Rating">
        <aura:iteration items="{!v.RatingPicklist}" var="val">
            <option value="{!val}"> {!val}</option>
        </aura:iteration>
    </lightning:select>
    
    <lightning:select label="Locality">
        <aura:iteration items="{!v.LocalityPicklist}" var="val">
            <option value="{!val}"> {!val}</option>
        </aura:iteration>
    </lightning:select>
    
    <lightning:select label="Generic">
        <aura:iteration items="{!v.GenericPicklist}" var="val">
            <option value="{!val}"> {!val}</option>
        </aura:iteration>
    </lightning:select>
</aura:component>

Controller:
({
    doInit: function(component, event, helper) {
        helper.fetchRatingPicklist(component); // fetches PickList Values of Rating Field
        helper.fetchLocalityPicklist(component); // fetches PickList Values of Locality Field
        helper.fetchGenericPicklist(component); // fetches PickList Values of Generic Field
    },
})

Helper:
({
    fetchRatingPicklist : function(component){
        var action = component.get("c.getPicklistvalues");
        action.setParams({
            'objectName': component.get("v.ObjectName"),
            'field_apiname': component.get("v.Rating"),
            'nullRequired': true // includes --None--
        });
        action.setCallback(this, function(a) {
            var state = a.getState();
            if (state === "SUCCESS"){
                component.set("v.RatingPicklist", a.getReturnValue());
            } 
        });
        $A.enqueueAction(action);
    },
    
    fetchLocalityPicklist : function(component){
        var action = component.get("c.getPicklistvalues");
        action.setParams({
            'objectName': component.get("v.ObjectName"),
            'field_apiname': component.get("v.Locality"),
            'nullRequired': false
        });
        action.setCallback(this, function(a) {
            var state = a.getState();
            if (state === "SUCCESS"){
                component.set("v.LocalityPicklist", a.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
    
    fetchGenericPicklist : function(component){
        var action = component.get("c.getPicklistvalues");
        action.setParams({
            'objectName': component.get("v.ObjectName"),
            'field_apiname': component.get("v.Generic"),
            'nullRequired': true
        });
        action.setCallback(this, function(a) {
            var state = a.getState();
            if (state === "SUCCESS"){
                component.set("v.GenericPicklist", a.getReturnValue());
            } 
        });
        $A.enqueueAction(action);
    },
})

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas