I'm having an issue where we have some code that can be called anywhere and then it makes an @future call to log a message to an external system.
The problem is if this method is called (either directly or indirectly) in a Controller Extension a System.LimitException
is thrown saying @future call currently not allowed
I know you can check to see the number of allowed future calls with Limits.getLimitFutureCalls()
and I know you can see how many you've made with Limits.getFutureCalls()
and see if it's batch with with System.isBatch()
or is in future call with System.isFuture()
. Is there any other way to know if you cannot make a future call?
My goal would be to check to see if the @future call can be made, and if not skip the method call.
NOTE: You cannot make @future calls from get/set/constructor in the Controller extension and metioned in the documentation
Example of bad code
Class
public class GenericUtils {
public static void reportException(Exception e) {
//Limits.getFutureCalls() returns 0
//Limits.getLimitFutureCalls() return 10
//Make @future call to log
System.debug(e.getMessage());
}
}
Controller
public with sharing class ControllerExtension {
public ControllerExtension(ApexPages.StandardController cont) {
//Do stuff
}
public getMyVariable() {
try {
//Do more stuff
} catch (Exception e) {
GenericUtils.reportException(e); //This throws the LimitException
}
}
}
Page
<apex:page standardController="Account" extensions="ControllerExtension">
<apex:form id="theForm">
<apex:inputText value="{!myVariable}" />
</apex:form>
</apex:page>