Thursday, 27 October 2016

Code Coverage Best Practices Part 2

Last time I have explained to you guys that we can use SOQL and Tooling API to get the estimated code coverage. Read About Part 1:

And now I would like to the use Tooling API to export as Excel format. 

Step1: Using the below SOQL 

Select id,ApexClassorTrigger.Name,NumLinesCovered,NumLinesUncovered from ApexCodeCoverageAggregate

Step2: Create a Apex class to make a HTTP Callout to the Tooling API Endpoint

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

You can also test it by using Salesforce workbench.
1. Login
2. Using "Get" HTTP method and using below request: 
Step3: Create a new Apex function to parse this response JSON 

public class JSON2Apex {

public class Attributes {
public String type;
public String url;

public class Records {
public Attributes attributes;
public String Id;
public ApexClassOrTrigger ApexClassOrTrigger;
public Integer NumLinesCovered;
public Integer NumLinesUncovered;

public class ApexClassOrTrigger {
public Attributes attributes;
public String Name;

public Integer size;
public Integer totalSize;
public Boolean done;
public Object queryLocator;
public String entityTypeName;
public List records;

public static JSON2Apex parse(String json) {
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);

Step4: Using ContactType in Visualforce Page

<apex:page controller="ExportTestCoverage" contentType="application/">

No comments:

Post a Comment

Back to Top

What I Can Help



Do you wanna share posts

I am always looking for writers that have something interesting to say about Salesforce. Whether you have a post in mind or would like to collaborate on one, get in touch! - Click Contact Me