API to Create a Sandbox Org

Some weeks ago I published my first try  with Tooling API where I could explain how to create a Custom field with this API, but when I worked on that, what I really needed was to create a Sandbox programatically. OMG!! A Sandbox!! And how can I do it?

At the end, a Sandbox is metadata, and as we learnt on my previous blog, Tooling API will help you to create metadata records. SandboxInfo is the object that represents a Sandbox, and we will use it to create our new environment.

SandboxInfo supports Get, Post, Patch and Delete REST API calls, that means, that with Post we will create a new Sandbox, with Patch we will do an update, a refresh at the end, and the Delete would remove the organization.

As you can see in the link there are lot of options when you create a Sandbox, like create the copy Chatter from production to Sandbox, or event it gives you the option to use Template to create the org. But if you need a simple environment, with below JSON body is more than enough:

{
   "Description": "New Test Sandbox",
   "HistoryDays": 0,
   "LicenseType": "DEVELOPER",
   "SandboxName": "SandboxTest"
}

And in order to execute it you can make a Post REST call with this url:

/services/data/v42.0/tooling/sobjects/SandboxInfo

This call will return an Id that you can use in order to check the creation progress via another object, SandboxProcess. You can perform a query against this object filtering by SandboxInfoId field, and get useful information such us CopyProgress that represents how much of the copy has been already performed.

Finally I would like to remind you that you could only make this call against a Production org, and if your user has the rights to create Sandboxes. Otherwise, it will fail.

 

API to Create CustomField

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.

Captura de pantalla 2018-06-03 a las 14.22.08

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

Captura de pantalla 2018-06-03 a las 14.26.42Captura de pantalla 2018-06-03 a las 14.26.52

and use it in the get call

/services/data/v42.0/tooling/sobjects/CustomField/00Nb0000009GqvP

Getting this result

Captura de pantalla 2018-06-03 a las 14.29.51Captura de pantalla 2018-06-03 a las 14.30.08Captura de pantalla 2018-06-03 a las 14.30.18

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:

Captura de pantalla 2018-06-03 a las 14.40.51.png

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:

Captura de pantalla 2018-06-03 a las 14.45.05

And in the org the new field created

Captura de pantalla 2018-06-03 a las 14.45.22

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 😉