|
public class DependentPicklistController { |
|
|
|
@AuraEnabled |
|
public static Map<String,List<String>> getPicklistValues( String objectName, string fieldName, string recordTypeName ) { |
|
|
|
System.debug( 'objectName : ' + objectName ); |
|
System.debug( 'fieldName : ' + fieldName ); |
|
System.debug( 'recordTypeName : ' + recordTypeName ); |
|
|
|
if(String.isBlank( objectName) || String.isBlank(fieldName)){ |
|
return null; |
|
} |
|
|
|
String recordTypeId; |
|
if( !String.isBlank( recordTypeName ) ){ |
|
recordTypeId = Schema.getGlobalDescribe().get( objectName ).getDescribe().getRecordTypeInfosByDeveloperName().get( recordTypeName ).getRecordTypeId(); |
|
} |
|
if( recordTypeId == null || recordTypeId == '' ){ |
|
recordTypeId = Schema.getGlobalDescribe().get( objectName ).getDescribe().getRecordTypeInfosByDeveloperName().get( 'Master' ).getRecordTypeId(); |
|
} |
|
|
|
Map<String, List<String>> mapPicklistValues = new Map<String, List<String>>(); |
|
|
|
mapPicklistValues = getValues( objectName, recordTypeId, fieldName ); |
|
|
|
return mapPicklistValues; |
|
} |
|
|
|
|
|
public static Map<String, List<String>> getValues(String objectType, String recordTypeId, String fieldName) { |
|
|
|
// Create the endpoint dynamically for your og |
|
String Endpoint = URL.getSalesforceBaseUrl().toExternalForm(); |
|
endpoint += '/services/data/v45.0'; |
|
endpoint += '/ui-api/object-info/' + objectType + '/picklist-values/' + recordTypeId + '/' + fieldName; |
|
|
|
|
|
EncodingUtil.urlEncode(endpoint,'UTF-8'); |
|
|
|
// Call the API in order to get the values |
|
HttpRequest req = new HttpRequest(); |
|
|
|
// Since, we can not use the UserInfo.getSessionId() method here, I am using a Visualforce page in order |
|
// to extract the session id which we can use for making API calls |
|
req.setHeader('Authorization', 'OAuth ' + getSessionIdFromVFPage(Page.SessionId)); |
|
req.setHeader('Accept', 'application/json '); |
|
req.setHeader('Content-Type', 'application/json'); |
|
req.setEndpoint(endpoint); |
|
req.setMethod('GET'); |
|
Http http = new Http(); |
|
|
|
HTTPResponse res = http.send(req); |
|
Map<String, String> result = new Map<String,String>(); |
|
Map<String, List<String>> mapControllingWithDependentList = new Map<String,List<String>>(); |
|
Map<Object, String> mapControllingValueWithIndex = new Map<Object,String>(); |
|
Map<String, List<String>> mapPicklistValues = new Map<String,List<String>>(); |
|
|
|
// Parse the response and build the dependent and controlling picklist values map |
|
if( res.getStatus() == 'OK' && res.getStatusCode() == 200 ) { |
|
|
|
Map<String,Object> root = (Map<String,Object>) JSON.deserializeUntyped( res.getBody() ); |
|
|
|
System.debug( ' Response Body : ' + res.getBody() ); |
|
|
|
|
|
// Get all the controlling values from response |
|
if( root.containsKey('controllerValues') ) { |
|
|
|
Map<String, Object> controllingValues = (Map<String, Object>) root.get( 'controllerValues' ); |
|
|
|
// Map of all the controlling values with their index |
|
for( String cValue: controllingValues.keySet() ) { |
|
|
|
mapControllingValueWithIndex.put( controllingValues.get(cValue), cValue ); |
|
} |
|
} |
|
|
|
System.debug('mapControllingValueWithIndex : ' + JSON.serializePretty( mapControllingValueWithIndex ) ); |
|
|
|
if( !root.containsKey( 'values' ) ){ |
|
|
|
return mapControllingWithDependentList; |
|
} |
|
|
|
// Get all the dependent values from the response returned with the Validfor attribute |
|
// Each bit in the bitmap indicates whether this dependent picklist value is "valid for" a corresponding controlling field value |
|
// The value in the validFor member is a Base64-encoded bitmap. |
|
List<Object> pValues = (List<Object>) root.get( 'values' ); |
|
for(Object pValue : pValues) { |
|
|
|
Map<String,Object> pValueMap = (Map<String,Object>)pValue; |
|
result.put( (String) pValueMap.get('value'), (String) pValueMap.get('label') ); |
|
|
|
for(Object validfor : (List<Object>)pValueMap.get('validFor')) { |
|
|
|
//Map the dependent Values List with their Controlling Value |
|
if( mapControllingValueWithIndex.containsKey( validfor ) ) { |
|
|
|
if( !mapControllingWithDependentList.containsKey( mapControllingValueWithIndex.get( validfor ) ) ) { |
|
|
|
mapControllingWithDependentList.put( mapControllingValueWithIndex.get( validfor ), new List<String>() ); |
|
} |
|
|
|
mapControllingWithDependentList.get( mapControllingValueWithIndex.get( validfor ) ).add( (String) pValueMap.get( 'label' ) ); |
|
} |
|
} |
|
} |
|
|
|
System.debug( 'mapControllingWithDependentList : ' + JSON.serializePretty( mapControllingWithDependentList ) ); |
|
|
|
//Map all the controlling values |
|
for( String controllingFields : mapControllingValueWithIndex.Values() ){ |
|
//Map controllingFields which has no dependent values associated to it |
|
if( !mapPicklistValues.containsKey( controllingFields ) ) { |
|
|
|
mapPicklistValues.put(controllingFields,new List<String>()); |
|
} |
|
//Map controllingFields which has dependent values associated to it |
|
if(mapPicklistValues.containsKey( controllingFields ) && |
|
mapControllingWithDependentList.containsKey( controllingFields ) ) { |
|
|
|
mapPicklistValues.get( controllingFields ).addAll( mapControllingWithDependentList.get( controllingFields ) ); |
|
} |
|
} |
|
} |
|
else{ |
|
|
|
System.debug( 'mapPicklistValues : ' + JSON.serializePretty( mapPicklistValues ) ); |
|
} |
|
|
|
//Return the Map of Controlling fields with the List of Dependent fields on the basis of Record Types |
|
System.debug( 'mapPicklistValues : ' + JSON.serializePretty( mapPicklistValues ) ); |
|
|
|
return mapPicklistValues; |
|
|
|
} |
|
|
|
|
|
public static String getSessionIdFromVFPage( PageReference visualforcePage ) { |
|
|
|
if( !Test.isRunningTest() ) { |
|
|
|
String sessionId = visualforcePage.getContent().toString(); |
|
return sessionId; |
|
} |
|
|
|
return UserInfo.getSessionId(); |
|
|
|
} |
|
|
|
} |