Salesforce - Tooling API

Tooling API
Tooling API exposes metadata information of Org, we can access Metadata information using REST or SOAP. Tooling API retrieve the small piece of metadata, we can use this to develop an interactive application or tools for developers. This can be used to fetch the metadata such as Apex classes, Apex triggers, custom objects, custom fields, Visualforce Pages, Users, Apex Component etc.

Tooling API provides both SOAP and REST interfaces.

Resources

The base URI for each Tooling REST API resource is:
http://domain/services/data/vXX.X/tooling/
where domain is a Salesforce instance or a custom domain and vXX.X is the API version number.
Example:- http://abctest.salesforce.com/services/data/v28.0/tooling/

Features:
  • We can accomplish following tasks using Tooling API:
    1. Fetch the metadata about Visualforce Pages, Apex Triggers, Apex classes.
    2. Get the metadata about an object’s field.
    3. Get the Code Coverage details of Apex Classes and Triggers.
    4. Retrieve metadata of custom and standard object properties.
    5. Retrieve metadata information about Validation Rules and Workflow Rules.
    6. We can Create, Update Apex Classes and Apex Triggers.
    7. We can get the RecentItems of our dev-org using Tooling API.
Implementation of Tooling API in Salesforce Classic:

First of all we need the credential for authenticating any API, if we want to make callout to Tooling API using REST, we can use salesforce session id for authentication purpose. Below are the code snippet using this we will get the session id of user org.

UserInfo.getSessionID(); // Get session id of user org.

And this session id will be used in the Authorization header of HTTP request. Like code snippet below.
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');

Here is an example of making the callout to Tooling API and gets details about Validation Rule and Workflow.

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); //Get user Session ID
req.setHeader('Content-Type', 'application/json');
String SFdomainUrl=URL.getSalesforceBaseUrl().toExternalForm();
String query='Select+id,FullName,createdDate,TableEnumOrId,ValidationName,Metadata+from+ValidationRule';
//string query='Select+id,Name+from+WorkflowRule'; //use this query to get Workflow Rule
req.setEndpoint(SFdomainUrl+'/services/data/v33.0/tooling/query/?q='+query);
req.setMethod('GET');
Http h = new Http();
HttpResponse response = h.send(req);
system.debug(response.getBody());


Implementation of Tooling API in Salesforce Lightning:

As we know we can not make the Salesforce API Call directly from the lightning component.If we use the UserInfo.getSessionID() it will return this.

INVALID_SESSION_ID:This session is not valid for use with the Rest API.

So to make Callout for this API in the Lightning Component we need to create three things:

  1. A Connected App.
  2. An Auth. Provider.
  3. A Named Credential.
1. Steps to Create the Connected App:
  • a.) Find for Apps in Quick Search Box.
  • b.) Click on App Manager.
  • c.) Click on New Connected App.
  • d.) Enter the Name for Connected App
  • e.) Enter your contact Email.
  • f.) Enter the URL of your page in Callback URL.
  • g.) Select all the scopes in Selected Auth Scopes.
  • h.) Click on Save Button. The page will shown as below:

    Tooling api image1

2.Steps to Create An Auth. Provider:
  • a.) From Setup, enter Auth. Providers in the Quick Find box, and then select Auth. Providers then click on New button.
  • b.) Select Salesforce for the provider type.
  • c.) Enter a name of the provider.
  • d.) Enter the URL suffix, which is used in the client configuration URLs.
  • e.) Paste the consumer key value from the connected app definition into the Consumer Key field.
  • f.) Paste the consumer secret value from the connected app definition into the Consumer Secret field.
  • g.) For Authorize Endpoint URL, specify an OAuth authorization URL. Ex: https://login.salesforce.com/services/oauth2/authorize.
  • h.) For Token Endpoint URL, specify an OAuth token URL.
    Ex: https://login.salesforce.com/services/oauth2/token
  • i.) After entering all the details click on save button, the page will be shown as below:

    Tooling api image2

3.Steps to Create A Named Credential:
  • a.) From Setup, enter Named Credentials in the Quick Find box, and then select Named Credentials| New Named Credential.
  • b.) Enter the Name.
  • c.) Enter URL for Named Credential.
  • d.) Select Named Principal for Identity type.
  • e.) Select Oauth 2.0 for Authentication Protocol.
  • f.) Choose the Authorization Provider you created.
  • g.) FEnter refresh_token full in the Scope field.
  • h.) Click on save button and page will seen as below after saving it.

    Tooling api image3

After completion of all the three steps, we can use the Named Credential in the ( Endpoint ) @AuraEnabled apex method to make the Salesforce API callout from the Lightning Component.

@AuraEnabled public static String Callout(){
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Test1/services/data/v35.0/tooling/query/?q=Select+id,FullName,createdDate,TableEnumOrId,ValidationName,Metadata+from+ValidationRule'); req.setMethod('GET');
req.setHeader('Content-Type', 'application/json');
HttpResponse res = h.send(req);
return res; }

For more information on this you can follow these Salesforce documentation links: