Skip to content

Instantly share code, notes, and snippets.

@sudikrt
Created August 8, 2019 19:12
Show Gist options
  • Save sudikrt/9d1e638e2a9bed8a52afa06f68807904 to your computer and use it in GitHub Desktop.
Save sudikrt/9d1e638e2a9bed8a52afa06f68807904 to your computer and use it in GitHub Desktop.
This Utility class helps to get the pick-list values based on record type (those pick-list values which are available to given record type)
public class Ui_PickListUtility {
public List<Values> values;
public static List<PickListValueWrapper> picklistValuesByRecordType (String objectName, String fieldName, String recordTypeId) {
List<PickListValueWrapper> result = new List<PickListValueWrapper> ();
result.add (new PickListValueWrapper ('--None--', ''));
try {
Http http = new Http ();
HttpRequest request = new HttpRequest ();
//String host = System.Url.getOrgDomainUrl ().toExternalForm ();
String url = 'callout:Your_Named_Credential' + '/services/data/v41.0/ui-api/object-info/' + objectName + '/picklist-values/' + recordTypeId + '/' + fieldName;
request.setEndpoint(url);
request.setMethod('GET');
//System.debug('SESSION ID :' + UserInfo.getSessionId());
//request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
for (Values values : parse(response.getBody()).values) {
result.add (new PickListValueWrapper (values.label, values.value));
}
}
} catch (Exception e) {
System.debug('Exception : ' + e);
}
System.debug('Result :' + result);
return result;
}
@TestVisible
private class Values {
public Object attributes;
public String label;
public List<ControllerValues> validFor;
public String value;
}
@TestVisible
private class ControllerValues {
}
public static Ui_PickListUtility parse(String json) {
return (Ui_PickListUtility) System.JSON.deserialize(json, Ui_PickListUtility.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment