Salesforce Apex Code Repository

Salesforce provides couple of developer related objects which are very useful when a developer wants to fetch the metadata information of apex class, components, pages, triggers, code coverage using apex code. There are lot of scenarios where we can utilize these objects :-

  1. To search any referenced fields and objects inside apex classes,pages & triggers.
  2. To Create an Apex Class, Apex Triggers, Apex Pages via API.
  3. To find out the package related classes or triggers.
  4. To check the number of test classes and running test results.
  5. To check the coverage of classes and triggers.

We have the following objects which represent Apex Code Repository:-

  1. ApexClass
  2. ApexComponent
  3. ApexPage
  4. ApexTrigger
  5. ApexTestResult
  6. ApexCodeCoverage
  7. ApexCodeCoverageAggregate

ApexClass

This object represents apex class.

For e.g. A Developer wants to show the list of classes related to a package called “APTTUS” , we can use a field NameSpacePrefix to filter out the classes related to package using below SOQL:

Select Name From ApexClass Where NameSpacePrefix = 'APTTUS'         

For more information about ApexClass object.

ApexTrigger

This object represents Apex Triggers.

For e.g. A Developer wants to show the list of Apex Triggers on the Account object. We can use a field called TableEnumOrId with passing the Object Name and below SOQL will return all the triggers written on Account object.

Select Name From ApexTrigger Where TableEnumOrId = 'Account'         

For more information about ApexTrigger object.

ApexPage

This object represents a Visualforce Page.

For e.g. A Developer wants to show the list of Visualforce Pages defined with the standardController attribute on the <apex:page> tag

Select Name From ApexPage Where ControllerType = 'Standard'   

For more information about ApexPage object.

ApexTestResult

This object represents the Test Results produced by apex tests. Each test method execution is represented by a single ApexTestResult record.

For e.g. A Developer wants to show the list of apex test results ran for an apex class “XYZ”. Here XYZ is the ID of Apex Class. Below SOQL will return the test results for the same:

Select Name From ApexTestResult Where ApexClassId = 'XYZ'   

For more information about ApexTestResult object.

ApexCodeCoverage

This object represents code coverage test results for an Apex class or trigger. This object is accessible via Tooling API only. This object is specifically used to design a statistic report for any apex class or trigger.

This object needs a REST Call to access the data stored into this object. Below is the sample code to get the data using REST call:


HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(wrapObj.instance_url+'/services/data/v39.0/tooling/query/?q='+SELECT+id,ApexTestClass.Name,TestMethodName,NumLinesCovered,NumLinesUncovered,ApexClassorTrigger.Name,ApexClassorTrigger.Type,Coverage+from+ApexCodeCoverage+where+ApexClassorTriggerId);
req.setHeader('Authorization', 'OAuth '+wrapObj.access_token);
req.setTimeout(120000);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('***Response Body****'+ res.getBody());
return res.getBody();

For more information about ApexCodeCoverage object.

ApexCodeCoverageAggregate

This object represents aggregate code coverage for a  Class or Trigger. For E.g. :-  We want to see the coverage for a specific class covered by all test methods, so we can use this object in our REST call. 

In Summary, Salesforce provides the flexibility to play around with the metadata of developer objects in Apex Code.

Author:

Ajomon Joseph AJOMON JOSEPH
Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog https://apexcoder.com
email Email me ajomon@apexcoder.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s