Fetch Picklist values by RecordTypeId in Lightning component

I have developed a Lightning data table component recently with inline edit support like picklist, textarea, lookup field etc. I have noticed one thing on the populated picklist values, since the picklist values are restricted by RecordType, but I can see all the picklist values in UI (basically am using Picklist meta data to get all the values for the given picklist field in Apex).

Visualforce inputfield have OOB functionality to get appropriate picklist values based on the RecordType but its not supported in Lightning component.

How we can achieve the same functionality in Lightning?

Apex is not supporting to fetch picklist values by RecordType and an idea is already posted in Ideaxchange.

Workaround:

We can use the Salesforce User Interface REST api to get picklist values by RecordTypeId. You can find the details from Salesforce UI developer guide.

Sample Apex code for this usecase below,

        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String host = System.Url.getSalesforceBaseURL().toExternalForm();
        String url = host + '/services/data/v42.0/ui-api/object-info/'{sobjectName}'/picklist-values/'{recordTypeId}'/'{fieldAPIName};        
        request.setEndpoint(url);
        request.setMethod('GET');  
        request.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
        HttpResponse response;        
        response = http.send(request);        
        Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        system.debug(meta);
		List<SelectOption> optionsList = new List<SelectOption>();
        optionsList.add(new SelectOption('--None--', ''));
        if(meta.containsKey('values')){                                
                for(Object o: (List<Object> )meta.get('values')){
                    Map<String, object > temp = (Map<String, object>)o;                    
                    optionsList.add(new SelectOption((String)temp.get('label'), (String)temp.get('value')));
                }
            }        
        

We can leverage the same sample code to fetch dependency picklist values (temp.get('validFor')). Please refer UI developer guide for more info.

Note:

Replace this placeholders {sobjectName}, {recordTypeId},{fieldAPIName} with proper details.

Add your host name in Remote Site Settings and execute your lightning component.

Take care of test methods to provide code coverage.

Other way: we can use lightning:inputField tag with lightning:recordEditForm tag but not sure about this picklist use case.

Happy Learning !!!


If I have to loop through several objects and each object has its recordtype, what is the best way to consume the API for this problem?

Like
Reply

Do you know what all permissions required to use this ? I am facing a strange issue, this API is working for one profile and not working for another profile. I found one similar issue. https://salesforce.stackexchange.com/questions/284860/rest-api-picklist-ui-api-error

Like
Reply

Independent picklist is rendering just fine. I'm trying to parse the response to render the dependent picklist values, but failing. Can you help or share any working code? Efforts will be highly appreciated, Yogaraj.

Like
Reply

Hi, i am trying to use this solution for a lightning component used in customer community. It is working perfectly for admin users but for customer community users the API call is throwing 403-forbidden error. Do you know how can we make it work for community users? Thanks!

To view or add a comment, sign in

More articles by Yogaraj Kandasamy Vadivel

Others also viewed

Explore content categories