4

I am still new in Salesforce and have Java background.There some governor limit that I totally not sure what to do.

The issue started when I need to update the Account object with the value that I retrieved from Google API that required me to do @future.I completed this until I get this error during testing:

Future method cannot be called from a future

I figured out there a trigger that fired on Account using @future, and it from other application and another purpose,it call external API.So when I call my @future and want to update the Account, there will be other trigger also calling @future.

Here the figure that might illustrate how it connected: enter image description here I am not sure what to do, if I do checking system.isFuture(), it will only check whether it is future running or not, but what can I do to make my future call also run?

So can I say, one object only can call one @future?So is there scenario in Salesforce whereby the object need to update with value from several external API?

1 Answer 1

3

No, you can call up to 10 subsequent future calls from 1 single transaction (apex invocation from a class/trigger/webservice), although you can't call a future method within a future method. So you can't have a trigger for an example that will call some future method, and inside that method calling another future method.

I guess in your case you're trying to do another future callout from within the method that is already annotated as @future and it's being called from another class/trigger?

Here you can find more information around the governor limits, and also the Limits class which you can utilise to get the number of methods with the future annotation that have been executed (Limits.getFutureCalls())

UPDATE:

Based on the updated question, you've got a @future method that does an update of an account, which triggers another @future call within the trigger on account update. This causes a 2nd @future call to be made within the same context. What you need to do is to prevent the trigger on account from firing when you're updating the account record from your google API @future method. You can do that by creating a static boolean variable that you will set to true in your class, and then inside the trigger you will check against that variable - execute the trigger only if this variable is set to false (which will be the default value at all times, except for your google API @future method where you're going to set it to true).

6
  • Thanks @Bachovski, I edited the question and illustrate how the structure currently.I got the exception with the structure.Please advise.
    – unidha
    Commented Aug 20, 2014 at 5:33
  • Ah right. So when your class updates the account that causes the trigger to run. And because your class is a future, the trigger is still in the future context so it can't call the other future method within itself. What you need to do is prevent that trigger from firing when you update the account in your class. Commented Aug 20, 2014 at 5:44
  • Well...if I prevent that trigger from firing,when it be fired or will it not be fired forever.My concern that trigger is using by other application.
    – unidha
    Commented Aug 20, 2014 at 5:45
  • No, you need to stop the trigger only when you're updating the account from your code, from your @future method. Something similar to this - raydehler.com/cloud/clod/…. Create a static boolean in your class and set it to true whenever you update the account, and then in the trigger say if your variable is false, only then run the code. This will prevent the trigger from firing only in your case when you set that variable to true. At all other times it will be false and it will run. Commented Aug 20, 2014 at 5:47
  • Well, thanks a lot.Using static boolean is working like charm.Do you mind to paste your comment into Answer because I am dying to Accept it as answer?Thanks again.
    – unidha
    Commented Aug 20, 2014 at 6:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .