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.
public with sharing class ContinuationDemo { | |
@AuraEnabled(continuation=true cacheable=true) | |
public static Object startContinuationRequest() { | |
Integer timeout = 50; | |
Continuation con = new Continuation(timeout); | |
//Apex method to execute after getting response | |
con.continuationMethod='processBlogResponse'; | |
//State can be a variable of any type supported in apex | |
con.state='Additional variable to give context. If more than one use list/set/map'; | |
//Create Http Request | |
HttpRequest req = new HttpRequest(); | |
req.setMethod('GET'); | |
//Make sure that your endpoint is added in Remote site settings | |
req.setEndpoint('https://salesforcecodes.blogspot.com/feeds/posts/default?max-results=10&alt=json'); | |
//Add request to continuation. Can add up to a maximum of three requests | |
con.addHttpRequest(req); | |
return con; | |
} | |
@AuraEnabled(cacheable=true) | |
public static String processBlogResponse(List<String> labels, Object state) { | |
// One label will be generated per request in continuation | |
HttpResponse response = Continuation.getResponse(labels[0]); | |
return response.getBody(); | |
} | |
} |
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.
<aura:component implements="flexipage:availableForAllPageTypes" controller="ContinuationDemo"> | |
<aura:handler name="init" value="{!this}" action="{!c.handleInit}" /> | |
<aura:attribute name="blogs" type="List" default="[]" /> | |
<aura:iteration items="{!v.blogs}" var="blog"> | |
<lightning:card title="{!blog.title}"> | |
<p class="slds-p-horizontal_small"> | |
<a href="{!blog.url}" target="_blank">{!blog.title}</a> | |
</p> | |
</lightning:card> | |
</aura:iteration> | |
</aura:component> |
({ | |
handleInit : function(component, event, helper) { | |
var action = component.get('c.startContinuationRequest'); | |
action.setCallback(this, function(response) { | |
if (component.isValid && response.getState().toLowerCase() === 'success') { | |
let responseObj = JSON.parse(response.getReturnValue()); | |
let blogs = responseObj.feed.entry.map(blogEntry => ({ | |
"title": blogEntry.title.$t, | |
"url": blogEntry.link[4].href | |
})); | |
component.set('v.blogs', blogs); | |
} else { | |
console.log(response.getError()[0].message); | |
} | |
}); | |
$A.enqueueAction(action); | |
} | |
}) |
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.
<aura:application extends="force:slds"> | |
<div> | |
<c:DisplayBlogs></c:DisplayBlogs> | |
</div> | |
</aura:application> |
No comments:
Post a Comment