In the last few weeks, and for my first time, I started working with the Salesforce Tooling API. This scared me a bit because new beginnings are hard for me, but at the same time I liked the challenge.
In a similar way other Salesforce APIs help you to get records data, Tooling API is used in order to access / create metadata. But Tooling could sound weird, how can I run this one? Not difficult at all as this is also accesible via SOAP and REST API. What does it mean? You can make Tooling API calls via REST calls as long as the Object says that. For instance, ApexCodeCoverage supports Query and Get REST API calls, while with CustomField you can also do Post and Patch calls.
Let’s dig a bi. If you open Workbench and execute
/services/data/v42.0/tooling/sobjects/CustomField
you get information about this Salesforce object, so the Get REST API call works.
But what about if I need to retrieve information about a certain CustomField? Same as you were trying to get information about a certain Account record.
Having the custom field Field1__c, I only need to look for its Id
and use it in the get call
/services/data/v42.0/tooling/sobjects/CustomField/00Nb0000009GqvP
Getting this result
That is really cool, but what I really need is to create a new custom field related to CO1__c custom object, and maybe I didn’t write the right words in google, but I didn’t find it anywhere so I took me few rounds till I get it.
My first try was to create a JSON body following Salesforce help:
{ "DeveloperName": "CO1__c.test__c", "ManageableState": "unmanaged", "Metadata": { "label": "test", "description": "my new test field", "length": "32", "type": "string" }, "TableEnumOrId":"01Ib0000000gSh6" }
And after running a POST call with the url:
/services/data/v42.0/tooling/sobjects/CustomField/
I got this error:
So I continued testing things. Removing fields, changing values etc, till I got it with the following JSON body
{ "FullName": "CO1__c.test__c", "Metadata": { "label": "test", "description": "my new test field", "required": false, "externalId": false, "type": "Text", "length": 32 } }
Getting the result:
And in the org the new field created
What about if I need to do it in Apex? With a simple HTTPRequest call you can run same code I executed in workbench.
Another alternative is create your own ToolingAPI wrapper class as Andy Fawcett explains on this old entry.
And that is all for this entry, but don’t think this was my original challenge. What I was asked to do was to check if I could create a Sandbox environment via the Tooling API … keep an eye on future blogs because I will publish it if I get it 😉
Pingback: API to Create a Sandbox Org – Agustina odeian