Skip to main content

Fetching the list of Classes along with their code coverage using API

Tooling API
Fetching the list of Classes along with their code coverage using Tooling API

We can find the list of classes along with their code coverages from external system.
PFB code to fetch the list of classes along with their coverage details, Using Tooling API.
(Here rest call is made, same can be implemented by SOAP).
----------------------------------------------------------------------------------------------------------------------
   HTTPRequest req = new HTTPRequest();
    String myQuery=’ select+id,ApexClassOrTrigger.Name,NumLinesCovered,NumLinesUncovered+from+ApexCodeCoverageAggregate’;
    req.setEndpoint('<Login Instance> /services/data/v39.0/tooling/query/?q= '+myQuery); // Login Instance Example: https://demo727-dev-ed.my.salesforce.com
    req.setMethod('GET');
    req.setHeader('Authorization', 'Bearer ' +<Enter the Session Id>); // Example:UserInfo.getSessionId()
    Http h = new Http();
    HttpResponse res = h.send(req);
    system.debug('Res '+res.getBody()); // Contains Jsonà parse it and find the list of the classes along with their test coverage.
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
OUTPUT:

----------------------------------------------------------------------------------------------------------------------
Note: you might asked to add the remote site setting.

Comments

Post a Comment

Popular posts from this blog

Creating Remote Site Settings Dynamically

As remote site setting is essential for making callouts to external systems. We can create remote site through apex code: Steps: 1.       Add a metadataService class either through WSDL or you can use attached  file: metadataService.class 2.       Use below code: public void createRemoteSiteSetting (){     MetadataService.MetadataPort service = createService();     MetadataService.RemoteSiteSetting remoteSiteSettings = new MetadataService.RemoteSiteSetting();     remoteSiteSettings.fullName = ‘abc123';     remoteSiteSettings.url = 'http://www.clrdp727.com';     remoteSiteSettings.isActive=true;     remoteSiteSettings.disableProtocolSecurity=false;     service.createMetadata(new List<MetadataService.Metadata> { remoteSiteSettings }); } // This method returns the metadata service, using this we ...

FETCHING ACCESS TOKEN FOR EINSTEIN PLATFORM SERVICES AUTHNTICATION USING JWT

FETCHING ACCESS TOKEN FOR EINSTEIN PLATFORM SERVICES AUTHNTICATION USING JWT JSON Web Token: JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA. Although JWTs can be encrypted to also provide secrecy between parties, we will focus on signed tokens. Signed tokens can verify the integrity of the claims contained within it, while encrypted tokens hide those claims from other parties. When tokens are signed using public/private key pairs, the signature also certifies that only the party holding the private key is the one that signed it. Utility of JWT: Authorization : This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include...