function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John Rodes 3John Rodes 3 

How to get dynamic values of picklist in aura

I need to dynamically fetch the picklist values 
Best Answer chosen by John Rodes 3
sachinarorasfsachinarorasf
Hi John,

Please apply the below code to get the dynamic values of picklist :

 @AuraEnabled(cacheable=true)
    public static List<PickListDetails> getPicklistvalues(String ObjectApi_name, String field_name) {
        try{
            List <PickListDetails> lstPickvals = new List <PickListDetails>();
            Schema.SObjectType targetType = Schema.getGlobalDescribe().get(ObjectApi_name);//From the Object Api name retrieving the SObject
            SObject Object_name = targetType.newSObject();
            Schema.sObjectType sobject_type = Object_name.getSObjectType(); //grab the sobject that was passed
            Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
            Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
            List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
            for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
                if (a.isActive()) {
                    PickListDetails pickListdetailobj = new PickListDetails();
                    pickListdetailobj.label = a.getLabel();
                    pickListdetailobj.value = a.getValue();
                    lstPickvals.add(pickListdetailobj);//add the value to our final list
                }
            }
            system.debug('lstPickvals ::'+lstPickvals);
            return lstPickvals;
        }Catch(Exception ex){
            System.debug('Error!!!'+''+'Error on Line '+ex.getLineNumber()+' Cause : '+ex.getCause());
            throw new AuraHandledException(ex.getMessage());
        }
        
    }
    public class PickListDetails{
        @AuraEnabled
        public string label{get;set;}
        @AuraEnabled
        public string value{get;set;}
    }

To know more please refer to the following blog :
https://www.biswajeetsamal.com/blog/get-picklist-values-dynamically-in-lightning-component/

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com