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

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>

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;
    }

}

CommonPicklistApp


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

0 Comments

Leave a Reply

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2020 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?