Back to Collection Items
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
-- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
-- Use this online tool to generate code from sample JSON: Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- [
-- {
-- "className": "YourClassName",
-- "testMethods": [
-- "testMethod1",
-- "testMethod2",
-- "testMethod3"
-- ]
-- },
-- {
-- "maxFailedTests": "2"
-- }
-- ]
DECLARE @jarr int
-- Use "Chilkat_9_5_0.JsonArray" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @jarr OUT
EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
DECLARE @jsonObj_1 int
EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'className', 'YourClassName'
EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'testMethods[0]', 'testMethod1'
EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'testMethods[1]', 'testMethod2'
EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'testMethods[2]', 'testMethod3'
EXEC @hr = sp_OADestroy @jsonObj_1
EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'maxFailedTests', '2'
EXEC @hr = sp_OADestroy @jsonObj_1
-- Adds the "Authorization: Bearer <access_token>" header.
EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
DECLARE @sbRequestBody int
-- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRequestBody OUT
EXEC sp_OAMethod @jarr, 'EmitSb', @success OUT, @sbRequestBody
DECLARE @resp int
EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'POST', 'https://domain.com/services/data/v{{version}}/tooling/runTestsSynchronous', @sbRequestBody, 'utf-8', 'application/json', 0, 0
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jarr
EXEC @hr = sp_OADestroy @sbRequestBody
RETURN
END
DECLARE @sbResponseBody int
-- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jarr
EXEC @hr = sp_OADestroy @sbRequestBody
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
EXEC @hr = sp_OADestroy @resp
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "apexLogId": "07L4H00000SpGroUAF",
-- "codeCoverage": [
-- {
-- "id": "01q4H000000WjD8QAK",
-- "locationsNotCovered": [
-- {},
-- {}
-- ],
-- "name": "StateTrigger",
-- "namespace": null,
-- "numLocations": 2,
-- "numLocationsNotCovered": 2,
-- "type": "Trigger"
-- },
-- {
-- "id": "01q58000000RLTyAAO",
-- "locationsNotCovered": [
-- {},
-- {},
-- {}
-- ],
-- "name": "RestrictContactByName",
-- "namespace": null,
-- "numLocations": 3,
-- "numLocationsNotCovered": 3,
-- "type": "Trigger"
-- },
-- {
-- "id": "01q58000000RLBhAAO",
-- "locationsNotCovered": [
-- {},
-- {},
-- {},
-- {},
-- {},
-- {}
-- ],
-- "name": "ClosedOpportunityTrigger",
-- "namespace": null,
-- "numLocations": 6,
-- "numLocationsNotCovered": 6,
-- "type": "Trigger"
-- },
-- {
-- "id": "01p4H000009PEFIQA4",
-- "locationsNotCovered": [
-- {},
-- {}
-- ],
-- "name": "PrepareSandbox",
-- "namespace": null,
-- "numLocations": 22,
-- "numLocationsNotCovered": 2,
-- "type": "Class"
-- }
-- ],
-- "codeCoverageWarnings": [
-- {
-- "id": "01q4H000000WjD8QAK",
-- "message": "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required",
-- "name": "StateTrigger",
-- "namespace": null
-- },
-- {
-- "id": "01q58000000RLTyAAO",
-- "message": "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required",
-- "name": "RestrictContactByName",
-- "namespace": null
-- },
-- {
-- "id": "01q58000000RLBhAAO",
-- "message": "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required",
-- "name": "ClosedOpportunityTrigger",
-- "namespace": null
-- },
-- {
-- "id": "01q4H000000WjD8QAK",
-- "message": "Average test coverage across all Apex Classes and Triggers is 60%, at least 75% test coverage is required.",
-- "name": null,
-- "namespace": ""
-- }
-- ],
-- "failures": [
-- ],
-- "flowCoverage": [
-- ],
-- "flowCoverageWarnings": [
-- ],
-- "numFailures": 0,
-- "numTestsRun": 1,
-- "successes": [
-- {
-- "id": "01p4H00000A0zYxQAJ",
-- "methodName": "runApexClass_should_work",
-- "name": "PrepareSandboxTest",
-- "namespace": null,
-- "seeAllData": false,
-- "time": 1088
-- }
-- ],
-- "totalTime": 1261
-- }
-- Sample code for parsing the JSON response...
-- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
DECLARE @id nvarchar(4000)
DECLARE @name nvarchar(4000)
DECLARE @namespace nvarchar(4000)
DECLARE @numLocations int
DECLARE @numLocationsNotCovered int
DECLARE @v_type nvarchar(4000)
DECLARE @j int
DECLARE @count_j int
DECLARE @message nvarchar(4000)
DECLARE @methodName nvarchar(4000)
DECLARE @seeAllData int
DECLARE @time int
DECLARE @apexLogId nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @apexLogId OUT, 'apexLogId'
DECLARE @numFailures int
EXEC sp_OAMethod @jResp, 'IntOf', @numFailures OUT, 'numFailures'
DECLARE @numTestsRun int
EXEC sp_OAMethod @jResp, 'IntOf', @numTestsRun OUT, 'numTestsRun'
DECLARE @totalTime int
EXEC sp_OAMethod @jResp, 'IntOf', @totalTime OUT, 'totalTime'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'codeCoverage'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'codeCoverage[i].id'
EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'codeCoverage[i].name'
EXEC sp_OAMethod @jResp, 'StringOf', @namespace OUT, 'codeCoverage[i].namespace'
EXEC sp_OAMethod @jResp, 'IntOf', @numLocations OUT, 'codeCoverage[i].numLocations'
EXEC sp_OAMethod @jResp, 'IntOf', @numLocationsNotCovered OUT, 'codeCoverage[i].numLocationsNotCovered'
EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'codeCoverage[i].type'
SELECT @j = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'codeCoverage[i].locationsNotCovered'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @jResp, 'J', @j
SELECT @j = @j + 1
END
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'codeCoverageWarnings'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'codeCoverageWarnings[i].id'
EXEC sp_OAMethod @jResp, 'StringOf', @message OUT, 'codeCoverageWarnings[i].message'
EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'codeCoverageWarnings[i].name'
EXEC sp_OAMethod @jResp, 'StringOf', @namespace OUT, 'codeCoverageWarnings[i].namespace'
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'failures'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'flowCoverage'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'flowCoverageWarnings'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'successes'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'successes[i].id'
EXEC sp_OAMethod @jResp, 'StringOf', @methodName OUT, 'successes[i].methodName'
EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'successes[i].name'
EXEC sp_OAMethod @jResp, 'StringOf', @namespace OUT, 'successes[i].namespace'
EXEC sp_OAMethod @jResp, 'BoolOf', @seeAllData OUT, 'successes[i].seeAllData'
EXEC sp_OAMethod @jResp, 'IntOf', @time OUT, 'successes[i].time'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jarr
EXEC @hr = sp_OADestroy @sbRequestBody
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-H "Content-Type: application/json"
-d '[
{
"className": "YourClassName",
"testMethods": [
"testMethod1",
"testMethod2",
"testMethod3"
]
},
{
"maxFailedTests": "2"
}
]
or
{
"tests": [
{
"className": "YourClassName",
"testMethods": [
"testMethod1",
"testMethod2",
"testMethod3"
]
}
],
"maxFailedTests": "integer value",
"testLevel": "TestLevel enum value",
"skipCodeCoverage": "boolean value"
}'
https://domain.com/services/data/v{{version}}/tooling/runTestsSynchronous
Postman Collection Item JSON
{
"name": "Tooling Run Tests Sync",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "[\n {\n \"className\": \"YourClassName\",\n \"testMethods\": [\n \"testMethod1\",\n \"testMethod2\",\n \"testMethod3\"\n ]\n },\n {\n \"maxFailedTests\": \"2\"\n }\n]\n\nor\n\n{\n \"tests\": [\n {\n \"className\": \"YourClassName\",\n \"testMethods\": [\n \"testMethod1\",\n \"testMethod2\",\n \"testMethod3\"\n ]\n }\n ],\n \"maxFailedTests\": \"integer value\",\n \"testLevel\": \"TestLevel enum value\",\n \"skipCodeCoverage\": \"boolean value\"\n}\n",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/tooling/runTestsSynchronous",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"tooling",
"runTestsSynchronous"
]
},
"description": "Runs one or more methods within one or more Apex classes, using the asynchronous test execution mechanism. In the request body, you can specify test class names and IDs, suite names and IDs, the maximum number of failed tests to allow, and the test level, as comma-separated lists or as an array. You can also indicate whether to opt out of collecting code coverage information during the test run (available in API version 43.0 and later)."
},
"response": [
{
"name": "Tooling Run Tests Sync",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"tests\": [\n {\n \"className\": \"PrepareSandboxTest\",\n \"testMethods\": [\n \"runApexClass_should_work\"\n ]\n }\n ],\n \"maxFailedTests\": \"1\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/tooling/runTestsSynchronous",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"tooling",
"runTestsSynchronous"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Mon, 11 Dec 2023 09:59:37 GMT"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=63072000; includeSubDomains"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-Robots-Tag",
"value": "none"
},
{
"key": "Cache-Control",
"value": "no-cache,must-revalidate,max-age=0,no-store,private"
},
{
"key": "Sforce-Limit-Info",
"value": "api-usage=246/15000"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Vary",
"value": "Accept-Encoding"
},
{
"key": "Content-Encoding",
"value": "gzip"
},
{
"key": "Transfer-Encoding",
"value": "chunked"
}
],
"cookie": [
],
"body": "{\n \"apexLogId\": \"07L4H00000SpGroUAF\",\n \"codeCoverage\": [\n {\n \"id\": \"01q4H000000WjD8QAK\",\n \"locationsNotCovered\": [\n {},\n {}\n ],\n \"name\": \"StateTrigger\",\n \"namespace\": null,\n \"numLocations\": 2,\n \"numLocationsNotCovered\": 2,\n \"type\": \"Trigger\"\n },\n {\n \"id\": \"01q58000000RLTyAAO\",\n \"locationsNotCovered\": [\n {},\n {},\n {}\n ],\n \"name\": \"RestrictContactByName\",\n \"namespace\": null,\n \"numLocations\": 3,\n \"numLocationsNotCovered\": 3,\n \"type\": \"Trigger\"\n },\n {\n \"id\": \"01q58000000RLBhAAO\",\n \"locationsNotCovered\": [\n {},\n {},\n {},\n {},\n {},\n {}\n ],\n \"name\": \"ClosedOpportunityTrigger\",\n \"namespace\": null,\n \"numLocations\": 6,\n \"numLocationsNotCovered\": 6,\n \"type\": \"Trigger\"\n },\n {\n \"id\": \"01p4H000009PEFIQA4\",\n \"locationsNotCovered\": [\n {},\n {}\n ],\n \"name\": \"PrepareSandbox\",\n \"namespace\": null,\n \"numLocations\": 22,\n \"numLocationsNotCovered\": 2,\n \"type\": \"Class\"\n }\n ],\n \"codeCoverageWarnings\": [\n {\n \"id\": \"01q4H000000WjD8QAK\",\n \"message\": \"Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required\",\n \"name\": \"StateTrigger\",\n \"namespace\": null\n },\n {\n \"id\": \"01q58000000RLTyAAO\",\n \"message\": \"Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required\",\n \"name\": \"RestrictContactByName\",\n \"namespace\": null\n },\n {\n \"id\": \"01q58000000RLBhAAO\",\n \"message\": \"Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required\",\n \"name\": \"ClosedOpportunityTrigger\",\n \"namespace\": null\n },\n {\n \"id\": \"01q4H000000WjD8QAK\",\n \"message\": \"Average test coverage across all Apex Classes and Triggers is 60%, at least 75% test coverage is required.\",\n \"name\": null,\n \"namespace\": \"\"\n }\n ],\n \"failures\": [],\n \"flowCoverage\": [],\n \"flowCoverageWarnings\": [],\n \"numFailures\": 0,\n \"numTestsRun\": 1,\n \"successes\": [\n {\n \"id\": \"01p4H00000A0zYxQAJ\",\n \"methodName\": \"runApexClass_should_work\",\n \"name\": \"PrepareSandboxTest\",\n \"namespace\": null,\n \"seeAllData\": false,\n \"time\": 1088\n }\n ],\n \"totalTime\": 1261\n}"
}
]
}