0

I am trying to get all the custom fields, but on using the tooling API through APEX am getting the below error

  • APEX

Code :

String baseURL = URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v51.0/query?q=';
String endPoint = baseURL + 'SELECT+Id+from+customField';
Http h = new Http();
HttpRequest hr = new HttpRequest();
hr.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
hr.setTimeout(60000);
hr.setEndpoint(endPoint);
hr.setMethod('GET');
HttpResponse res = h.send(hr);

ERROR System.HttpResponse[Status=Bad Request, StatusCode=400]


  • WorkBeanch

Request : /services/data/v51.0/query?q=select+Id+from+customFields

ERROR:

 [ {
  "message" : "\nselect Id from customFields\n               ^\nERROR at Row:1:Column:16\nsObject type 'customFields' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.",
  "errorCode" : "INVALID_TYPE"
} ]
2
  • 3
    You are not using tooling api.For tooling api URI should be /services/data/v51.0/tooling/
    – kurunve
    Mar 30, 2021 at 12:43
  • I want to query all the custom fields as I can do it in developer console.
    – Sam77
    Mar 30, 2021 at 16:22

1 Answer 1

4

The Tooling API uses a different base URI to the standard REST API: Docs

The base URI for each Tooling API REST resource is http://domain/services/data/vXX.X/tooling/ where domain is a My Domain or a custom domain and vXX.X is the API version number. For example: https://MyDomainName.my.salesforce.com/services/data/v35.0/tooling/

So, as kurunve commented, your anonyomous Apex code only needs a minimal modification to the url to work.

String baseURL = URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v51.0/tooling/query?q=';
String endPoint = baseURL + 'SELECT+Id+from+customField';
Http h = new Http();
HttpRequest hr = new HttpRequest();
hr.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
hr.setTimeout(60000);
hr.setEndpoint(endPoint);
hr.setMethod('GET');
HttpResponse res = h.send(hr);
System.debug(res);

You must log in to answer this question.

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