3

The Tooling API is great - very easy to use from Apex code by making HTTP calls containing straightforward JSON.

Two questions:

  1. Is it still not possible to create a new SObject type e.g. MyNewCustomType__c?
  2. Can a custom field be added to an already existing SObject type?
    • the CustomField documentation lists POST that makes me think the answer is "yes"

1 Answer 1

4
  1. You still cannot create Custom Object with REST API, only with METADATA API.
  2. Yes, you can create a Custom Field on existing sObject, request example:
HttpRequest request = new HttpRequest();
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
request.setHeader('Content-Type', 'application/json');
request.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v41.0/tooling/sobjects/CustomField/');
request.setMethod('POST');

String fieldLabel = 'New_Label';
String objectAPIName = 'Account';
String fieldAPIName = fieldLabel + '__c';

request.setBody('{"Metadata" : {"type" : "Text","description" : "", "inlineHelpText" : "","precision" : null,"label":"' + fieldLabel + '","length" : 255,"required" : false}, "FullName" : "'+objectAPIName+'.'+fieldAPIName+'"}');

Http http = new Http();
HTTPResponse res = http.send(request);
system.debug(res.getBody());
1
  • 1
    That's helpful - thank-you. In fact I want to create a fake object as part of the CI build, so given your answer, I'll use sfdx force:source:deploy of a separate folder containing the object definition instead.
    – Keith C
    Mar 4, 2020 at 10:57

You must log in to answer this question.

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