function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VenkataRajaVenkataRaja 

Query on ApexCodeCoverageAggregate.

Hi All,

I have to query on ApexCodeCoverageAggregate. I tried like this, but not able to get the result, is there any one have sample codes on this can you please post it. I need how use these Tooling API Objects. Please post with sample class and procedure how to execute these.

I tried like below.

public class SampleClass
{
      public List<ApexCodeCoverageAggregate> getCoverage()
      {
                 List<ApexCodeCoverageAggregate> apexCodeCoverageList = [SELECT NumLinesCovered, NumLinesUncovered, ApexClassorTriggerId FROM ApexCodeCoverageAggregate];
                 System.debug('@@@:apexCodeCoverageList:  '+apexCodeCoverageList);
                 return apexCodeCoverageList;
      }
This code is not working.

Thanks
Venkat


Best Answer chosen by VenkataRaja
kiranmutturukiranmutturu
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();

All Answers

kiranmutturukiranmutturu
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();

This was selected as the best answer
VenkataRajaVenkataRaja
Hi Kiran,

Thanks!

I have modified code like this. I am getting system callout exception on RemoteSite Settings. Can you please send me full steps and I have to send email thse class names and code coverage. Can I store the reuslt in list.

String objectIdQuery = 'SELECT ApexClassorTriggerId, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate';

  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();

Thanks & Regards
Venkat

kiranmutturukiranmutturu
You need to add the instance URl to remote site settings.. But as I told you I gave a vrief idea about what you can do, but the above code is not the final one.
VenkataRajaVenkataRaja
Hi Kiran,

I am calling this method in @future(Callout=true), UserInfo.getSessionID() getting null. Can you please post for this any alternate.

Thanks 
Venkat
kiranmutturukiranmutturu
callouts are asynchronous, so it doesn't contain any sessionId 
VenkataRajaVenkataRaja
Hi Kiran,

I am Calling future method from schedule apex, getting an error: System.HttpResponse[Status=Unauthorized, StatusCode=401]
I think we are getting because of UserInfo.getSessionId() null, If I comment hilighted line also I am getting same error. Is there any solution for this. or Can I schedule the callouts?

public class Sample
{
      @future(Callout = true)
      public static void getCoverageforClasses()
      {
           String objectIdQuery = 'SELECT ApexClassorTriggerId, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate';

           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();              
      } 
}

Thanks 
Venkat