Integration

Salesforce Rest API with Example


Salesforce Rest API with Example

 Salesforce Rest API with Example

When we talk about Salesforce Data API's - "Rest API", "SOAP API", "Bulk API" and "Streaming API" are few commonly used API's which enables us to manipulate Salesforce Data.  There are other API's available which are specifically used for Metadata changes.

In this post we will stress mainly about Rest API along with some live examples.

Salesforce exposes the functionality to retrieve data and manipulate data via Rest Resources and HTTP methods. By Manipulating data I mean creating ,updating and deletion of records.

Since Rest Callout are based on HTTP methods, lets understand the basic methods.
HTTP MethodDescription
GETRetrieve data identified by a URL. Works similar to a query
POSTCreate records/ Post data to server.
DELETEDelete Record/ resource identified by a URL.
PUTCreate or replace the record/resource sent in the request body.
To view these methods in action , I have built a Rest Explorer on Salesforce Sites and exposed it to the "Salesforce Rest Explorer" tab of the website. use this link 

http://www.worldofsalesforce.com/p/world-of-salesforce.html

 to open the Rest Explorer. 

Login to your organization by providing the username, password and selecting the Environment. Select production in case the organization you want to connect is develop org.

Rest Explorer Salesforce

On successful validation of credentials you would be presented with a screen to choose the HTTP method and perform REST Callout to the org. You also see the session information by clicking on the Session Information tab.

Salesforce Rest Explorer

The radio buttons above the URI represent the standard HTTP methods. To make an API call, enter the resource URI, select the appropriate method, add body  as needed, and then click Execute.



GET Method:  Let’s try out the SObject Describe resource. This resource, when combined with the GET method, returns metadata about an object and its fields.
We’ll try describing the Account object. Replace the existing text in the URI text box with /services/data/v39.0/sobjects/account/describe , where 39 maps to the API version. 

Salesforce Rest Explorer

Lets Understand the URI before clicking on Execute. 
  • /services/data—Specifies that we’re making a REST API request
  • /v39.0— Version Number of the API used.
  • /sobjects—Specifies that we’re accessing a resource under the sObject grouping
  • /account—sObject being actioned; in this case, account
  • /describe—Action; in this case, a describe request
Press execute the get the results. 

Salesforce Rest Explorer

Now lets try something interesting.  Querying the database .
To query we need to change the URI appending query as the resource.
Eg:- to fetch the count of accounts in the org use /services/data/v39.0/query/?q=SELECT+count()+From+Account  and press execute.  You can frame any query but remember to replace spaces with + to properly encode the URI. 


Salesforce Rest Explorer


POST Method : As described in the HTTP methods table , post is used for creation of record. Lets try creating a record. 
Select Post as the radio option in the explorer.
The Endpoint URI should point to sobjects/object name. so use /services/data/v39.0/sobjects/account/ in the URI box.  Additionally you would be presented with the space to enter the body. This body takes the data in JSON. To create a account lets provide the mandatory fields in JSON format, Name in the case of  account.
{
"Name": "test Account"
}
Press execute and you will be presented with the result as the ID of the account which is created. 
This way you can create records for any Sobject providing the field information in the form of JSON in the body of the request. 

Salesforce Rest Explorer

In the next post

http://www.worldofsalesforce.com/2017/04/custom-apex-webservice-using-rest.html


I have explained how to create custom Rest Resources using Apex. Comments and suggestions are more than appreciated.

About Saurabh Dua

3 comments:

  1. A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.



    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai


    ReplyDelete

Powered by Blogger.