Loading

Calculate overall code coverage in Salesforce

Publiceringsdatum: Oct 2, 2025
Beskrivning


The 'Estimate your organization's code coverage' link estimates your code coverage and isn't 100% accurate.
 

Below are ways to check the reliability of the 'Estimate your organization's code coverage' option.

Lösning

 

Follow these steps every time you run the code coverage to have reliable coverage details:

  1. Navigate to Setup
  2. In the Quick Find Search type '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 Test Data'
  7. In the Quick Find Search type 'Apex' and click 'Apex Classes'
  8. Click 'Compile all classes'
  9. In the Quick Find Search type 'Apex' and select 'Apex Test Execution'
  10. Click 'Select Tests...' then click the top checkbox to select all, then click Run

 

After the completed run, check the overall code coverage for your org by navigating to:

  1. In the Quick Find Search type 'Apex' and click 'Apex Classes'
  2. Click 'Estimate your organization's code coverage'

 

If you still lack confidence in your coverage, please use the Tooling API to determine coverage. 

We can use Tooling API to calculate the coverage individually for an Apex class/Apex trigger or overall coverage. 

  1. How many lines are covered for a specific class or trigger: 
    • SELECT NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverage WHERE ApexClassOrTriggerId = '01XXXXXXXXXXXXX'
  2. Which lines are covered for a specific class or trigger: 
    • SELECT Coverage FROM ApexCodeCoverage WHERE ApexClassOrTriggerId = '01XXXXXXXXXXXXX'
  3. The current org-wide coverage: 
    • SELECT PercentCovered FROM ApexOrgWideCoverage 
    • To test these queries you may use Workbench (workbench.developerforce.com) as follows:
      1. Navigate to Utilities | REST Explorer
      2. Select GET
      3. Enter the following URL: 
        • /services/data/v60.0/tooling/query/?q=YOUR_QUERY 
        • where YOUR_QUERY should be the query to execute
  4. Click 'Show Raw Response



If you are still not confident enough, then you can do a production validation (click 'Validate Only' button in the tool you are using to deploy) and it will show errors/warnings if there is no coverage for classes. 

Testing and Code Coverage
Code coverage best practices

Knowledge-artikelnummer

000386327

 
Laddar
Salesforce Help | Article