1

We used to retrieve Picklist values in Apex through and object and a field like this :

Schema.DescribeFieldResult F = <Object_API_Name>.<Field_API_Name>.getDescribe();
Schema.sObjectField T = F.getSObjectField();
List<PicklistEntry> entries = T.getDescribe().getPicklistValues()

But now, I want to know if it's possible to retrieve Global Value Set in Apex directly through its name, and without using an Object name and a field name.

enter image description here

Thank You

1 Answer 1

2

There is no easy way to do this without using tooling or Metadata API.

Using tooling API you can retrieve it using the below pseudo codee,

String GLOBALVALUESETNAME = 'xyz'; //name of the global valueset you want to retrieve

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + System.userInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v50.0/tooling/query/?q=Select+Metadata,Id+FROM+GlobalValueSet+WHERE+DeveloperName=\''+GLOBALVALUESETNAME+'\'');
req.setMethod('GET');
Http request = new Http();
HttpResponse res  = request.send(req);
system.debug(res.getBody());   
5
  • It doesn't work. It only return infos about the GLOBALVALUESET : {"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"GlobalValueSet","records":[{"attributes":{"type":"GlobalValueSet","url":"/services/data/v50.0/tooling/sobjects/GlobalValueSet/myId"},"Id":"myId"}]}. I want to retrieve all the picklist values of the Global Value Set without using the Object name and the field name.
    – kabrice
    Oct 23, 2020 at 8:12
  • Updated the query! Check now Oct 23, 2020 at 9:31
  • Still doesn't work. I get the following error : [{"message":"\nSelect CustomValue,Id FROM GlobalValueSet\n ^\nERROR at Row:1:Column:8\nNo such column 'CustomValue' on entity 'GlobalValueSet'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.","errorCode":"INVALID_FIELD"}]
    – kabrice
    Oct 23, 2020 at 13:27
  • 1
    Solved :). Just replaced CustomValue by MetaData in the query
    – kabrice
    Oct 23, 2020 at 14:16
  • @kabrice This looks like a documentation bug! Hopefully, you acknowledge the approach suggestions I had for you Oct 23, 2020 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .