0

I'm able to create a global picklist -'Fruits'

 String fieldDef = '{"Metadata":{"customValue":[{"label":"Apple","valueName":"Apple"},{"label":"Orange","valueName":"Orange"}],"description":null,"masterLabel":"Fruits","sorted":false,"urls":null},"FullName":"Fruits"}';
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());

Now I'm trying to update the 'Fruits' global picklist values. Apple->Grape, Orange->Strawberry

String configType = 'Fruits';
String fieldDef = '{"Metadata":{"customValue":[{"label":"Apple","valueName":"Grape"},{"label":"Orange","valueName":"Strawberry"}],"description":null,"masterLabel":"'+configType+'","sorted":false,"urls":null},"FullName":"'+configType+'"}';
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/query/?q=select+id+from+globalvalueset+where+MasterLabel=\''+
   configType+'\'+limit+1?_HttpMethod=PATCH');
req.setMethod('POST');
Http httpreq = new Http();
HttpResponse res  = httpreq.send(req);
system.debug(res.getBody());

But I'm receiving this error -- [{"errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'POST' not allowed. Allowed are HEAD,GET"}]

1
  • I'd expect that the query resource isn't one to be used for updating data, so your error shouldn't really be that much of a surprise. I'm not familiar enough with the tooling API to be able to answer this question, but I'd imagine that the right API resource to use would be contained somewhere in the documentation for the Tooling API.
    – Derek F
    Commented Apr 19, 2019 at 15:06

1 Answer 1

1

You seem to be sending a PATCH request here req.setEndpoint('....?_HttpMethod=PATCH'); then setting the method as POST req.setMethod('POST'); whereas you actually want to perform a GET request here as you are trying to retrieve results based on a query.

Your request should be modified as:

req.setEndpoint(
    URL.getSalesforceBaseUrl().toExternalForm()
    +'/services/data/v41.0/tooling/query/?q=select+id+from+globalvalueset+where+MasterLabel=\''+configType
    +'\'+limit+1'); // remove the PATCH request parameter from here

req.setMethod('GET'); // change this to GET

If you want to update a particular record, then you should be using PATCH here along with the Id of the record to be updated.

6
  • I'm trying to update the record which has ID there. That's why i put it as PATCH along with the ID. If you look at the link above i referenced above, I already created a global picklist, and now trying to update one of the existing record. Commented Apr 19, 2019 at 1:20
  • But your code does not reflect that. You should edit your question then to reflect what exactly are you passing to update the record. In its current form, you are just querying and it should be a GET request.
    – Jayant Das
    Commented Apr 19, 2019 at 1:27
  • I edited my question to reflect my entire question. thanks Jayant Das Commented Apr 19, 2019 at 1:46
  • You still ate not updating a record but only querying it. I will recommend to take a look at REST API docs related to updates
    – Jayant Das
    Commented Apr 19, 2019 at 1:51
  • I got what you are saying. Basically i cannot combine to fetch Id and update values in one call. Need to make an API call to fetch the Id first using GET, and by using that Id make a POST. Thanks again! Commented Apr 19, 2019 at 2:02

You must log in to answer this question.

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