3
votes

My production environment now has a good number of triggers and classes. These all work great and are functioning as they should. However, I am unable to deploy a couple of new triggers because the test classes are calling too many future methods. I assure you that my code is bulkified so as to only call a future method one time per run. However, when a trigger is being deployed through the IDE, every test is run and as a result the future calls are being run too many times.

I have tried places a try/catch around all of my future calls hoping that if it did hit the limit it would just route to the catch method. It still fails the deployment though with the same error.

The main future call that I am making is only reference-able through one class. It is an HTTP call which pings my website.

Are there any methods to avoid this limit, short of completely re-doing all of my test classes? As you can see below, the excess future calls are occurring on (default) and not on a specific trigger.

10:39:15.617|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 85 out of 100 ******* CLOSE TO LIMIT
Number of query rows: 1474 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 19 out of 150
Number of DML rows: 23 out of 10000
Number of script statements: 2370 out of 200000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 11 out of 10 ******* CLOSE TO LIMIT
1

1 Answers

2
votes

Without looking at the specific code, it is difficult to tell what paths are causing you to reach the future calls limit during testing. It would be worth exploring why the components in the current deployment are hitting the future method limit.

If your objective is to get the tests to pass, you could use a combination of Test.isRunningTest(), Limits.getFutureCalls() and Limits.getLimitFutureCalls() so that the future methods aren't invoked during testing when they would otherwise cause the limit to be exceeded.

E.g.

 if(Test.isRunningTest() && Limits.getFutureCalls() >= Limits.getLimitFutureCalls()) {
     system.debug(LoggingLevel.Error, 'Future method limit reached. Skipping...');
 } else {
     callTheFutureMethod();
 }