Loading

How to Calculate Overall Apex Code Coverage in Salesforce

Publiceringsdatum: Jun 2, 2026
Beskrivning

The "Estimate your organization's code coverage" link in Salesforce Setup provides an estimate of your Apex code coverage but is not 100% precise. This article explains how to obtain a reliable, accurate code coverage calculation for your Salesforce org, using both the Apex Test Execution tool and the Tooling API.
Apex code coverage is a measure of how many lines of your Apex code are executed by your test classes. Salesforce requires at least 75% overall code coverage to deploy Apex to production.

Lösning

Step 1: Run All Tests to Get a Reliable Coverage Result

Follow these steps each time you want to obtain an accurate code coverage result for your org:

  1. Navigate to Setup.
  2. In the Quick Find box, search for Apex and select Apex Test Execution.
  3. Click Options.
  4. Deselect "Store Only Aggregated Code Coverage" and click OK.
  5. Click View Test History.
  6. Click Clear All Test History.
  7. In the Quick Find box, search for Apex and click Apex Classes.
  8. Click Compile All Classes.
  9. In the Quick Find box, search for Apex and select Apex Test Execution.
  10. Click Run All Tests.

Step 2: Check Overall Org Code Coverage

After all tests complete, check your org's overall code coverage:

  1. In the Quick Find box, search for Apex and click Apex Classes.
  2. Click Estimate Your Organization's Code Coverage.

Step 3: Use the Tooling API for Precise Coverage Data

If you need more precise or programmatic coverage data, use the Salesforce Tooling API to query coverage at the individual class or trigger level, or for the entire org.
The Tooling API is a REST-based API that provides access to metadata and debugging information in Salesforce, including Apex code coverage data.
Query 1 — Number of lines covered for a specific Apex class or trigger: Query the ApexCodeCoverage object filtering by ApexClassOrTriggerId. Replace the ID with your 15 or 18-character Apex class or trigger ID. The result returns NumLinesCovered and NumLinesUncovered fields.
Query 2 — Which specific lines are covered for a class or trigger: Query the ApexCodeCoverage object for the Coverage field, filtering by ApexClassOrTriggerId. Replace the ID with your actual class or trigger ID.
Query 3 — Current org-wide code coverage percentage: Query the ApexOrgWideCoverage object and retrieve the PercentCovered field. This returns the overall code coverage percentage for your entire Salesforce org.
Running these queries in Workbench: You can test these Tooling API queries using Workbench (workbench.developerforce.com). Navigate to Utilities, then REST Explorer. Select GET, enter the URL /services/data/v29.0/tooling/query/?q=YOUR_QUERY replacing YOUR_QUERY with the SOQL statement, and click Show Raw Response.

Step 4: Validate Against Production

If you still have concerns about your coverage, perform a production validation by clicking the Validate Only button in your deployment tool. The tool displays errors or warnings if any classes have insufficient coverage.
 

Knowledge-artikelnummer

000386327

 
Laddar
Salesforce Help | Article