+91-9953024688
           

fetch the dependent picklist value – UI API in APEX salesforce

To Fetch the dependent Picklist value:

https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_resources_picklist_values.htm

 

/ui-api/object-info/{objectApiName}/picklist-values/{recordTypeId}/{fieldApiName}
  • objectApiName—The API name of a supported object.
  • recordTypeId—The ID of the record type.
  • fieldApiName—The API name of the picklist field on the object.

public class FetchPicklist
{

public list<DependentValues> getValidPicklistOptionsFromDependencyMatrix(String standardObject, Id recordTypeId, String DependentFieldName, String selectedValue) {
List<DependentValues> dpnPckLst = new List<DependentValues>();
String url = ‘callout:REST_UI_API/services/data/v43.0/ui-api/object-info/’+standardObject+’/picklist-values/’+recordTypeId+’/’+DependentFieldName;

if(String.isNotBlank(getCalloutResponseContents(url))) {
Map<String, Object> response = (Map<String, Object>)JSON.deserializeUntyped(getCalloutResponseContents(url));
Map<String, Object> controllerValues = (Map<String, Object>)response.get(‘controllerValues’);
//System.debug(controllerValues.get(selectedValue));

//Checking if the controller values consists of the specified key
if(controllerValues.containsKey(selectedValue)) {
//Getting all the data in values property of the JSON response, i.e., the dependent picklist values
List<Object> pickListValues = (List<Object>)response.get(‘values’);
System.debug(‘:: pickListValues :: ‘+pickListValues);
String valuesStr = JSON.serialize(response.get(‘values’));
System.debug(‘:: valuesStr :: ‘+valuesStr);
List<pickListValueWrapper> values = (List<pickListValueWrapper>) JSON.deserialize(JSON.serialize(response.get(‘values’)), List<pickListValueWrapper>.class);
System.debug(‘:: values :: ‘+values);
//System.debug(‘pickListValueWrapper values are below:’);
//System.debug(values);
if(values != null && values.size() > 0){
for(pickListValueWrapper value : values) {

if(!value.validFor.isEmpty() && value.validFor.contains(Integer.valueOf(controllerValues.get(selectedValue)))) {

dpnPckLst.add(new DependentValues(value.label,value.value) );
//System.debug(value.label);
}
}
}

}
}
System.debug(‘Dependent picklist values are below: \n’ +dpnpckLst);
return dpnPckLst;

}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Nube Lead Infoventure Pvt Ltd