How to integrate Salesforce with a third party Application

How to integrate Salesforce with a third party Application

Developers often face the challenge of integrating Salesforce with a third party Application. This is same as consuming external API in Salesforce.

Some use cases:

  • Uploading files to AWS, Google Drive, Dropbox from within Salesforce
  • Sending data to external Apps such as email or SMS from Salesforce
  • Updating your financial system from Salesforce and more…

There are many Apps available on AppExchange for most commonly used external systems. However, if your application is not among these, you can follow these general guidelines to integrate the application.

Steps to integrate with an Application

First your application should have well defined API. API is nothing but a set of rules on how the external party communicates with your application. There will be end point, methods, expected parameters and response.

Second, read and explore a bit of HTTP request class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_httprequest.htm. Also, the HTTP response class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_httpresponse.htm

As the name suggests, using request class we can send a request to a third part application. And using response class, we can read the response sent back by the third party Application.

Examples

Next, we will create apex code to send a request and receive response from an application.

For example, if we consider Google Places API: https://developers.google.com/places/web-service/intro

Let’s say I wish to get auto suggestions from Google when my users start typing the name of a place. If you read a bit on the available API’s, you will know that Places Autocomplete API matches our requirement.

Important before writing the Apex Class

Add the base end point ( maps.googleapis.com in this case) to Remote Site Settings in Salesforce setup.

If you have read through the HTTP request class, first we need to create a request.

String strURL=''; // Here set the endpoint.
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(strURL);
req.setMethod('GET');

The end point in the documentation is https://maps.googleapis.com/maps/api/place/autocomplete/output?parameters. Therefore, for this example strURL can be:

https://maps.googleapis.com/maps/api/place/autocomplete/json?'
                 + 'input=' + EncodingUtil.urlEncode(input, 'UTF-8')
                 + &key=' +key;

Here, input is the string entered by the user and key is the Google API key.

Now to receive the response, we will use the HTTP response class:

HttpResponse res = new HttpResponse();
res = h.send(req); //sending the request to the external application
String responseBody = res.getBody();

Notice that we used Json for output format in the endpoint. Thus, The response can be parsed using Json Class.

Another example: MailChimp API. Let say we want to add a contact to our Mailchimp List.

As per API documentation, the end point is https://<dc>.api.mailchimp.com/3.0 where dc is the datacenter. Your data center would be in the URL once you login in Mailchimp.

How to integrate Salesforce with a third party Application

In the above screenshot, my data center is ‘us3’.

Also, as per the API document we need to send ‘POST’ request to ‘ /3.0/lists/<list id>/members/ ‘ . List Id to located at Audience->Settings->Unique Id for Audience.

Thus our request code becomes:

Http h= new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://us3.api.mailchimp.com/3.0/lists/ed637e23b9/members/');
req.setMethod('GET');
req.setHeader('authorization','apikey '+apikey);//replace with API key
req.setBody('{"email_address": "uristGFF.mcvankab@freddiesjokes.com","status": "subscribed","merge_fields": {"FNAME": "Urist", "LNAME": "McVankab"}}');

The API key is located at Account->Extras->API Keys

In real life, you would probably take input from user and send that as Body to the API.

Summary

While each API is different, basic structure is same. Some like AWS S3 have complex authorization mechanism. Salesforce API itself returns session and instance token on authorization through API, which can be used in next step for any activity.

In conclusion, it is not difficult to integrate Salesforce with a third party Application. We only need to understand the API documentation well.

It is also a good idea to test the API using Postman or other such applications. This will give you clarity on how the API works.

Leave a Reply

Your email address will not be published.

Scroll to top