I have the following code, that retrieves picklist values dependent on a record type:
public static List<String> getPicklistValuesByRecordType(String objectType, String recordTypeId, String fieldName) {
//Endpoint
system.debug('objectType : ' + objectType);
system.debug('recordTypeId : ' + recordTypeId);
system.debug('fieldName : ' + fieldName);
String endpoint = URL.getSalesforceBaseUrl().toExternalForm();
endpoint += '/services/data/v41.0';
endpoint += '/ui-api/object-info/{0}/picklist-values/{1}/{2}';
endpoint = String.format(endpoint, new String[]{ objectType, recordTypeId, fieldName });
EncodingUtil.urlEncode(endpoint,'UTF-8');
//HTTP Request send
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setEndpoint(endpoint);
req.setMethod('GET');
system.debug('endpoint: ' + endpoint);
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug('res: ' + res);
//Parse response
List<String> result = new List<String>();
Map<String,Object> root = (Map<String,Object>) JSON.deserializeUntyped(res.getBody());
if(!root.containsKey('values')){
return result;
}
List<Object> pValues = (List<Object>)root.get('values');
for(Object pValue : pValues){
Map<String,Object> pValueMap = (Map<String,Object>)pValue;
result.add((String)pValueMap.get('value'));
}
System.debug(result);
return result;
}
When i run the following code from Execute Anonymous Window, i receive
System.HttpResponse[Status=OK, StatusCode=200]
But when running it from apex code that's being called by a component, i receive
System.HttpResponse[Status=Unauthorized, StatusCode=401]
Maybe its because i can't get UserInfo.getSessionId() from the code?
Please advise.