Utilizing The Continuation Method In Lightning Web Components LWC Involves The Following Steps:

Continuation Class

The Continuation Class facilitates asynchronous callouts via REST and SOAP services.

Continuation process executes “in the background” without the user having to wait for the response.

Rephrased:

Continuation integration comprises two primary components:

  • Constructing a message and sending the request.
  • Retrieving the response and returning it to the page.

Steps for implementing the continuation method invoked when a Lightning Web Component (LWC) loads:

Step 1 – Prior to initiating calls to an external web service, it’s essential to add the callout URL to the remote site in the Salesforce user interface. Here, the name of the remote site URL is set as CALLOUT_URL.

Step 2 – To execute a callout, develop an Apex method that yields a Continuation object with continuation set to true, along with the @AuraEnabled annotation.

@AuraEnabled(continuation=true)
public static Object sendRequest() {
 // Create continuation and set timeout in seconds.
 Continuation con = new Continuation(40);
 //More code 
 return con;
}


In this instance, the sendRequest() method will yield the Continuation object, and the response will be received by the callback method getResponse().

Step 3 – Assign an Apex callback method to the continuationMethod property, which will be invoked once the callout concludes. In this case, the callback method is getResponse.

con.continuationMethod='getResponse';


Step 4 – Establish a callout endpoint by incorporating the HttpRequest object into the Continuation object.

Note – A single Continuation object can accommodate a maximum of three callouts.

HttpRequest req = new HttpRequest();
 req.setMethod('GET');
 req.setEndpoint(CALLOUT_URL);
 con.addHttpRequest(req);


Step 5 – Assign data to be passed to the callback method within the state property of the Continuation object. The state property is of type Object.

con.state='This is state from request method';

Step 6 – Implement the logic within the Apex callback method. Upon the completion of all configured callouts, the Apex callback method, getResponse, is triggered. This callback method can access two parameters:

labels – A list of labels, one for each request within the continuation. These labels are generated automatically.

state – The data set in the state property of your Continuation object.

public static Object getResponse(List<String> labels, Object state)

Step 7 – Retrieve the response for each request within the continuation. For instance:

HttpResponse response = Continuation.getResponse(labels[0]);

…and deliver the outcomes to the JavaScript file of the component.

Apex Class with Continuation

public with sharing class ContinuationSampleController {
 //Action method
 @AuraEnabled(continuation=true)
 public static Object sendRequest() {
 Continuation con = new Continuation(40);
 con.continuationMethod='getResponse';
 HttpRequest req = new HttpRequest();
 req.setMethod('GET');
 req.setEndpoint('CALLOUT_URL');
 con.addHttpRequest(req);
 con.state='This is state from request method';
 return con;
    }
 // Callback method
 @AuraEnabled
 public static Object getResponse(List<String> labels, Object state) {
 HttpResponse response = Continuation.getResponse(labels[0]);
 String result = response.getBody();
 return result;
    }
}

To invoke the apex continuation method in LWC, utilize the new import syntax along with the apexContinuation tag.

This import statement informs the framework that we are invoking an Apex method capable of returning a continuation, specifically the sendRequest method.

Below is the JavaScript file for the component.

import { LightningElement, track } from 'lwc';
import sendRequest from '@salesforce/apexContinuation/ContinuationSampleController.sendRequest ';
export default class ContinuationSample extends LightningElement {
    @track listOfRecords={};
    @track error;
 connectedCallback() {
 sendRequest()
            .then(result => {
 this.listOfRecords = result;
 this.error = undefined;
            })
            .catch(error => {
 this.error = error;
 this.listOfRecords = undefined;
            });
    }
 
}

HTML Code

<!-- ContinuationSample.html -->
<template>
 <div>
        Show result: {listOfRecords}
 </div>
</template>