SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Friday 10 April 2020

Multiple Dependent Apex HTTP Callouts in a Single Transaction from Salesforce | HTTPCalloutFramework

Hello Trailblazers,

Welcome to the 4th tutorial in Simplifying the Callouts in Salesforce Tutorial Series. In this tutorial, we're going to perform Multiple Dependent Apex HTTP Callouts in a single transaction. This is a Video-Only Tutorial Series. So, I am giving you a summary of the tutorial below and you can learn in detail by watching the video at the end of the blog.

Note:- This tutorial is using resources from previous tutorial. So, in case, you want to implement the same example on your own, make sure to have a look at the previous tutorial once.

We've made some updates to our CustomerRubyOrg metadata record as shown below:-


As you can see above, I have added a header with key:- Content-Type and value:- application/json. I have updated the method as POST and also the endpoint as:- callout:CustomerRubyAPI/services/data/v48.0 where I am referring CustomerRubyAPI which is the name of my Named Credential record that we created in our previous tutorial.

Let's have a look at the code below:-


As you can see above, we have a OrgConnectService class. In which we have a single method named as createAccountAndContact(). This method is receiving a string for account name and a contact object as a parameter. Inside this method, we're performing 3 callouts to another salesforce org (we call it as source org) one by one. So, in total 3 operations are performed in source org as shown below:-
  1. Creating a new Account Record using the account name received in parameter.
  2. Updating the contact record received in parameter by linking it with Account Record and creating a new Contact Record.
  3. Querying the contact and related account record.

First of all we're creating an instance of HTTPCalloutService named as destinationOrgService and we passed the custom metadata name:- CustomerRubyOrg in the constructor. Then we're setting the endpoint URL and request body for the callout using getter and setter methods present in HTTPCalloutService which is a part of HTTPCalloutFramework.

Finally, we're sending the request and checking if the response code is correct or not. For record creation in salesforce, the response code should be 201 and for querying it should be 200. For the first two callouts, I am parsing the response body using JSON.deserializeUntyped() method which is returning an Object in the response that I am typecasting into Map<String, Object>. I am checking the value of success and getting the id of the record which is created. However, in the third callout, I am simply displaying the response using System.debug().

Wether the request is successful or not, we're forming a Map<String, String> which we're returning by this method. We'll be using this map in the next tutorial where we'll be creating a test class for this class in order to add asserts for all 3 callouts depending upon the return value map.

I have added necessary comments to help you understand the code. The basic flow is :- We're creating an account record by calling out to salesforce standard api. Then we're creating a contact record and linking it with the account record whose id we got in the response from first API callout. Finally, we're querying the contact and related account record using the contact id that we got in the response from 2nd callout. So, in this way each callout depends on the previous callout response which is a very common requirement that we get usually face in real life projects while working on integration.

Want to learn in depth ? Have a look at the below video:-



In the next tutorial we'll see how we can create a test class for such a scenario where we have 3 dependent callouts in a single transaction and that too without creating a mock class using HTTPCalloutFramework. If you liked this tutorial, make sure to share it in your network and let me know your feedback in the comments down below.

Happy Trailblazing..!!

No comments:

Post a Comment