Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
REST Resource Examples
Example Setup
The following examples use Apex to execute REST requests, but you can use any standard REST tool to access the Tooling API.
1HttpRequest req = new HttpRequest();
2req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
3req.setHeader('Content-Type', 'application/json');1Http h = new Http();
2HttpResponse res = h.send(req);
3system.debug(res.getBody());Retrieve a Description
1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/sobjects/');
2req.setMethod('GET');1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/sobjects/
2TraceFlag/');
3req.setMethod('GET');1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/sobjects/
2TraceFlag/describe/');
3req.setMethod('GET');Manipulate Objects by ID
1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/sobjects/
2MetadataContainer/');
3req.setBody('{"Name":"TestContainer"}');
4req.setMethod('POST');1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/sobjects/
2MetadataContainer/ + containerID + '/');
3req.setMethod('GET');1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/sobjects/
2MetadataContainer/ + containerID + '/');
3req.setBody('{"Name":"NewlyNamedContainer"}');
4req.setMethod('PATCH');1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/query/?q=
2Select+id,Name+from+MetadataContainer+Where+ID=\'' + containerID + '\'');
3req.setMethod('GET');Query Within MetadataContainer
1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/query/?q=
2Select+id,Body,LastSyncDate,Metadata+from+ApexClassMember+Where+MetadataContainerID=\'
3+ containerID + '\'');
4req.setMethod('GET');Check Deployment Status
1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/sobjects/
2ContainerAsyncRequest/' + requestID + '/');
3req.setMethod('GET');Execute Anonymous Apex
To execute anonymous Apex:
1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/executeAnonymous/?
2anonymousBody=System.debug('Test')%3B');
3req.setMethod('GET');Retrieve Apex
To retrieve your Apex classes and triggers, and the global Apex classes and triggers from your installed managed packages:
1req.setEndpoint('https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/apexManifest');
2req.setMethod('GET');Execute Apex Unit Tests
To execute Apex unit tests, use the runTestsSynchronous or runTestsAsynchronous resource. This example illustrates how to POST to the runTestsSynchronous resource using JavaScript. The comment blocks show which objects these calls return.
1var xhttp = new XMLHttpRequest();
2xhttp.open("POST", "https://MyDomainName.my.salesforce.com/services/data/v68.0/tooling/runTestsSynchronous/", true)
3
4// SESSION_ID is the session ID
5xhttp.setRequestHeader("Authorization", "OAuth <SESSION_ID>")
6xhttp.setRequestHeader('Accept', "application/json");
7
8// testObject should include a list of object(s) with the classId and list of
9// desired test methods for the desired classes to be tested
10testObject = {tests: [{classId: "N0tARealClassId", testMethods: ["testMethod1", "testMethod2"]}]}
11requestObject = json.stringify(testObject);
12response = xhttp.send(requestObject)
13response = JSON.parse(response)
14
15/*
16{
17 "successes": [
18 {
19 "namespace": null,
20 "name": "MyTestClass",
21 "methodName": "testMethod1",
22 "id": "N0tARealTestId1",
23 "time": 1167,
24 "seeAllData": false
25 },
26 {
27 "namespace": null,
28 "name": "MyTestClass",
29 "methodName": "testMethod2",
30 "id": "N0tARealTestId2",
31 "time": 47,
32 "seeAllData": false
33 }
34 ],
35 "failures": [
36 {
37 "type": "Class",
38 "namespace": null,
39 "name": "MyTestClass",
40 "methodName": "testMethod3",
41 "message": "System.AssertException: Assertion Failed",
42 "stackTrace": "Class.MyTestClass.testMethod3: line 13, column 1",
43 "id": "01pxx0000000JTpAAM",
44 "seeAllData": false,
45 "time": 27,
46 "packageName": "MyTestClass"
47 },
48 {
49 "type": "Class",
50 "namespace": null,
51 "name": "MyTestClass",
52 "methodName": "testMethod4",
53 "message": "System.AssertException: Assertion Failed",
54 "stackTrace": "Class.MyTestClass.testMethod4: line 17, column 1",
55 "id": "01pxx0000000JTpAAM",
56 "seeAllData": false,
57 "time": 32,
58 "packageName": "MyTestClass"
59 }
60 ],
61 "totalTime": 143,
62 "apexLogId": "07Lxx0000000A9NEAU",
63 "numFailures": 2,
64 "codeCoverage": [
65
66 ],
67 "codeCoverageWarnings": [
68
69 ],
70 "numTestsRun": 4
71}
72*/
73
74// Check how many tests ran
75response["numTestRun"] === 4
76// Check how many tests passed
77response["successes"].length === 2
78
79// Return a list of objects that correspond to the tests that passed
80response["successes"]
81/*
82[
83 {
84 "id": "N0tARealTestId1",
85 "methodName": "testMethod1",
86 "name": "MyTestClass",
87 "namespace": null,
88 "seeAllData": false,
89 "time": 1167
90 }
91 ]
92*/
93
94// Access the first object in the list
95response["successes"][0]["name"] === "MyTestClass"
96response["successes"][0]["methodName"] === "testMethod1"
97// This ID refers to the classId
98response["successes"][0]["id"] === "MyTestClass"
99response["successes"][0]["time"] === 1167 // milliseconds
100
101response["failures"]
102/*
103 {
104 "type": "Class",
105 "namespace": null,
106 "name": "MyTestClass",
107 "methodName": "testMethod3",
108 "message": "System.AssertException: Assertion Failed",
109 "stackTrace": "Class.MyTestClass.testMethod3: line 13, column 1",
110 "id": "01pxx0000000JTpAAM",
111 "seeAllData": false,
112 "time": 27,
113 "packageName": "MyTestClass"
114 },
115 {
116 "type": "Class",
117 "namespace": null,
118 "name": "MyTestClass",
119 "methodName": "testMethod4",
120 "message": "System.AssertException: Assertion Failed",
121 "stackTrace": "Class.MyTestClass.testMethod4: line 17, column 1",
122 "id": "01pxx0000000JTpAAM",
123 "seeAllData": false,
124 "time": 32,
125 "packageName": "MyTestClass"
126 }
127*/
128
129response["failures"][0]["name"] === "MyTestClass"
130response["failures"][0]["methodName"] === "testMethod3"
131response["failures"][0]["message"] === "System.AssertException: Assertion Failed"
132response["failures"][0]["stackTrace"] === "Class.MyTestClass.testMethod3: line 13, column 1"
133response["failures"][0]["time"] === 27