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.


The base URI for each Tooling REST API resource is:
where domain is a Salesforce instance or a custom domain and vXX.X is the API version number.


    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
Http h = new Http();
HttpResponse response = h.send(req);

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:
  • Find for Apps in Quick Search Box.
  • Click on App Manager.
  • Click on New Connected App.
  • Enter the Name for Connected App
  • Enter your contact Email.
  • Enter the URL of your page in Callback URL.
  • Select all the scopes in Selected Auth Scopes.
  • Click on Save Button. The page will shown as below:

    Tooling api image1

2.Steps to Create An Auth. Provider:
  • From Setup, enter Auth. Providers in the Quick Find box, and then select Auth. Providers then click on New button.
  • Select Salesforce for the provider type.
  • Enter a name of the provider.
  • Enter the URL suffix, which is used in the client configuration URLs.
  • Paste the consumer key value from the connected app definition into the Consumer Key field.
  • Paste the consumer secret value from the connected app definition into the Consumer Secret field.
  • For Authorize Endpoint URL, specify an OAuth authorization URL.
  • For Token Endpoint URL, specify an OAuth token URL.
  • 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:
  • From Setup, enter Named Credentials in the Quick Find box, and then select Named Credentials| New Named Credential.
  • Enter the Name.
  • Enter URL for Named Credential.
  • Select Named Principal for Identity type.
  • Select Oauth 2.0 for Authentication Protocol.
  • Choose the Authorization Provider you created.
  • FEnter refresh_token full in the Scope field.
  • 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: