REST API Integration

REST API Integration

Salesforce is a leading Customer Relationship Management (CRM) platform that provides a comprehensive set of tools and features to manage customer data, interactions, and business processes. REST API integration in Salesforce is a common requirement for organizations that want to integrate their Salesforce instance with external systems and applications.

What is REST API?
REST stands for Representational State Transfer and is a popular web standard for exchanging data between systems. REST APIs use HTTP requests (GET, POST, PUT, DELETE, etc.) to interact with resources and exchange data in a standardized format, typically JSON or XML.

Why integrate REST API with Salesforce?
Integrating REST APIs with Salesforce opens up a wealth of possibilities for organizations. Some of the benefits of integrating REST APIs with Salesforce include:
Data exchange: You can use REST APIs to exchange data between Salesforce and other systems, such as databases, web services, and cloud applications.
Customization: You can customize Salesforce to meet the specific needs of your organization by integrating it with other systems and applications.
Automation: You can automate tasks and processes by integrating Salesforce with other systems and applications. For example, you can automate the transfer of data between systems or trigger actions based on events in Salesforce.
Enhanced functionality: You can enhance the functionality of Salesforce by integrating it with other systems and applications. For example, you can integrate Salesforce with a mapping tool to display customer locations on a map or integrate it with a payment gateway to process online payments.

How to integrate REST API with Salesforce?
Integrating REST APIs with Salesforce involves several steps:
Obtain an API key or authentication token from the external system that you want to integrate with.
Use the Salesforce REST API to make HTTP requests to the external system. You can use the Salesforce REST API to access data, create records, update records, delete records, and perform other operations.
Use the Salesforce Apex code to write custom logic that integrates with the external system. The Apex code can be used to process the data returned by the REST API and to trigger actions in Salesforce based on the data.
Use Salesforce Lightning Connect or Salesforce External Objects to display data from the external system in Salesforce.

Here are the steps to integrate a REST API:

1. Create your REST API endpoints
To begin with, I created a REST API to GET, DELETE, and POST account records. I have provided the Apex code below.

2. Create a Salesforce Connected App
The next step is to establish a Connected App. On your organization, go to the Setup > App Manager > New Connected App section.

 Add new Connected App


Make sure you are using the same organization which contains the endpoint established in the initial step!
-Choose a name for your app
-Make sure to check the Enable OAuth Settings box.
-The Callback URL requires a placeholder URL and for this particular purpose, we can use https://www.login.salesforce.com/services/authcallback.
-Make sure to adjust the Selected OAuth Scope to match the purpose of your app.

Configure Connected App


-Click on the Manage Consumer Details button.

Manage Consumer Details Button

-Copy Consumer Key and Consumer Secret. Those two keys will be required to connect with our app.

Copy Consumer Key and Consumer Secret

 

3. Create Auth. Provider
To make callouts, you must set up an Authentication Provider.
This is done by going to Setup > Authentication Provider > New.

Select Salesforce


-Select “Salesforce” from the provided type.
-Enter the Customer Key and Customer Secret Values, which come from the Connected App.
-Enter the Authorize URL, which is “https://login.salesforce.com/services/oauth2/authorize” and the Token Endpoint URL “https://login.salesforce.com/services/oauth2/token”.

Fill fields
Check Test-Only Initialization URL and Callback URL
  • Remember to copy the Callback URL and paste it into the Connected App in the org that was defined. This can be found under Setup > App Manager > Select Your App > Edit. Finally, save the changes.
Paste CallbackURL


It may take from two to ten minutes for the alterations you have made to take effect on the server before using the app associated with it. To test the connection, go back to Auth. Provider and open the Test-Only Initialization URL which can be found in the Salesforce Configuration section.
Note: If you encounter an error that says “error=redirect_uri_mismatch&error_description=redirect_uri%20must%20match%20configuration,” it is likely that you have not modified the Callback URL in your app or you need to wait a few minutes.
If everything is correct, you should be able to see a Salesforce login popup. Log in with your credentials, preferably with an integration user.


4. Create Named Credentials

The fourth step involves utilizing Named Credentials to assist with authorization. Salesforce will oversee any authentication for Apex callouts that identify a Named Credential as its endpoint, thus eliminating the necessity for code to perform this task.

Setup > Named Credentials > New

Create new Named Credentials

1. Label – Select your API Name.
2. Name – This name will used in Apex
3. URL – Enter the Salesforce org URL, which is the same one where you created the Connected App.
4. Authentication Protocol – Select OAuth 2.0
5. Authentication Provider – Select the provider you have created before.
6. Scope – Select the scope you need. The scope has to be linked to the Connected App and must be separated by a space.
7. Start Authentication Flow on Save – choose “Start Authentication Flow on Save”, and you will be prompted to log into Salesforce.

-After clicking the ‘Save’ button, log into the Salesforce org that contains the Connected App and assess the Authentication Status.
Ideally, you should be authorized as an Integration User.

Test authorization

5. Remote Site Settings (Optional)
Before testing, we need to add the endpoint URL to the Remote Site Settings, or else, Salesforce will not enable us to make a callout (when we lack Named Credentials).
To do this, go to Setup > Remote Site Settings > New Remote Site. The Remote Site URL has to be the same as the salesforce instance containing the endpoints.

Remote site Settings


Once completed, we can now test the app!

Create a new Apex class on the Salesforce org where you have previously established the Auth. Provider, Named Credentials, and Remote Site Setting.
For reference, you can use the Salesforce Authorization Endpoints, Connected App, and Code Example.

Open an anonymous window to invoke apex methods.

Invoke method in Anonymous

Conclusion:
Integrating REST APIs with Salesforce can bring a wealth of benefits to your organization, including improved data exchange, customization, automation, and enhanced functionality. By following the steps outlined in this article, you can integrate REST APIs with Salesforce to achieve your business objectives.