If I were to make a callout using Apex class when the request is initiated from a Lightning Web Component then I will be reaching out to a concept called Apex Continuation.
The purpose of Apex Continuation is to house code that takes more time when compared to the usual requests.
We will be trying to make a HTTP callout so make sure you add the endpoint in the Remote Site Settings. This is a mandate.
Here is the template file of the web component.
In this example am trying to invoke the Apex method imperatively.
The Apex method has to be annotated with @AuraEnabled(cacheable=true continuation=true) and it's a mandate to include continuation=true.
We are instantiating the class Continuation and specifying the timeout as a param.
We also need to specify the callback method name such that it gets invoked once the callout is made and after the response is sent.
If I were to send data from the method to callback method I can do it using state property in Continuation class.
After that I can frame a request with method name and endpoint, but, we are not invoking the send method, we are padding the requests and adding it to the addHttpRequest() method in Continuation class.
We can have up to three requests that are added up to the method addHttpRequest()
In the callback processResponse(List labels, Object state) labels are going to have the response of the requests added in the method addHttpRequest() and state is going to have the info that we pass from the Apex method.
Subscribe to Salesforce Casts
Get the latest posts delivered right to your inbox