Create/Update Global Value Set using Tooling API
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());
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.
- Trim out the
attributes
- Trim out any other top level fields except the
Metadata
andFullName
- Modify the
FullName
andmasterLabel
- 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à -