How to get list of all apex classes code coverage from SOQL query?



Requirement Overview

Hi everyone , In this article we will discuss how to get list of all apex classes code coverage from SOQL query? This post will help you to ease your work and at the end you will be handy with excel where all the classes will be listed along with their code coverage. I am going to provide you the SOQL query as well as the simplest way to transfer all records to excel sheet.

Solution Approach

Once the execution of test classes from “Run All tests” is completed. Below is the SOQL query we are going to use for our requirement which will give us all the apex classes/triggers , number of lines covered and number of lines uncovered.
Note – Use Tooling API must be checked to run the below query.

SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate ORDER BY ApexClassOrTrigger.Name ASC

Now we will transfer all the records and put into the excel and just apply a formula to get the exact percentages of all classes.

Demo

  1. Go to query records fetched after performing SOQL and do inspect element in brower.
  2. Select the correct Div element by hovering into the records.
  3. Right click to the div element ->copy -> copy element and paste in excel sheet.
  4. Apply this formula to get the percentage and drag down to the bottom so that it evaluates the whole column.

    =ROUNDDOWN(B2/(C2+B2)*100,0)

Conclusion

Excel help us to get all the list of apex classes along with their code coverage in the org. This technique will definitely expedite in achieving our expected result and seems not tedious as well.

I hope you find this post useful! Catch you in the next content.
And thank you for being an awesome reader.
Share and grow together !!

If you have any doubts you can comment down below, I will try to reply ASAP.

Also keep visiting us, for more such content!

Adil Ansari

Md.Adil is a Senior Salesforce developer, a Computer Science graduate working on the Salesforce platform and providing customer solutions using force.com as a Salesforce consultant/developer. He is the founder of SalesforceBitsandBytes blogging site. He shares unique real time posts on different Salesforce implementations, the source code available on this SalesforceBitsandBytes blog is fully tested in the development environment.

Leave a Reply

Your email address will not be published. Required fields are marked *