DEV Community

Cover image for How to Query Salesforce Code Coverage
Matheus Goncalves
Matheus Goncalves

Posted on • Edited on

How to Query Salesforce Code Coverage

In Salesforce Orgs, code coverage percentage is a calculation of the number of covered lines divided by the sum of the number of covered lines and uncovered lines.

In addition to ensuring the quality of your code, unit tests enable you to meet the code coverage requirements for deploying or packaging Apex. To deploy Apex or package it for the Salesforce AppExchange, unit tests must cover at least 75% of your Apex code, and those tests must pass.

After running tests, you can view code coverage information in the Tests tab of the Developer Console. The code coverage pane includes coverage information for each Apex class and the overall coverage for all Apex code in your organization.

However, you can also use SOQL queries with Tooling API as an alternative way of checking code coverage and a quick way to get more details. Let's see how it works:

Inspecting Code Coverage

Step 1 - Get your org's base URL:

If you know what's your Org's base URL (you can get it from your browser as well), please jump to step 2.

  • From Developer Console, open the Debug menu, then select the option "Open Execute Anonymous Window (CTRL+E)
  • Enter the following Apex Code:

    String baseURL =  'https://' + System.URL.getSalesforceBaseUrl().getHost();
    System.debug(baseURL);

Enter fullscreen mode Exit fullscreen mode
  • Click the button [Execute]
  • Go to your Logs tab on the bottom of the page
  • Copy the full Base URL, including https://

Step 2 - Now you know your base URL, create a Remote Site Setting

  • From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.
  • Click New Remote Site.
  • Enter a descriptive term for the Remote Site Name.
  • Enter the URL for the remote site.
  • Optionally, enter a description of the site.

  • Click Save.
  • Just in case, create a second Remote Site Settings for the URL in the "Visual.force.com" format:
    • https://c.xx99.visual.force.com - where xx99 is the code of your org's instance.

Step 3 - Get the JSON file for the Code Coverage Wrapper

  • Go back to the Developer Console, open the Debug menu, then select the option "Open Execute Anonymous Window (CTRL+E)
  • Enter the following Apex Code:
  • Click the button [Execute]
  • Go to your Logs tab on the bottom of the page
  • Copy the full JSON result
    • Please note you will need to remove any text before the first "{" bracket.
    • Eg.: "hh:mm:ss:999 USER_DEBUG [99]|DEBUG|"
  • Go to the website JSON2Apex: https://json2apex.herokuapp.com/
  • Paste your JSON file
  • Enter the name for the generated class (here, we will use CodeCoverageWrapper)
  • Click the button [Create Apex]

  • Download the generated zip file that contains the Wrapper class plus the Test class.
  • Extract the files
  • Open the files with your prefered text editor
  • Check if everything looks good, and add these classes to your Salesforce org as new classes.

This is how it looks like:

Step 4 - Create a Helper Class

  • Create the following class, that contains a method that will query and return information about the Code Coverage

Don't forget to write the test class for it.

Step 5 - Create a Controller and a Page to display the Code Coverage

  • Create the following class, that contains the logic for the page

Create the following page, using the controller we just created.

Please note we're leveraging Salesforce Lightning Design System;

The page has two buttons. One to sort the results by Class / Trigger name, and another button to sort the results by Code Coverage.

This is how it looks like:

Final Considerations

You can follow the same logic to use the Tooling API whenever you need to query exposed metadata used in developer tooling that you can access through REST or SOAP.

Also, the method sortCodeCoverageMapByCoverage from CodeCoverageHelper is quite handy and can be used as an example for situations when you need to sort Maps by its values. In this case, sort the Code Coverage percentage value.

Just don't forget to adapt the Wrapper and the Comparable.

You can find all code used for this project in my GitHub account, at github.com/gitmatheus/QueryCodeCoverage

GitHub logo gitmatheus / QueryCodeCoverage

How to Query Code Coverage - After running tests, you can view code coverage information in the Tests tab of the Developer Console. The code coverage pane includes coverage information for each Apex class and the overall coverage for all Apex code in your organization. However, you can also use SOQL queries with Tooling API as an alternative way of checking code coverage and a quick way to get more details.

How to Query Code Coverage

In Salesforce Orgs, code coverage percentage is a calculation of the number of covered lines divided by the sum of the number of covered lines and uncovered lines. In addition to ensuring the quality of your code, unit tests enable you to meet the code coverage requirements for deploying or packaging Apex.

To deploy Apex or package it for the Salesforce AppExchange, unit tests must cover at least 75% of your Apex code, and those tests must pass. After running tests, you can view code coverage information in the Tests tab of the Developer Console. The code coverage pane includes coverage information for each Apex class and the overall coverage for all Apex code in your organization. However, you can also use SOQL queries with Tooling API as an alternative way of checking code coverage and a quick way to get more details.

The code…

I hope you enjoyed the ride and learned something new today.

See you next time.

This article was originally posted at matheus.dev

Top comments (0)