2

I have actually a batch on user, where user are activated or deactivated based on a criteria. In each execute I update my set of users, which in turn launch an @future method to recalculate the role for each user. The method need to be @future and is executed on trigger for user after update. The problem is that this generate the following error :

 System.AsyncException: Future method cannot be called from a future or batch method:

I understood that as each execute is considered as a future call, I cannot execute another future from my trigger in each batch execute. So i decided to update all my users in my finish, which will call my future method, but now I am obtaining the following error :

System.LimitException: Too many future calls: 1

Does this mean that will not be able to execute a future method from a batch ??? If not, then how can I achieve this?

This is not a duplicate of Calling future method from batch class?, because there it is doing callouts whereas in my case, I am recuring to @future because in a user trigger i cannot do any other DML. Also their hasn't been any best answer marked there.

vanessen
  • 2,645
  • 3
  • 37
  • 60
  • 1
    You can go for batch class chaining by starting another batch from finish method, which will handle the same logic that you want in the future method. – Saumya Ranjan Satapathy Feb 15 '17 at 08:13
  • @RohitMourya, can you place your comment as answer instead, such that if it works , i can mark it as best answer. Because I actually implemented your suggestion on my sandbox and it is working. I will now deploy it to my prod where i was getting the issues. – vanessen Feb 15 '17 at 13:03
  • @vanessen done. I'm glad that my suggestion solved your problem. – Rohit Mourya Feb 15 '17 at 13:07

1 Answers1

2

You can implement Queueable class which acts as a Future method and then you can invoke other future method. This is indirect way of calling future method from future method (A queueable class method acts as a future method). But this approach also has governor limit of 2. So at max you can call two future methods.

Rohit Mourya
  • 3,861
  • 2
  • 15
  • 23