function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pari KatyalPari Katyal 

REST callout to external service when an account is created

Hi,
I am just starting with apex class, triggers and integration patterns.
My goal is that, whenever an account is created in Salesforce, I want to make a REST callout to an external service with the data in the account.
Based on research, I found that I will have to create an apex class with an HTTP service to make a REST API call.
But from where should I invoke this apex class? When an account is created/updated, an apex class should be invoked to make a REST API call to an external service.
Can anyone please guide me on this?

Thanking you in advance. 
Ashish Singh SFDCAshish Singh SFDC

Hi Pari,

As a beginner, I would recommend you to start with GET Service.

First of all complete this module: Apex Rest API Service (https://trailhead.salesforce.com/content/learn/modules/api_basics/api_basics_rest)

And the go ahead with an authenticated service like:

http://www.postalpincode.in/Api-Details

then, https://currencylayer.com/

https://openweathermap.org/api,

https://developer.box.com/reference/.

After that you can create one more developer account and integrate service between them.

Thanks,

Ashish Singh.

Pari KatyalPari Katyal
Hi Ashish, 
Thanks for your suggestion.
I have previously used REST API from an external system to create accounts in Salesforce (inbound integration).
Now I am working on outbound, HTTP callout (Calling REST API from Salesforce to external systems). 
I have figured out to use apex classes to make an HTTP callout, but I would like to get your suggestion on, how this apex class is called when an account is created.

Thanks and regards.
Ashish Singh SFDCAshish Singh SFDC
Hi Pari,

There are various Integration Pattern (https://developer.salesforce.com/docs/atlas.en-us.integration_patterns_and_practices.meta/integration_patterns_and_practices/integ_pat_pat_summary.htm) which you should get familiar as you progress.

I would take an example of whenever an Account is created in Salesforce you want to send it to the third-party system. First of all, a third-party system(server) will expose a web service in their system, and then they will Provide API documentation for your consumption.

You'll go through the API documentation which would comprise of authentication mechanism which means something like username password/security token to validate your request to the server. Also, it would consist of an endpoint URL where the service resides. There will be a POST method to call this service. This is web standard if you want to create a record in a different system using API then the POST method is exposed.

Now in Salesforce, you'll create a trigger that will call a future method and in that method, you'll write the logic for the authentication mechanism to get access token from the server, and then you'll finally request service along with the token in the header and body which was defined in the structure.

See this neatly documented example  Salesforce to Salesforce Rest API Integration (https://www.forcetalks.com/blog/salesforce-to-salesforce-integration-using-rest-api-callouts/). 

Useful tools to test Integration
1. Postman (https://www.postman.com/)
2. Workbench (http://rkbench.developerforce.com/login.php)

Tool to convert JSON to Apex
1. Convert JSON to Apex (https://json2apex.herokuapp.com/)

API Concept:

1. JSON w3school (https://www.w3schools.com/js/js_json_intro.asp)
2. Rest API Learning Tutorial (https://www.restapitutorial.com/lessons/httpmethods.html)

All the very best.

Thanks,
Ashish Singh.
Pari KatyalPari Katyal
Thank you very much for the detailed explanation and useful links Ashish. 
Much appreciated !