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.
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);
Senior Salesforce Architect
Connect me on LinkedIn
Follow me on Twitter @apexcoder
Subscribe to my blog https://apexcoder.com
Email me email@example.com