Many a times there are requirements where we have to integrate our Salesforce platform with an external system for effective and efficient flow of data to & fro and we can do this by Apex Callout’s, which enables our Salesforce org to tightly integrate our Apex with an external service by making a call to an external Web service or sending a HTTP request from Apex code and then receiving the response. Apex provides integration with Web services that utilize SOAP and WSDL, or HTTP services (RESTful services).
While implementing such services, there are few roadblocks that are in place for making callouts from Apex. And due to the Salesforce’s unblemished Multi-tenant architecture, one has to be very cautious while developing such critical functionalities so that we are not holding on to a resource unnecessarily.
As per Salesforce: the number of concurrent long-running requests per organization is allowed a maximum of ten concurrent requests running for longer than five seconds. When that limit is reached, subsequent requests fail until an existing request completes.
Apex Continuations (also referred to as Asynchronous callouts) can help in overcoming such roadblocks by allowing you to invoke long-running services without unnecessarily consuming up the server resources and running into Apex limits. Through Continuation approach, we can call external web service and inform that which method for callback should be used to process the response.
Although Apex continuations are not currently supported in the Lightning Component Framework, but, you can always use the custom solution described in this article to make asynchronous Apex callouts from within Lightning Components.
However, there is currently no built-in support for continuations in Lightning Components i.e. you can’t call an @AuraEnabled method that makes an asynchronous callout, but, you can make use of a Visualforce page as a proxy to invoke Apex continuations from Lightning Components.
According to this approach:
- The Lightning Component embeds a Visualforce page (in an iframe).
- Making the use of standard postMessage() API, we can safely enable the cross-origin communication between the page and iframe embedded within it.
- And once the request is complete, the VF page makes use of the same window.postMessage() API to pass the result back to its Lightning Component container.
Through the above stated approach, you can overcome the issue of calling long-running services without blocking server threads and running into Apex limits and as stated earlier, we cannot use asynchronous callouts in Lightning, but through this approach, without much hassle, you can easily communicate to an external system in Lightning platform.