7

Can someone tell me how to create and update Global value set using tooling API? No sure what so send in request body.

2 Answers 2

7

As mentioned by Daniel, you can develop code to create/update GlobalValueSet.

By using tooling api, we will need to make http request to endpoint (/services/data/v41.0/tooling/sobjects/GlobalValueSet). While case of insert, no need to provide picklist value set it. But while updating that we must need to provide as below.

Insert GlobalValueset:

//you would need to update picklist values here
String fieldDef = '{"Metadata":{"customValue":[{"color":null,"default":false,"description":null,"isActive":null,"label":"Ayubb","urls":null,"valueName":"Ayub"},{"color":null,"default":false,"description":null,"isActive":null,"label":"Ansari","urls":null,"valueName":"Ansari"}],"description":null,"masterLabel":"GemsofMyLife","sorted":false,"urls":null},"FullName":"Gems_of_My_Life"}';
HttpRequest req = new HttpRequest();
req.setBody(fieldDef);
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');      
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v41.0/tooling/sobjects/GlobalValueSet');
req.setMethod('POST');
Http httpreq = new Http();
HttpResponse res  = httpreq.send(req);
system.debug(res.getBody());

Update GlobalValueSet

String fieldDef = '{"Metadata":{"customValue":[{"color":null,"default":false,"description":null,"isActive":null,"label":"Ayubb","urls":null,"valueName":"Ayub"},{"color":null,"default":false,"description":null,"isActive":null,"label":"Ansari","urls":null,"valueName":"Ansari"}],"description":null,"masterLabel":"GemsofMyLife","sorted":false,"urls":null},"FullName":"Gems_of_My_Life"}';
HttpRequest req = new HttpRequest();
req.setBody(fieldDef);
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');      
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v41.0/tooling/sobjects/GlobalValueSet/0Nt90000000XZHw?_HttpMethod=PATCH');**//replace id with your GlobalPicklist Id**
req.setMethod('POST');
//req.setHeader('X-HTTP-Method-Override','PATCH');
Http httpreq = new Http();
HttpResponse res  = httpreq.send(req);
system.debug(res.getBody());
8
  • 1
    Thanks Ayub.. but i am running a SOQL.. How to retrive multiple global value set... /services/data/v41.0/tooling/query?q=Select+id,MasterLabel,Description,fullname,metadata+from+GlobalValueSet+limit+1... If you use this it will set limit to 1 but its not working without limit 1.
    – Arteezy
    May 15, 2018 at 9:52
  • In order to update, you only need one Id...so i dont think it make sense to query multiple records...or provide where clause...
    – Ayub
    May 15, 2018 at 10:05
  • I also want to get full list of the global value sets with the values... Thats why i was asking... Is it possible?
    – Arteezy
    May 17, 2018 at 19:02
  • @Arteezy ..you can do that..but do not pass Fullname and Metadata in Query...it does not support bulk records in query....use MasterLabel field if you need to print somewhere...here would be query to retrieve all: /services/data/v41.0/tooling/query?q=Select+id,MasterLabel,Description+from+GlobalValueSet
    – Ayub
    May 18, 2018 at 5:41
  • Why do we have isActive as Null, can we use it to Activate and Deactivate the picklist values from global picklist? Jul 20, 2020 at 9:55
4

With things like this the documentation is the place to start - GlobalValueSet.

We can then jump over to Workbench to try it out with the REST API:

GET /services/data/v41.0/tooling/sobjects/GlobalValueSet/0Nt7F000000cIqx

I got the GlobalValueSet ID (Keyprefix 0Nt) straight from the UI when making a test record.

The gave me the raw response:

{
  "attributes" : {
    "type" : "GlobalValueSet",
    "url" : "/services/data/v41.0/tooling/sobjects/GlobalValueSet/0Nt7F000000cIqxSAE"
  },
  "Metadata" : {
    "customValue" : [ {
      "color" : null,
      "default" : false,
      "description" : null,
      "isActive" : null,
      "label" : "First",
      "urls" : null,
      "valueName" : "First"
    }, {
      "color" : null,
      "default" : false,
      "description" : null,
      "isActive" : null,
      "label" : "Second",
      "urls" : null,
      "valueName" : "Second"
    }, {
      "color" : null,
      "default" : false,
      "description" : null,
      "isActive" : null,
      "label" : "Third",
      "urls" : null,
      "valueName" : "Third"
    } ],
    "description" : null,
    "masterLabel" : "Test",
    "sorted" : false,
    "urls" : null
  },
  "FullName" : "Test",
  "Id" : "0Nt7F000000cIqxSAE",
  "DeveloperName" : "Test",
  "MasterLabel" : "Test",
  "Description" : null,
  "NamespacePrefix" : null,
  "ManageableState" : "unmanaged",
  "CreatedDate" : "2018-05-08T10:45:35.000+0000",
  "CreatedById" : "0057F000002XauUQAS",
  "LastModifiedDate" : "2018-05-08T10:45:35.000+0000",
  "LastModifiedById" : "0057F000002XauUQAS"
}

With some slight alterations that can be replayed back to create the new record.

  1. Trim out the attributes
  2. Trim out any other top level fields except the Metadata and FullName
  3. Modify the FullName and masterLabel
  4. Modify the customValue's as required.

Use Workbench to POST to /services/data/v41.0/tooling/sobjects/GlobalValueSet.

E.g.

{
  "Metadata" : {
    "customValue" : [ {
      "color" : null,
      "default" : false,
      "description" : null,
      "isActive" : null,
      "label" : "First",
      "urls" : null,
      "valueName" : "First"
    }, {
      "color" : null,
      "default" : false,
      "description" : null,
      "isActive" : null,
      "label" : "Second",
      "urls" : null,
      "valueName" : "Second"
    }, {
      "color" : null,
      "default" : false,
      "description" : null,
      "isActive" : null,
      "label" : "Third",
      "urls" : null,
      "valueName" : "Third"
    } ],
    "description" : null,
    "masterLabel" : "Test2",
    "sorted" : false,
    "urls" : null
  },
  "FullName" : "Test2"
}

Voilà -

enter image description here

enter image description here

4
  • Is there any way for adding the new values to all the Picklist values whose are dependent on Record types?
    – Amjad khan
    Sep 27, 2019 at 14:17
  • 1
    @Amjadkhan I'd recommend asking a new question to help determine that. You can link it to this question and include anything you have tried already. Sep 29, 2019 at 19:35
  • @DanielBallinger When doing a PATCH http method REST call to update the valueName of a picklist value I get the "Duplicate label" error. Do you know if picklist value's API name or its label can be updated (using the REST http ) ?
    – Tickle Me
    Jan 31, 2021 at 5:28
  • 1
    @TickleMe I haven't explicitly tried. Maybe raise a new question with what you tried and link it back to this one. Feb 3, 2021 at 21:33

You must log in to answer this question.

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