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
Jayanth ThathapudiJayanth Thathapudi 

How to get picklist values based on recordtype ?

Jayanth ThathapudiJayanth Thathapudi
UI-API workaround worked me.

public static List<String> getPickListValuesByRecordTypeId(
string objectName,
string recordTypeId,
string fieldApiName
) {
Httprequest req = new HttpRequest();
req.setEndpoint(
URL.getSalesforceBaseUrl().toExternalForm() +
'/services/data/v50.0/ui-api/object-info/' +
objectName +
'/picklist-values/' +
recordTypeId +
'/' +
fieldApiName
);
req.setMethod('GET');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
Http http = new Http();
HttpResponse res = http.send(req);
//ystem.debug('body11:' + res1.getBody());
Map<String, Object> respBodyMap = (Map<String, Object>) Json.deserializeUntyped(
res.getBody()
);
//system.debug(lst_JsonParse.get('values'));
List<object> valuesObjList = (List<object>) respBodyMap.get('values');
List<string> pickValues = new List<string>();
for (object obj : valuesObjList) {
Map<string, object> valuesMap = (map<string, object>) obj;
pickValues.add((string) valuesMap.get('value'));
}
system.debug('pvalues ' + pickValues);
return pickValues;
}

 
Malika Pathak 9Malika Pathak 9

Hi jayanth,

Can you please provide me what data is coming in respBodyMap  and valuesObjList 

Jayanth ThathapudiJayanth Thathapudi
Hi Malika,

respBodyMap

{"controllerValues":{},"defaultValue":{"attributes":{"converted":false,"picklistAtrributesValueType":"LeadStatus"},"label":"New","validFor":[],"value":"New"},"eTag":"574d09f5a9c1e4a57ebd (12 more) ...","url":"/services/data/v50.0 (66 more) ...","values":[{"attributes":{"converted":false,"picklistAtrributesValueType":"LeadStatus"},"label":"New","validFor":[],"value":"New"},{"attributes":{"converted":false,"picklistAtrributesValueType":"LeadStatus"},"label":"Disqualified","validFor":[],"value":"Disqualified"},{"attributes":{"converted":false,"picklistAtrributesValueType":"LeadStatus"},"label":"Working","validFor":[],"value":"Working"},{"attributes":{"converted":true,"picklistAtrributesValueType":"LeadStatus"},"label":"Qualified","validFor":[],"value":"Qualified"}]}

valuesObjList

[{"attributes":{"converted":false,"picklistAtrributesValueType":"LeadStatus"},"label":"New","validFor":[],"value":"New"},{"attributes":{"converted":false,"picklistAtrributesValueType":"LeadStatus"},"label":"Disqualified","validFor":[],"value":"Disqualified"},{"attributes":{"converted":false,"picklistAtrributesValueType":"LeadStatus"},"label":"Working","validFor":[],"value":"Working"},{"attributes":{"converted":true,"picklistAtrributesValueType":"LeadStatus"},"label":"Qualified","validFor":[],"value":"Qualified"}]

Please feel free to play on the above code in Execute Anonymous and let me know if you have any concerns.

Thanks,
Jay 
Venky KambhamVenky Kambham
Hey Jayanth, Great solution. Thanks for sharing!
Venky KambhamVenky Kambham
Change this URL.getSalesforceBaseUrl().toExternalForm() to URL.getOrgDomainUrl().toExternalForm(), otherwise we will get "Unauthorized endpoint" error, when we access from Visualforce page.
Emmanuel Maina 2Emmanuel Maina 2
Hey Jayanth,

I am getting a 401 error when I use the solution above in an Aura app. Do you have a clue on what I should do to solve this?
sai007sai007
Hi Emmanuel Maina 2

Do you got the solution?. I'm also facing the same issue