3

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

2

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());
  • 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 '18 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 '18 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 '18 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 '18 at 5:41
1

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

  • Is there any way for adding the new values to all the Picklist values whose are dependent on Record types? – Amjad khan Sep 27 at 14:17
  • @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. – Daniel Ballinger Sep 29 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.