[SalesForce] Alternatives for handling a Long Running Transaction

I have logic inside an Apex Class that will be invoked from a Lightning Component. There are three sequential callouts that will be sent to an External System whenever its method is Called.

As per the Salesforce Governor limits, if a transaction exceeds more than 5 seconds it will be considered a long-running transaction, and there cannot be more than 10 concurrent long-running transactions.

In my case, if each callout took 4 seconds on average, then it would take 12 seconds on average to execute all three callouts. Because this transaction has exceeded 5 secs it will be considered a long-running transaction.

What are the alternative approaches to handle this use case?

Maybe doing an Asynchronous Callout – Continuations method will help. The Continuations method is not yet supported directly from lightning. But we can invoke apex Continuations from lightning as mentioned in this blog.

Appreciate your Suggestions

Best Answer

That's tricky. The first thing that comes to mind is to do it asynchronously and find a way to "subscribe" to the response. You can make use of Platform Events for this.

  1. Call your Apex class and either pass in or have it return a unique Id for the transaction.
  2. Run your sequential callouts in @Future context from your Apex class.
  3. When the transactions are complete, fire a platform event from your Apex class that includes the unique Id.
  4. In your lightning component, use the lightning:empApi to subscribe to to the Platform Event.
  5. When the event message containing the correct Id comes through, render the info on your component.

Note: You may have to watch out for Platform Event daily limits if your expected volume could reach 50,000.

Related Topic