Skip to main content

Salesforce Tooling Api

Hello Eveyone, Today I'll be focusing on Salesforce Tooling API and explaining how to integrate with a .NET console application. Here I'm using console application because I need to keep it simple. Tooling Api is available for SOAP and REST. In this post I'm focusing on SOAP  which will be creating an apex class from the api.
What can do from Salesforce Tooling API
  • Deploy Apex, Triggers and Visualforce Pages
  • Manage validation rules, static resources, workflow rules
  • Manage custom fields on custom objects
  • Execute tests synchronously or asynchronously
  • Org wide and class specific code coverage
  • Execute anonymous Apex
  • Access debug logs
Tooling API is specifically designed to help developers build IDE apps for Salesforce, while the metadata API is more general purpose configuration migration. Tooling API will support for wide range of objects and you can find the list of objects from here.
Tooling API vs Metada API
Technically, the Tooling API could completely replace the metadata API, since the metadata API, came first and is less powerful, but it will be supported for some time to come yet. I have written an article about the Metadata api and you will be able to find it from here.
New projects should probably use the Tooling API as much as practical, but the metadata API is widely supported, in comparison, so it will be a while yet until it is completely replaced.
Step 1 : Get the url of the Tooling API  wsdl url from Salesforce
You need to login to Salesforce and got to Setup -> Api -> Tooling WSDL -> Generate Tooling WSDL .  Once you are clicking on "Generate Tooling WSDL" you will be able to see the WSDL file for the tooling api. Just copy that url keep it for the moment after later we'll use the url.
1
Step 2 : Create a console application to integrate Salesforce via tooling api
Our next step would be create a console application using visual studio. For my case I'm using visual studio community 2017 and I'm targeting .NET framework 4.5.
2
Step 3 : Get the session Id from Salesforce Rest API
In order to communicate with Salesforce from Tooling api you need a session Id. From the tooling api itself there is no login method to retrieve session id. This is same as Salesforce other apis except Enterprise/Partner and Rest API. So I'm using Salesforce REST api to get the session id. You can find my earlier post that will explaining about the session id retrieve from .net application. You can find it from here.  Therefore I will just mention the related classes below and if you need any further details you can refer to the above post.
Here I have created Auth and Constants class. Auth class has GetAuthResponse method which will returning session id as a wrapper class. I have created the wrapper class as AuthResponse in the same file. The Constants class will containing the authentication parameters.
Program Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ToolingAPI
{
class Program
{
static void Main(string[] args)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Task authResponse = Task.Run(() => Auth.GetAuthResponse());
authResponse.Wait();
if (authResponse.Result != null)
{
}
}
}

}

Auth Class
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace ToolingAPI
{
public class Auth
{
public async static Task GetAuthResponse()
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("client_id", Constants.CONSUMER_KEY),
new KeyValuePair<string, string>("client_secret", Constants.CONSUMER_SECRET),
new KeyValuePair<string, string>("username", Constants.USERNAME),
new KeyValuePair<string, string>("password", Constants.PASSWORD + Constants.TOKEN)
});
HttpClient _httpClient = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(Constants.TOKEN_REQUEST_ENDPOINTURL),
Content = content
};
var responseMessage = await _httpClient.SendAsync(request).ConfigureAwait(false);
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
AuthResponse responseDyn = JsonConvert.DeserializeObject(response);
return responseDyn;
}
}
public class AuthResponse
{
public string access_token { get; set; }
public string instance_url { get; set; }
public string id { get; set; }
public string token_type { get; set; }
public string issued_at { get; set; }
public string signature { get; set; }
}
}

Constants Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ToolingAPI
{
public static class Constants
{
public static string USERNAME = "REPLACE WITH YOUR USERNAME";
public static string PASSWORD = "REPLACE WITH YOUR PASSWORD";
public static string TOKEN = "REPLACE WITH YOUR SECURITY TOKEN";
public static string CONSUMER_KEY = "REPLACE WITH THE CONSUMER KEY";
public static string CONSUMER_SECRET = "REPLACE WITH THE CONSUMER SECRET";
public static string TOKEN_REQUEST_ENDPOINTURL = "https://login.salesforce.com/services/oauth2/token";
}
}

