Testing Apex InvocableMethods

Testing an InvocableMethod got me stumped for a few hours.

C.E. Lopes
2 min readAug 27, 2022
Poster for the movie The Invoking

TL; DR; Follow best practices and make sure to call the object of your test case, your InvocableMethod, between Test.startTest() and Test.stopTest(), or face the horror of "Your request exceeded the time limit for processing. connection was cancelled here"

Maybe my experience wasn't as chilling as "The Invoking," but I did spend a couple of hours scratching my head as to why the test I wrote for an InvocableMethod was:

  • Taking so long to run (about ten minutes for a simple test)
  • Returning a Pass (in about a second) and a Fail execution for the same test (in about nine excruciating minutes)
The same test case passed and failed!
  • Showing the error: "Your request exceeded the time limit for processing. connection was cancelled here"

In part, best practices dictate that after setting up the data for the test, one should call Test.startTest() and Test.stopTest(). But I've seen (and, if I'm honest, wrote) plenty of tests that don't do it — even Salesforce's Testing Example does not mention it.

My test code ended up looking like this (gold in lines 11 and 15):

Pardon the.java extension to enable syntax highlighting.

It is possible that the error materialized not because of the InvocableMethod annotation but rather because of the data I created for my test (including PriceBook2, PriceBookEntry, and Product). Still, given my experience, adopting test boundary delimitation as recommended in best practices is a safe bet.

--

--

C.E. Lopes

A geek making sense of different technologies and sharing what he learned.