1

I know we can't call future method from a batch class or from any future methods. "Future method cannot be called from a future or batch method" I have an Account trigger which has a lot of future calls in the code. Now I have to create a batch job on account which will definitely invoke my trigger which has future calls, Is this something allowed?.

From the batch if it calls the trigger which has future call, is this is allowed?

I saw couple of post where we are advised to use "system.isBatch() || system.isFuture()" these commands, but the problem is I want my batch processing to use the trigger logic.

If anyone can help me with this.

3

From your description, you're not calling the trigger directly. It executes as a result of DML operations that occur in your batch class. In that context, any future calls that are created by the trigger would seem to spawn a new thread. If you were calling a class directly that called an @future method, that would definitely be considered as being invoked directly from the execution context your batch class is running in. Calling the @Future method after the DML operation from the trigger is less clear. But, as you know, you can avoid the trigger running by checking the context.

If you want to use the logic in your trigger that doesn't call the @Future method, I'd recommend you create a helper class your trigger calls for that logic (you don't say how your trigger is structured). The logic in that helper can then be called from your batch class without invoking the @Future methods and you'll know you're "safe" for everything that's running in your batch context.

If that's not possible and you do need the asynchronous processing of additional logic as performed in your trigger, I'd recommend you incorporate it in 2nd batch class that runs on completion of the batch class you're currently writing by calling it from the Finish Method.

  • 1
    We all love the basics, right ;) – Mr.Frodo May 20 '18 at 11:45
  • @crmprogdev: Thanks for the clarification. So you meant that if my trigger invokes any future methods that should be fine as it is a different context from the batch apex invocation. Is that understanding correct ?? Secondly, I cant use helper because I need the future methods to get executed while the batch is running. I am just thinking from the finish method Can I call the future method if thats permitted? I dont want to create another batch class and so same processing. Thanks – Quddus May 20 '18 at 23:14
  • @Quddus The future methods from your triggers would likely be problematic, especially if you'll be referencing the records again in your batch class. If you need the logic from your triggers and it involves the future methods, you should rewrite them it run as a batch class that's called from your finish method. You can call the non future portion from within the batch you're writing (will return to your batch), but anything more should be called from the finish method. – crmprogdev May 21 '18 at 12:51
1

Basically when you are running a batch you don't need the future method, a batch is also an asynchronous job.

So what you need to do is write your code like this that if a batch runs and make a trigger to run then call a non-future method which do the same as what your future method does.

If a trigger runs directly then call your future method.

As you already know you can find context with the help of system.isBatch() || system.isFuture()

  • @mrfrodo: From the above post, I understand that if my trigger invoke as a result of any DML, it can continue to execute the future method since that is in the different context. – Quddus May 20 '18 at 23:17
  • @quddus Like I said in my answer , you just need to have a non future method which does the same job as your future method does now first you will check the context, if request is coming from batch than call non future method otherwise call future method. – Mr.Frodo May 21 '18 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.