Created
January 21, 2026 17:13
-
-
Save dcinzona/3c6e77a6a1555688b4135f8eb8aa4525 to your computer and use it in GitHub Desktop.
Tooling API APEX REST Call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RunTestsUsingToolingAPI { | |
| public static HttpResponse runAsynchronousTestsWithToolingAPI() { | |
| String endpoint = '/services/data/v65.0/tooling/runTestsAsynchronous/'; | |
| // Set up the HTTP request | |
| String urlString = endpoint; | |
| String baseUrl = URL.getOrgDomainUrl().toExternalForm(); | |
| HttpRequest request = new HttpRequest(); | |
| request.setEndpoint(baseUrl + urlString); | |
| request.setMethod('POST'); | |
| request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId()); | |
| request.setHeader('Content-Type', 'application/json'); | |
| String requestBody = '{"tests": [{"className": "FlowTesting.<test>", "testMethods": [""]},{"className": "", "testMethods": [""]}]}'; | |
| request.setBody(requestBody); | |
| // Send the request | |
| try { | |
| Http http = new Http(); | |
| HttpResponse response = http.send(request); | |
| // Check for a successful response (status code 200) | |
| if (response.getStatusCode() == 200) { | |
| String result = response.getBody(); | |
| System.debug('Test run successfully queued. AsyncApexJob ID: ' + result); | |
| } else { | |
| System.debug('Error calling runTestsAsynchronous. Status code: ' + response.getStatusCode() + ' - ' + response.getStatus()); | |
| System.debug('Response body: ' + response.getBody()); | |
| } | |
| return response; | |
| } catch (Exception e) { | |
| System.debug('Exception during Tooling API callout: ' + e.getMessage()); | |
| } | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment