Calculate Overall Code coverage in Salesforce

Calculate Overall Code coverage in Salesforce

Last Updated on May 21, 2023 by Rakesh Gupta

Big Idea or Enduring Question:

  • How to calculate overall code coverage in Salesforce? 


After reading this blog, you’ll be able to:

  • Compile all test classes
  • Estimate your organization’s code coverage
  • Use SOQL via Tooling API to calculate code coverage
  • and much more

Business Use case

Isabella Stewart is working as a Salesforce Administrator at Gurukul On Cloud (GoC). GoC is working with a third-party vendor to implement Field Service Lightning to improve customer experience. Isabella got a task from her manager to calculate the code coverage in PROD.

Guided Practice (We-do):

Code coverage is a testing technique that determines what code is being tested and what is not tested. It is often represented as a percentage of the number of lines of code tested for an Apex class or trigger.

Perform the steps below to get reliable code coverage:

Step 1: Compile All Test Classes

  1. Navigate to Setup | Custom Code | Apex Test Execution.
  2. Uncheck Store Only Aggregated Code Coverage.
  3. Then, clear test history, by navigating to View Test History | Clear Test Data.
  4. The last steps is to compile all test classes. By navigate to Setup | Custom Code | Apex Classes.

Step 2: Check Overall Code Coverage

Once the Apex unit tests are completed, check the overall code coverage for your org by following the instructions below:

  1. Navigate to Setup | Custom Code | Apex Classes.
  2. Click on Estimate your organization’s code coverage.

You can also run the below query to find out org-wide coverage. If querying from the Developer Console Query Editor, ensure you select Use Tooling API.

ApexOrgWideCoverage represents code coverage test results for an entire organization.

SELECT PercentCovered FROM ApexOrgWideCoverage

The following example SOQL query retrieves code coverage results for a specific class or trigger:

SELECT TestMethodName, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverage WHERE ApexClassOrTrigger.Name='AccountTrigger'

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.