Salesforce integration using REST API – Explained step by step

//Salesforce integration using REST API – Explained step by step
  • Banner Image Salesforce Integration using REST API

Salesforce integration using REST API – Explained step by step

In this article, we are going to see the step-by-step procedure on how to connect to Salesforce using Postman for performing integration tests. The tutorial is fairly elaborate and contains, details on how to authenticate, retrieve details of a record, create a new record and run SOQL queries within Salesforce. Though we are using Postman for this tutorial, you can use this tutorial to call Salesforce REST API from virtually any other application capable of calling REST protocol.

Salesforce provides “Connected App” to connect with the platform with any other application. As we will be using this capability of Salesforce, so lets learn a bit more about it.


Salesforce Connected App

“Connected App” is an application which connects Salesforce org with an external application. It uses standard SAML and OAuth protocols to authenticate, provide single sign-on, and provide tokens to use with Salesforce APIs.

Getting started with Salesforce Connected App

Below are the steps to create a connected app in Salesforce

  • Go to Setup->Build->Create->Apps

                          Navigating to Apps 

  • Click on ‘New’ Button in “Connected App” section, Below screen will be shown

Screenshot of connected App section

  • Enter the Name of the Application.
  • Enter Contact Email and any additional information suitable for your application.
  • Enable OAuth settings in API section.
  • Enter a Callback URL. This is the URL that a user’s browser is redirected to after successful authentication. Here I’m giving “https://login.salesforce.com/services/oauth2/callback” as callback URL. You can enter multiple callback URLs but, you have to make sure that callback URL must be secured and if multiple URLs are used, they should be separated by line breaks.
  • Add Selected OAuth Scopes. Here I’m giving “Full access(full)”.

Entering requried field infomation

  • Click on ‘Save’ button.
  • Click on ‘Continue’
  • You will be redirected to your Connected App’s Page
    Redirected to connected App’s

After creating the Connected App

  1. Click on Click to reveal link to get Consumer secret.
  2. Make a note of “Consumer Key “ and “Consumer secret”, as you need these details to authenticate the external application.
  3. Goto Manage->OAuth Policies-> select IP restrictions asRelax IP restrictions”.

With the completion of this step, now we will see how to call and integrate Salesforce with Postman.


Understanding basics of Postman 

Postman is an application for interacting with HTTP APIs. It has powerful testing features and user friendly GUI for making requests and reading responses.

Getting started with Postman

  1. Install Postman by going to Postman APPs . It is also available as Add-On for Google Chrome. If you wish to install it as a plugin, navigate to Google Chromes web-store->search for Postman and add it to chrome.
  2. Launch Postman, below screen will be shown


Integrating Salesforce using REST API

Now that are equipped with understanding Salesforce integration, lets get to our main objective of integrating Salesforce with third party application. For this we have the following steps.

Step #1. Setting up the HTTP login request URL

  • We are going to set request URL by (Base URL + Parameters), below is the structure to construct request URL for Salesforce org

https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=YourConsumerKey&client_secret=YourConsumerSecret&username=SalesforceUserName&password=SalesforcePassword

  • In the above URL replace highlighted text with
    • YourConsumerKey: Consumer Key obtained from Connected App.
    • YourConsumerSecret: Consumer Secret obtained from Connected App.
    • SalesforceUserName: Salesforce User ID.
    • SalesforcePassword: Provide the login credentials of Salesforce

Step #2.  Authenticating your request with Salesforce

Create a POST method and Copy the request URL as shown below and click on send.

Create a POST method and Copy the request URLOn successful login, you will get Instance-URL, Access token, Token-type. This Access token is further used as a parameter value in Header to send any HTTP requests and get the response from Salesforce. So, make a note of access_token as shown in the figure above.


Business Case #1. Retrieving record details from Salesforce

Now we will see how to get details of a record from Salesforce object using the GET.  Below is the Sample URL.

https://instanceurl/services/data/v25.0/sobjects/Objectname/Id

  • In the above URL replace highlighted text with
    • Instanceurl: Instanceurl obtained in the same manner.
    • Object Name: SObject’s like Account, Contact, Lead etc.
    • Id: This is the salesforce Id of particular record of the SObject that you have selected.

Create a GET method, copy the URL and copy the Access token along with Token type in Header section as shown below
Screenshot of access token along with Token type

Click on the send button. In the response, you will get all the Account details of that particular record as shown below.

Screenshot of response from salesforce


Business Case #2. Running SOQL queries in Salesforce using REST 

SOQL are parallel to database query. If you wish to learn more about SOQL queries click on the link. We will see how to fetch data using SOQL query using the GET. Below is the sample URL.

https://instanceurl/services/data/v25.0/query?q=SOQL-Query

  • In the above URL replace highlighted text with
    • Instanceurl: Instanceurl obtained in example above.
    • SOQL-Query: Each word in SOQL Query should be separated with ‘+’ as shown below
      • Normal SOQL: Select Id, Name from Account
      • Postman SOQL: Select+Id+,+Name+from+Account

Repeat the steps and click on send button, In the response, you will get Id, Name for all the records as shown belowScreenshot of all the records response from salesforce


Business Case #3. Creating a record in Salesforce using REST

Now we’ll see how to add records to salesforce object using POST method. Below is the sample URL.

https://instanceurl/services/data/v25.0/sobjects/Objectname

  • In the above URL replace highlighted text with
    • Instanceurl: Instanceurl obtained in the same way as described above.
    • Object Name: SObject’s like Account, Contact, Lead etc.

Create a POST method, copy the URL and copy the Access token in the Header section. In the body section provide the details of record either in XML or JSON format. Here I’m providing data in XML format.

Screenshot of record details

Click on the send button. In the response, you will get the SalesforceId of the record if the record is added successfully to Salesforce org else you will get the error message.

Screenshot of salesforce Id

This marks the completion of our tutorial on how to create, retrieve and query records in Salesforce using REST APIs.


How we can help

Our team can help you customize and integrated Salesforce as per your business process. Read about our Salesforce Integration Services. Reach out to us for a free assessment of your business needs.

Contact us for a free assessment

Spread the Wisdom
By |2018-06-17T21:33:46+00:00June 14th, 2018|salesforce|0 Comments

About the Author:

Veon brings best in consulting and integration solutions on Salesforce, SugarCRM, and SAP. It is an innovative company which deals best of the breed solution using integration solutions on the above platforms. making small and medium enterprise become smarter.