This diagram shows the execution path of an asynchronous callout, starting from a Visualforce page. A user invokes an action on a Visualforce page that requests information from a Web service (step 1). The app server hands the callout request to the Continuation server before returning to the Visualforce page (steps 2–3). The Continuation server sends the request to the Web service and receives the response (steps 4–7), then hands the response back to the app server (step 8). Finally, the response is returned to the Visualforce page (step 9).
A typical Salesforce application that benefits from asynchronous callouts is one that contains a Visualforce page with a button that users click to get data from an external Web service. For example, the Visualforce page might get warranty information for a certain product from a Web service. This page can be used by thousands of agents in the organization. Consequently, a hundred of those agents might click the same button to process warranty information for products at the same time. These hundred simultaneous actions exceed the limit of concurrent long-running requests of 10, but by using asynchronous callouts, the requests aren’t subjected to this limit and can be executed.
In the following example application, the button action is implemented in an Apex controller method. The action method creates a Continuation and returns it. After the the request is sent to the service, the Visualforce request is suspended. The user must wait for the response to be returned before proceeding with using the page and invoking new actions. When the external service returns a response, the Visualforce request resumes and the page receives this response.
This is the Visualforce page of our sample application. This page contains a button that invokes the startRequest method of the controller that’s associated with this page. After the continuation result is returned and the callback method is invoked, the button renders the outputText component again to display the body of the response.
<apex:page controller="ContinuationController" showChat="false" showHeader="false"> <apex:form > <!-- Invokes the action method when the user clicks this button. --> <apex:commandButton action="{!startRequest}" value="Start Request" reRender="result"/> </apex:form> <!-- This output text component displays the callout response body. --> <apex:outputText id="result" value="{!result}" /> </apex:page>
The following is the Apex controller that’s associated with the Visualforce page. This controller contains the action and callback methods.
public with sharing class ContinuationController { // Unique label corresponding to the continuation public String requestLabel; // Result of callout public String result {get;set;} // Endpoint of long-running service private static final String LONG_RUNNING_SERVICE_URL = '<Insert your service URL>'; // Action method public Object startRequest() { // Create continuation with a timeout Continuation con = new Continuation(40); // Set callback method con.continuationMethod='processResponse'; // Create callout request HttpRequest req = new HttpRequest(); req.setMethod('GET'); req.setEndpoint(LONG_RUNNING_SERVICE_URL); // Add callout request to continuation this.requestLabel = con.addHttpRequest(req); // Return the continuation return con; } // Callback method public Object processResponse() { // Get the response by using the unique label HttpResponse response = Continuation.getResponse(this.requestLabel); // Set the result variable that is displayed on the Visualforce page this.result = response.getBody(); // Return null to re-render the original Visualforce page return null; } }