Making Callouts for Long-Running requests using Continuation object in Salesforce ?

Sometimes we may encounter webservice callouts in business scenarios that will take longer to respond. This is because the other end need to do significant amount of work prior to responding back. if the other end process huge number of transactions during peak loads, the response will be even slower.

Salesforce has a governor limit of maximum of 10 synchronous requests that last longer than five seconds. Asynchronous callouts that are made from a Visualforce page don’t count this limit (e.g. @future method).

To resolve above Salesforce limitation, we can use Continuation Class which makes asynchronous callout using REST and SOAP services. Using this class, we can make a long running request from a Visualforce page to external systems, and its response is returned through a callback method.

 

Execution Flow of an Asynchronous Callout - Image from Salesforce documentation

In the above example, Warranty information is accessed from an external service and the service is called by an action button on a Visualforce page. This page is accessed by hundreds of employees in an organization. So, when all of employees click the same button to get the account information from the external systems, the actions exceed the limit of maximum of 10 synchronous requests that last longer than five seconds. This limit can be overcome by using the asynchronous callout using Continuation Class .

Callout request using Continuation object

 
string requestLabel;
//Create continuation with a timeout
Continuation con = new Continuation(40);

//Set callback method
con.continuationMethod='processWarranty';

//Create callout request 
HttpRequest req1 = new HttpRequest();

req1.setMethod('GET');

req1.setEndpoint(LONG_RUNNING_SERVICE_URL);

this.requestLabel = con.addHttpRequest(req1);

Author:

Ajomon Joseph AJOMON JOSEPH
Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog https://apexcoder.com
email Email me ajomon@apexcoder.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s