Get Picklist Values in Salesforce Lightning Component from a common Apex function

Get Picklist Values in Salesforce Lightning Component From a Common Apex Function

See below the code example to get Picklist Values in Salesforce Lightning Component from a Common Apex Function:

CommonPicklist.cmp

<aura:component controller="CommonPicklistController">
    <aura:attribute name="ObjectName" type="String" default="Opportunity" access="global"/> <!-- Object Name as String-->
    <aura:attribute name="Type" type="String" default="Type" access="global"/>  <!-- Field Name as String-->
    <aura:attribute name="Stage" type="String" default="StageName" access="global"/> <!-- Field Name as String-->
    <aura:attribute name="Lead_Source" type="String" default="LeadSource" access="global"/> <!-- Field Name as String-->
    <aura:attribute name="TypePicklist" type="String[]" />  <!-- Picklist Values of Type Field -->
    <aura:attribute name="StagePicklist" type="String[]" />  <!-- Picklist Values of StageName Field -->
    <aura:attribute name="LeadSourcePicklist" type="String[]" />  <!-- Picklist Values of LeadSource Field -->    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />    
    <lightning:select label="Type">
        <aura:iteration items="{!v.TypePicklist}" var="val">
            <option value="{!val}"> {!val}</option>
        </aura:iteration>
    </lightning:select>    
     <lightning:select label="Stage">
        <aura:iteration items="{!v.StagePicklist}" var="val">
            <option value="{!val}"> {!val}</option>
        </aura:iteration>
    </lightning:select>    
     <lightning:select label="Lead Source">
        <aura:iteration items="{!v.LeadSourcePicklist}" var="val">
            <option value="{!val}"> {!val}</option>
        </aura:iteration>
    </lightning:select>    
</aura:component>

dont miss out iconDon't forget to check out: Top Salesforce Lightning Components to Improve Functionality

CommonPicklistController.js

({
  doInit: function(component, event, helper) {
        helper.fetchTypePicklist(component); // fetches PickList Values of Type Field
        helper.fetchStagePicklist(component); // fetches PickList Values of Stage Field
        helper.fetchLeadSourcePicklist(component); // fetches PickList Values of LeadSource Field
    },
})

CommonPicklistHelper.js

({
    fetchTypePicklist : function(component){
        var action = component.get("c.getPicklistvalues");
        action.setParams({
            'objectName': component.get("v.ObjectName"),
            'field_apiname': component.get("v.Type"),
            'nullRequired': true // includes --None--
        });
        action.setCallback(this, function(a) {
            var state = a.getState();
            if (state === "SUCCESS"){
                component.set("v.TypePicklist", a.getReturnValue());
            } 
        });
        $A.enqueueAction(action);
    },    
    fetchStagePicklist : function(component){
        var action = component.get("c.getPicklistvalues");
        action.setParams({
            'objectName': component.get("v.ObjectName"),
            'field_apiname': component.get("v.Stage"),
            'nullRequired': false
        });
        action.setCallback(this, function(a) {
            var state = a.getState();
            if (state === "SUCCESS"){
                component.set("v.StagePicklist", a.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },    
    fetchLeadSourcePicklist : function(component){
        var action = component.get("c.getPicklistvalues");
        action.setParams({
            'objectName': component.get("v.ObjectName"),
            'field_apiname': component.get("v.Lead_Source"),
            'nullRequired': true
        });
        action.setCallback(this, function(a) {
            var state = a.getState();
            if (state === "SUCCESS"){
                component.set("v.LeadSourcePicklist", a.getReturnValue());
            } 
        });
        $A.enqueueAction(action);
    },
})

CommonPicklistController.apxc

public with sharing class CommonPicklistController {    
    @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;
    }
}

dont miss out iconLearn more about: Salesforce Visualforce Vs Salesforce Lightning Component

CommonPicklistApp

<aura:application extends="force:slds">
    <c:CommonPicklist/>
</aura:application>

Related Articles

Responses