4

We're hitting CPU Time Limit Exceeded issues with a class that's being called by a Process Builder.

From the SF solutions page, it was noted that one fix for CPU Time Limit Exceeded is to make the code asynchronous by adding the @future tag.

So what we did is that the Process Builder calls an @invocablemethod and that method calls a @future method. The class compiles so I assume it might work.

However, are there any issues that might be encountered with a Process Builder calling an invocable method calling a @future method?

Thanks.

7

The only main problem you should be aware of is that your process builder must not call the future method recursively. This would happen because the future method performs some sort of DML operation (usually an update) that causes the process builder to fire again, and calling the future method while already in a future context. As long as you remember to build your criteria properly, or avoid recursively calling your method, you should be okay.

In your invocable method, you can:

if(!System.isFuture()) {
    callFutureMethod();
}

To avoid the potential for infinite recursion.

  • Can we use Queueable apex instead of the above if() condition? – Arnold Jr. Jan 6 at 15:48
  • @ArnoldJr. Yes, but again, you could run into an infinite loop if you're not careful. The only difference is that you'd happily run along until you exhausted the asynchronous limit (250,000/day). The two methods are basically the same in this regard, so choose whichever you prefer. – sfdcfox Jan 6 at 17:07
  • Thanks for the info. If you don't mind, can you please include the best practices we should follow for the Queueable apex? – Arnold Jr. Jan 6 at 17:18
  • @ArnoldJr. Basically, avoid creating infinite loops would be mu advice. Pretty easy to accidentally do, and awfully hard to break such a loop. – sfdcfox Jan 6 at 21:06
  • Hi @sfdcfox, sorry for the late reply. Thank you for your suggestion. It's a great help for our code and it now works! – Jerard Dela Victoria Jan 20 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.