How to find number of lines covered and uncovered in Apex class using SOQL?

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

These objects like

a) ApexCodeCoverage contains coverage per class per test class
b) ApexCodeCoverageAggregate contains coverage per class for all test classes
c) ApexOrgWideCoverage contains coverage overall for all classes in the org

available from Tooling API.. These are not the regular Sobjects to get the data via SOQL using APEX…

So you need to send an request to Tooling API to get the information what ever you want from APEX. Below is the sample code that I used to get the custom object ID via Tooling API. Hope it makes sense.
String objectIdQuery = \’select Id from CustomObject where DeveloperName = \\\’\’ + objectName + \’\\\’\’;
String environmentURL = URL.getSalesforceBaseUrl().toExternalForm() + \’/services/data/v28.0/tooling/query/?q=\’ + EncodingUtil.urlEncode(objectIdQuery, \’UTF-8\’);

HttpRequest req = new HttpRequest();

req.setHeader(\’Authorization\’, \’Bearer \’ + UserInfo.getSessionID());

req.setHeader(\’Content-Type\’, \’application/json\’);

req.setEndpoint(environmentURL);

req.setMethod(\’GET\’);

Http h = new Http();

return h.send(req).getBody();

Cheers!!!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam! Please check your mail for confirmation.

Leave a Comment

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

Select Language »