If there's no errors you will be able to get session id from the Salesforce Rest api.
Step 4 : Add the web reference ( Tooling api wsdl ) to the console app 
Next you need to add the web reference to the project. In order to do this you can download Tooling API wsdl from Salesforce and give the local path or you can just give the Toolin API wsdl url which is generated from Salesforce. In this case I'm giving the Salesforce url to the project.
You need to right click on the references and click on Add Service References and then click Add Web References button.
3
Then paste the url which you generated from Salesforce in the step 1 and paste it here. Once you click on the go button, the tooling wsdl will be visible for you. If you are not logged you might need to log in the Salesforce in order to load the wsdl.
4
Next you need to give a web reference name and click on Add Reference. In my case I have given it as com.salesforce.tooling.
Then compile and run the project. Surely you will end up with an error as below.
5
If so search [][] in the project and replace [][] with []. This is an bug when generating the reference.cs.
Step 4 : modify the code
After that you need to modify the code as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Services.Protocols;
using ToolingAPI.com.salesforce.tooling;
namespace ToolingAPI
{
class Program
{
static void Main(string[] args)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Task authResponse = Task.Run(() => Auth.GetAuthResponse());
authResponse.Wait();
SforceServiceService toolingService = new SforceServiceService();
toolingService.Url = authResponse.Result.instance_url;
toolingService.SessionHeaderValue = new SessionHeader();
toolingService.SessionHeaderValue.sessionId = authResponse.Result.access_token.ToString();
if (authResponse.Result != null)
{
try {
ApexTrigger1 newTrigger = new ApexTrigger1();
newTrigger.Name = "TriggerOnAccountBeforeUpdate";
newTrigger.TableEnumOrId = "account";
newTrigger.Body = "trigger TriggerOnAccountBeforeUpdate on Account (before update{ }";
SaveResult[] saveResult = toolingService.create(new sObject[] { newTrigger });
}
catch (Exception e)
{
Console.WriteLine("Error occured " + e.Message.ToString());
}
}
}
}
}

Comments

Popular posts from this blog

Exploring the Marvels of Salesforce Marketing Cloud: Unleashing the Power of Marketing Automations

Salesforce Marketing Cloud is a comprehensive marketing automation platform offered by Salesforce, a leading customer relationship management (CRM) company. It is designed to help businesses manage and optimize their marketing efforts across various channels and touchpoints. The platform enables organizations to create, execute, and analyze marketing campaigns to engage with their target audiences more effectively and drive better results. Key features and capabilities of Salesforce Marketing Cloud include: Email Marketing: Users can create and send personalized email campaigns to segmented audiences, track email performance metrics, and automate email workflows to nurture leads and build customer relationships. Journey Builder: This tool allows marketers to design and automate customer journeys across multiple channels such as email, mobile, social media, and advertising. It helps create personalized experiences based on customer behavior and interactions. Social Media Marketing: Sale

Salesforce Metadata API

In this post I'm going the cover basics in the Metadata API and how it can connect with a .net console application. Then after I will show you how to create a custom object and create fields by using the metadata api from the console app. What is Salesforce Metadata API There are many things can be done from the metadata api. you can create Objects,Fields,Validation,Workflows, Profiles, Reports for a particular client organization just by running the piece of code so that you need not do all the customization settings manually which will consume time if you have to do that several times for different clients for the same configuration settings. You can also take the backup of your organization customization settings just by fetching the metadata WSDL from your Salesforce org. You can read, create, update, delete following components in a Salesforce organization. Custom Objects/Fields. Visualforce pages. Page Layouts. Validation rules. Apex. Workflows. Approval processe

Salesforce REST API

This post describe about the salesforce rest api and using salesforce rest api . Salesforce implements the OAuth to authenticate soap and rest calls with the client. So I will describe with the simple steps, how to create salesforce app that expose outside to authenticate and call rest methods. First you need to create a salesforce developer account if you don't have an developer account or sandbox account. If you don't have an developer account go with the following link and create an account. It's free.   https://developer.salesforce.com/signup . Once you create the developer account go to https://login.salesforce.com/ and login with your credentials.     Navigate to setup and type app and you will be able to find Apps section and click the link. So now you will be able to find Connected Apps section and click New. What we doing here is creating a connected app and that app will exposing to outside to authenticate and call rest api calls. So you can insert C