Work with a Continuation in an Apex Class

To work with a continuation in an Apex class, use the Apex Continuation object.

Before you can call an external service, you must add the remote site to a list of authorized remote sites in the Salesforce user interface. From Setup, in the Quick Find box, enter Remote Site Settings. Select Remote Site Settings, and then click New Remote Site. Add the callout URL corresponding to LONG_RUNNING_SERVICE_URL in the Apex Class Continuation example below.

If the callout specifies a named credential as the endpoint, you don’t need to configure remote site settings. A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. In your code, specify the named credential URL instead of the long-running service URL.

  1. To make a long-running callout, define an Apex method that returns a Continuation object. (Don’t worry about the attributes of the @AuraEnabled annotation yet. We explain that soon.)

  2. Set an Apex callback method to be invoked after the callout completes in the continuationMethod property of the Continuation object. In this example, the callback method is processResponse. The callback method must be in the same Apex class.

  3. Set the endpoint for a callout by adding an HttpRequest object to the Continuation object. A single Continuation object can contain a maximum of three callouts. Each callout must have a remote site or named credential defined in Setup.

  4. Set data to pass to the callback method in the state property of the Continuation object. The state property has an Object type so you can pass in any data type that’s supported in Apex.

  5. Code the logic in the Apex callback. When all the callouts set in the Continuation object have completed, the Apex callback method, processResponse, is invoked. The callback method has two parameters that you can access.

    1. labels—A list of labels, one for each request in the continuation. These labels are automatically created.
    2. state—The state that you set in the state property in your Continuation object.
  6. Get the response for each request in the continuation. For example:

  7. Return the results to the component’s JavaScript file.

Here’s a complete Apex class that ties together all the earlier steps.