Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Cancel an Asynchronous Request in a Flow Local Action
If an asynchronous request times out, the flow executes the local action's
fault connector and sets $Flow.FaultMessage to the error message. However,
the original request isn't automatically canceled. To abort an asynchronous request, use the
cancelToken parameter available in the invoke method.
Example
In this client-side controller, the invoke method returns a Promise. When the method has done all it needs to do, it completes the call and control returns to the flow.
- If the request is successful, the method uses resolve() to execute the next element in the flow after this action.
- If the request isn't successful, it uses reject() to execute the local action’s fault connector and sets $Flow.FaultMessage to “My error message”.
- If the request takes too long, it uses cancelToken.promise.then to abort the request.
1({
2 invoke : function(component, event, helper) {
3 var cancelToken = event.getParam("arguments").cancelToken;
4
5 return new Promise(function(resolve, reject) {
6 var xhttp = new XMLHttpRequest();
7
8 // Do something, like get data from
9 // a database behind your firewall
10 xhttp.onreadystatechange = $A.getCallback(function() {
11 if (/* request was successful */) {
12 // Complete the call and return to the flow
13 resolve();
14 } else {
15 reject(new Error("My error message"));
16 }
17 });
18
19 // If the Promise times out, abort the request and
20 // pass set $Flow.FaultMessage to "Request timed out"
21 cancelToken.promise.then(function(error) {
22 xhttp.abort();
23 reject(new Error("Request timed out."));
24 });
25
26 });
27 }
28})