Wednesday, May 7, 2014

APEX Test class failing due to @Future Calls limits


Most recently we faced a issue with @Future calls with governor limits going over 10 in Test Class execution during deployments. During investigation we found it to be due to test data being created more than 10 times and every test data is initiating the method with @Future call. So in the end the governor limit is crossing. We thought of all options like removing @Future call altogether as an alternative, but the best solution for these type of issues where you cannot afford to remove @Future call is  -  to put below line of code wherever methods with @future call are triggered.

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

    CallMethodWithFutureCall();