Activity Forums Salesforce® Discussions How to get picklist values dynamically in lightning component in salesforce?

  • How to get picklist values dynamically in lightning component in salesforce?

    Posted by Laveena on September 30, 2019 at 1:52 pm

    How to get picklist values dynamically in lightning component in salesforce?

    Saddam replied 4 years, 7 months ago 3 Members · 2 Replies
  • 2 Replies
  • Piyush

    Member
    October 1, 2019 at 2:47 am

    Hi Laveena,

    You can take help from below link to get picklist values dynamically in lightning component in salesforce:-

    https://www.biswajeetsamal.com/blog/get-picklist-values-dynamically-in-lightning-component/

  • Saddam

    Member
    October 1, 2019 at 6:10 am

    Hi,

    you can use this code for getting dynamic picklist value

     

    Class

    =====

    public class fetchPicklistOptsController {
    @AuraEnabled
    public static List < String > getselectOptions(sObject objObject, string fld) {
    system.debug('objObject --->' + objObject);
    system.debug('fld --->' + fld);
    List < String > allOpts = new list < String > ();
    // Get the object type of the SObject.
    Schema.sObjectType objType = objObject.getSObjectType();

    // Describe the SObject using its object type.
    Schema.DescribeSObjectResult objDescribe = objType.getDescribe();

    // Get a map of fields for the SObject
    map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();

    // Get the list of picklist values for this field.
    list < Schema.PicklistEntry > values =
    fieldMap.get(fld).getDescribe().getPickListValues();

    // Add these values to the selectoption list.
    for (Schema.PicklistEntry a: values) {
    allOpts.add(a.getValue());
    }
    system.debug('allOpts ---->' + allOpts);
    allOpts.sort();
    return allOpts;
    }
    }

    Component

    ============

    <aura:component controller="fetchPicklistOptsController">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="objInfo" type="account" default="{sobjectType : 'Account'}" />

    <div class="slds-form-element">
    <label class="slds-form-element__label" for="select-01">Select Label</label>
    <div class="slds-select_container">
    <ui:inputSelect  aura:id="accIndustry" class="slds-select"  change="{!c.onPicklistChange}"/>
    </div>
    </div>
    </aura:component>

    Controller

    ========

    ({
    doInit: function(component, event, helper) {
    helper.fetchPickListVal(component, 'Industry', 'accIndustry');
    },
    onPicklistChange: function(component, event, helper) {
    // get the value of select option
    alert(event.getSource().get("v.value"));
    },
    })
    Helper

    ======

    ({
    fetchPickListVal: function(component, fieldName, elementId) {
    var action = component.get("c.getselectOptions");
    action.setParams({
    "objObject": component.get("v.objInfo"),
    "fld": fieldName
    });
    var opts = [];
    action.setCallback(this, function(response) {
    if (response.getState() == "SUCCESS") {
    var allValues = response.getReturnValue();

    if (allValues != undefined && allValues.length > 0) {
    opts.push({
    class: "optionClass",
    label: "--- None ---",
    value: ""
    });
    }
    for (var i = 0; i < allValues.length; i++) {
    opts.push({
    class: "optionClass",
    label: allValues[i],
    value: allValues[i]
    });
    }
    component.find(elementId).set("v.options", opts);
    }
    });
    $A.enqueueAction(action);
    },
    })

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos