Salesforce REST API

Salesforce REST API

 

What is the Salesforce Rest API? 

REST APIs are available from Salesforce to interface with its platform. It is the most typical method of integrating third-party services or apps. Its benefits include simplicity in development and integration. Salesforce REST API supports JSON and XML.

External apps can connect to Salesforce using the rest APIs that are built into the platform (or those that developers have specifically created) to obtain data (records, field values) or to put (edit) data.

Salesforce.com‘s apex language can call the rest APIs created in other applications to retrieve or communicate information to another application.

Salesforce compatibility with REST API

A Salesforce org with API access is a prerequisite for using the REST API Salesforce to access Salesforce data. Additionally, you will need an API Enabled user authorization within that organization.

Salesforce offers the following editions with API access:

    • Performance Edition, Professional Edition, Unlimited Edition, Developer Edition, and Enterprise Edition.
    • While utilizing the REST API Salesforce, it is best to carry out all testing and development in the Developer Edition, sandboxes, or scratch org. Before putting new ideas into practice, you might test and experiment with them in a different setting.
    • It is necessary to have API Enabled Permission to use the Salesforce REST API. An organization’s administrator grants this authorization. This authorization is pre-configured in the Developer Edition

Working with Salesforce REST API

REST API for Salesforce Pre-requisites

Before using the Salesforce REST API, you must finish all the prerequisites. The REST API for Salesforce Among the requirements are:

    • Join the Developer Edition now.
    • Make sure the API is enabled.
    • Create a Connected App
    • Obtain Consumer Secrets and Consumer Key
    • Recognize cURL

Callouts for the Salesforce Apex REST API

Callout denotes making a call from Apex code to an external Web service or submitting an HTTP request and then waiting for the result. REST callouts are built on HTTP; each callout request has an endpoint and an accompanying HTTP method. The HTTP method specifies the desired action type.

The descriptions of typical HTTP methods are as follows:

    • GET 

HTTP Method Description Obtain information from a URL

    • POST

Publish data to the server or create a resource.

    • DELETE

Remove a resource that has a URL associated with it.

    • PUT

The resource given in the request body should be created or changed.

  • A GET request is the most basic type; it expresses the sender’s desire to receive data (information) from the server. The information contained in the request is returned to the sender as a response when the server has received and processed it.
  • JSON is the response format used to provide the requested data. For example, a GET response might occasionally take the form of XML.
  • To generate a new HTTP Request to be delivered to an external system, utilize the HttpRequest class. It provides methods like setMethod(), set header(), and set body() that are used to configure the HTTP request’s parameters.
  • The HTTP Response returned from an external system is stored using the HttpResponse class. The data returned in the response can be obtained using methods like getBody(), getStatus(), and get status code().
  • This class, known as HTTP, is used to make HTTP requests to external APIs and get responses from such APIs. It only has two methods, send() and function toString() { [native code] }(), with send() being the more popular choice. This function returns an instance of HttpResponse in the response and accepts an instance of HttpRequest as a parameter.

JSON specifications

A well-liked standard for data communication is JSON. The data are handled in key-value format.

A double quote around the critical value (string)

Value – Any datatype

{} – object

[] – array

– separates data elements within the object

How to create a callout in apex? 

Step 1: Navigation

The open developer console in Salesforce org – click file – new – apex class. Next, enter the apex class name and click ok, which creates a new apex class.

Step 2: Write Apex Class with Callout

Http Http = new Http();

HttpRequest request = new HttpRequest();

request.standpoint(‘https://th-apex-http-callout.herokuapp.com/animals’);

request.setMethod(‘POST’);

request.set header(‘Content-Type’, ‘application/JSON;charset=UTF-8’);

// Set the body as a JSON object

request.set body(‘{“name”:”mighty moose”}’);

HttpResponse response = http.send(request);

// Parse the JSON response

if(response.getStatusCode() != 201) {

    System.debug(‘The status code returned was not expected: ‘ + response.getStatusCode() + ‘ ‘ + response.getStatus());

} else {

    System.debug(response.getBody());

}

Step 3: Setting Up Remote Site

An external site must be registered in the Remote Site Settings tab before any Apex callout can call it. Otherwise, because Salesforce blocks call to unauthorized network addresses, the Salesforce REST API callouts will fail.

A remote site setting should be included.

    • To access the Remote site details page from Setup, type Remote site settings into the Quick Find box and then click Remote site settings.
    • You are now in the New Remote Site.
    • The Remote Site Name should have a descriptive phrase added.
    • Enter the Remote Site URL here.
    • Enter a site description if you wish.
    • Press “Save”

Test your Salesforce Rest API Callout code at the last step.

Conclusion

To learn more, read the REST API developer guide. The REST API allows you to access (read, update, delete, export…) all data that is present in Salesforce from an external app.

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_rest.htm

Summary
Article Name
Salesforce REST API
Description
Salesforce REST APIs are the method of integrating third-party services or apps, including simplicity in development and integration. It supports JSON and XML.
Author
Publisher Name
BugendaiTech Pvt Ltd
Publisher Logo
Spread the updates

Leave a reply