Finding the ID of a Salesforce Global Value Set

2019-06-10 05:39发布

问题:

I'm attempting to write an APEX class that will add items to a global valueset, but in order to do so I need to know the ID of the global value set. Is there a way to find the ID of the global valueset (through APEX, not by looking at the URL) that a picklist field is using? Ideally I'd be able to do something similar to:

Case.picklistField__c.getdescribe();

and get a response that includes the ID of the global value set that it uses - that way I can then use my the metadataAPI to update the values.

Alternatively if I could find the global valueset by name I could use that with the metadata api as a work around.

UPDATE: Was able to get this to work using Eyescreams suggestion with the tooling API - full implementation:

    String gvsName = 'TestValueSet'; //name of the global valueset you want the Id for goes here

    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
    req.setHeader('Content-Type', 'application/json');      
    req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v41.0/tooling/query/?q=select+id+from+globalvalueset+Where+developername='+gvsName);
    req.setMethod('GET');
    Http httpreq = new Http();
    HttpResponse res  = httpreq.send(req);
    system.debug(res.getBody()); 

回答1:

SELECT Id, FullName, Description
FROM GlobalValueSet

But it's not available in straight Apex queries, you'd need Tooling API (meaning a REST callout). You can play with it in Developer Console -> Query Editor, just check the tooling api checkbox on bottom