Chilkat Online Tools

SQL Server / Salesforce Platform APIs / Tooling Search

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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @queryParams int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @queryParams OUT

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'q', ''
    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, '[    {        "className": "YourClassName",        "testMethods": [            "testMethod1",            "testMethod2",            "testMethod3"        ]    },    {        "maxFailedTests": "2"    }]or[    {        "classId": "01pD0000000Fhy9IAC",        "testMethods": [            "testMethod1",            "testMethod2",            "testMethod3"        ]    },    {        "maxFailedTests": "2"    }]', ''

    -- 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 @resp int
    EXEC sp_OAMethod @http, 'QuickRequestParams', @resp OUT, 'GET', 'https://domain.com/services/data/v{{version}}/tooling/search', @queryParams
    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 @queryParams
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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 @queryParams
        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)

    -- {
    --   "searchRecords": [
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqcAAA"
    --       },
    --       "Name": "LightningSelfRegisterController"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqhAAA"
    --       },
    --       "Name": "LightningSelfRegisterControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wq9AAA"
    --       },
    --       "Name": "SiteRegisterControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p4H00000A0zYxQAJ"
    --       },
    --       "Name": "PrepareSandboxTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqTAAQ"
    --       },
    --       "Name": "CommunitiesSelfRegControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p4H000009aIwvQAE"
    --       },
    --       "Name": "MicrobatchSelfRegControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p4H000009PEFIQA4"
    --       },
    --       "Name": "PrepareSandbox"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqRAAQ"
    --       },
    --       "Name": "CommunitiesLandingControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqFAAQ"
    --       },
    --       "Name": "MyProfilePageControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqDAAQ"
    --       },
    --       "Name": "ForgotPasswordControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wq7AAA"
    --       },
    --       "Name": "SiteLoginControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p4H00000ANdnqQAD"
    --       },
    --       "Name": "AccountTriggerHelperTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqgAAA"
    --       },
    --       "Name": "LightningForgotPasswordControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqfAAA"
    --       },
    --       "Name": "LightningLoginFormControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqeAAA"
    --       },
    --       "Name": "LightningLoginFormController"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqVAAQ"
    --       },
    --       "Name": "CommunitiesSelfRegConfirmControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqPAAQ"
    --       },
    --       "Name": "CommunitiesLoginControllerTest"
    --     },
    --     {
    --       "attributes": {
    --         "type": "ApexClass",
    --         "url": "/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqBAAQ"
    --       },
    --       "Name": "ChangePasswordControllerTest"
    --     }
    --   ]
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @v_Type nvarchar(4000)

    DECLARE @v_Url nvarchar(4000)

    DECLARE @Name nvarchar(4000)

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'searchRecords'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'searchRecords[i].attributes.type'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_Url OUT, 'searchRecords[i].attributes.url'
        EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'searchRecords[i].Name'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @queryParams
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -G -d "q="
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '[
    {
        "className": "YourClassName",
        "testMethods": [
            "testMethod1",
            "testMethod2",
            "testMethod3"
        ]
    },
    {
        "maxFailedTests": "2"
    }
]

or

[
    {
        "classId": "01pD0000000Fhy9IAC",
        "testMethods": [
            "testMethod1",
            "testMethod2",
            "testMethod3"
        ]
    },
    {
        "maxFailedTests": "2"
    }
]'
https://domain.com/services/data/v{{version}}/tooling/search

Postman Collection Item JSON

{
  "name": "Tooling Search",
  "protocolProfileBehavior": {
    "disableBodyPruning": true
  },
  "request": {
    "method": "GET",
    "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    {\n        \"classId\": \"01pD0000000Fhy9IAC\",\n        \"testMethods\": [\n            \"testMethod1\",\n            \"testMethod2\",\n            \"testMethod3\"\n        ]\n    },\n    {\n        \"maxFailedTests\": \"2\"\n    }\n]",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/tooling/search?q=",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "tooling",
        "search"
      ],
      "query": [
        {
          "key": "q",
          "value": "",
          "description": "SOSL search statement"
        }
      ]
    },
    "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 Search",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Content-Type",
            "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    {\n        \"classId\": \"01pD0000000Fhy9IAC\",\n        \"testMethods\": [\n            \"testMethod1\",\n            \"testMethod2\",\n            \"testMethod3\"\n        ]\n    },\n    {\n        \"maxFailedTests\": \"2\"\n    }\n]",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{_endpoint}}/services/data/v{{version}}/tooling/search?q=FIND {test*} IN ALL FIELDS RETURNING ApexClass (Name)",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "data",
            "v{{version}}",
            "tooling",
            "search"
          ],
          "query": [
            {
              "key": "q",
              "value": "FIND {test*} IN ALL FIELDS RETURNING ApexClass (Name)",
              "description": "SOSL_Search_Statement"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 11 Dec 2023 10:34:10 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=294/15000"
        },
        {
          "key": "queryId",
          "value": "-mxbcxdoqukvy"
        },
        {
          "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    \"searchRecords\": [\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqcAAA\"\n            },\n            \"Name\": \"LightningSelfRegisterController\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqhAAA\"\n            },\n            \"Name\": \"LightningSelfRegisterControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wq9AAA\"\n            },\n            \"Name\": \"SiteRegisterControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p4H00000A0zYxQAJ\"\n            },\n            \"Name\": \"PrepareSandboxTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqTAAQ\"\n            },\n            \"Name\": \"CommunitiesSelfRegControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p4H000009aIwvQAE\"\n            },\n            \"Name\": \"MicrobatchSelfRegControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p4H000009PEFIQA4\"\n            },\n            \"Name\": \"PrepareSandbox\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqRAAQ\"\n            },\n            \"Name\": \"CommunitiesLandingControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqFAAQ\"\n            },\n            \"Name\": \"MyProfilePageControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqDAAQ\"\n            },\n            \"Name\": \"ForgotPasswordControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wq7AAA\"\n            },\n            \"Name\": \"SiteLoginControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p4H00000ANdnqQAD\"\n            },\n            \"Name\": \"AccountTriggerHelperTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqgAAA\"\n            },\n            \"Name\": \"LightningForgotPasswordControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqfAAA\"\n            },\n            \"Name\": \"LightningLoginFormControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqeAAA\"\n            },\n            \"Name\": \"LightningLoginFormController\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqVAAQ\"\n            },\n            \"Name\": \"CommunitiesSelfRegConfirmControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqPAAQ\"\n            },\n            \"Name\": \"CommunitiesLoginControllerTest\"\n        },\n        {\n            \"attributes\": {\n                \"type\": \"ApexClass\",\n                \"url\": \"/services/data/v59.0/tooling/sobjects/ApexClass/01p580000090wqBAAQ\"\n            },\n            \"Name\": \"ChangePasswordControllerTest\"\n        }\n    ]\n}"
    }
  ]
}