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 :-
- To search any referenced fields and objects inside apex classes,pages & triggers.
- To Create an Apex Class, Apex Triggers, Apex Pages via API.
- To find out the package related classes or triggers.
- To check the number of test classes and running test results.
- To check the coverage of classes and triggers.
We have the following objects which represent Apex Code Repository:-
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'
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'
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'
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'
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.setHeader('Authorization', 'OAuth '+wrapObj.access_token);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('***Response Body****'+ res.getBody());
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.
Senior Salesforce Architect
Connect me on LinkedIn
Follow me on Twitter @apexcoder
Subscribe to my blog https://apexcoder.com
Email me email@example.com