Long-Running callout with continuation in Lightning

API callouts that take more than 5 seconds are called long-running callouts. Salesforce allows only 10 long-running callouts at a time in an environment. To avoid hitting this limit Salesforce provides continuation. This uses a callback mechanism and can be implemented in Visualforce or Aura.

Use cases

If you have an integration that is used by more than 10 concurrent users and the API can take more than 5 seconds to respond, you should consider using continuation.

Steps to Implement

STEP 1 :- Create a remote site setting/named credential corresponding to the API you want to access. You can find remote site settings under setup => remote site settings. To follow this example, create a remote site setting whitelisting the domain https://salesforcecodes.blogspot.com/

STEP 2 :- Create an apex class that has an AuraEnabled method and annotated as continuation=true. This method starts the continuation processing. Add a second method to process the result. It is processBlogResponse in our example.


STEP 3 :- Create an aura component and call the apex method annotated with continuation=true. Syntax to call continuation method from aura is similar to calling other apex methods from aura.


STEP 4 :- Create an aura app or flexi page to display your aura component with continuation. In this example I am creating a TestApp aura applicaiton. After this go to https://YOURDOMAINHERE.com/c/TestApp.app to see the application displaying blogs retrieved using continuation.


No comments:

Post a Comment