public class Cover{
    private static List<codecov> coverlist = new List<codecov>();
    private static List<Jsondat> jd = new List<Jsondat>(); 
    public class Jsondat
        public String ApexClassOrTriggerId; 
        public Integer NumLinesCovered; 
        public Integer NumLinesUncovered;
    public class codecov
        public String ApexClassName; 
        public Integer NumLinesCovered; 
        public Integer NumLinesUncovered;
    public void basicAuthCallout()
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        req.setHeader('Content-Type', 'application/json');
        Http h = new Http();
        HttpResponse res = h.send(req);      
        jd = (List<Jsondat>) System.JSON.deserialize(res.getBody(),Jsondat.class); 
        for(Jsondat covList : jd)
            codecov coList = new codecov();
    public static void sendEmail(String body){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        mail.setSenderDisplayName('Salesforce Admin');
        mail.setSubject('Coverage Status');
        String mailBody = body; 
        List<String> sendTo = new List<String>();  
        sendTo.add('mymailid');\\I used my mail id;
    public static String createMailBody(List<codecov> results){
        String mailBody = '<html><body>';
        mailBody = mailBody + '<b>Code Coverage Status as of '+ System.now() + '</b><br/><br/>';
        mailBody = mailBody + '<table style="width:100%" border="1">';
        mailBody = mailBody + '<tr>'+
            '<td bgcolor="Blue"><b><font color="white">Class Name</font></b></td>'+
            '<td bgcolor="Blue"><b><font color="white">NumLinesCovered</font></b></td>'+
            '<td bgcolor="Blue"><b><font color="white">NumLinesUncovered</font></b></td>'+
        for(codecov result : results){
            mailBody = mailBody + '<tr>';
            mailBody = mailBody + '<td>' + result.ApexClassName+ '</td>';
            mailBody = mailBody + '<td>' + result.NumLinesCovered + '</td>';
            mailBody = mailBody + '<td>' + result.NumLinesUncovered+ '</td>';
            mailBody = mailBody + '</tr>';
        mailBody = mailBody + '</table>';
        mailBody = mailBody + '</body></html>';
        return mailBody;

I am not getting the result to my mail.kindly help me on this code.


If you print your response:

system.debug('res.getBody(): '+res.getBody());

You will see next structure:

    "size": 2,
    "totalSize": 2,
    "done": true,
    "queryLocator": null,
    "entityTypeName": "ApexCodeCoverageAggregate",
    "records": [{
        "attributes": {
            "type": "ApexCodeCoverageAggregate",
            "url": "/services/data/v37.0/tooling/sobjects/ApexCodeCoverageAggregate/71536000001TkKmAAK"
        "ApexClassOrTriggerId": "01q36000000JeNVAA0",
        "NumLinesCovered": 0,
        "NumLinesUncovered": 6
    }, {
        "attributes": {
            "type": "ApexCodeCoverageAggregate",
            "url": "/services/data/v37.0/tooling/sobjects/ApexCodeCoverageAggregate/71536000000ND6tAAG"
        "ApexClassOrTriggerId": "01q360000004jhoAAA",
        "NumLinesCovered": 0,
        "NumLinesUncovered": 12

Result you're looking for is stored in array records, that's why json.deserialize throws error. All you need is to add class that has an array of Jsondat:

public class CoverageWrapper{
    list<Jsondat> records;

And replace your json deserialization with:

CoverageWrapper acca = (CoverageWrapper)System.JSON.deserialize(res.getBody(),CoverageWrapper.class); 

for(Jsondat covList : acca.records) {
  • Shouldn't you deserialize to List<ApexCodeCoverageAggregate>.class?
    – Adrian Larson
    Jul 18 '16 at 15:06
  • 1
    It's only accessible within tooling Api. Jul 18 '16 at 16:28
  • Ah, I see what's going on. Guess I just got tripped up on the name of your recommended class. Perhaps something along the lines of QueryData or CodeCoverageData or something that sounds less like the standard stuff would be less confusing?
    – Adrian Larson
    Jul 18 '16 at 16:31
  • 1
    You right, I picked bad name, one day back I didn't even know that such class ApexCodeCoverageAggregate exists. Jul 18 '16 at 16:33
  • Nice, I'd +1 again if I could. It'd be nice if you could just use a scheduled report to achieve the same underlying functionality. Ah well.
    – Adrian Larson
    Jul 18 '16 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.