function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MTBRiderMTBRider 

What does "Database.executeBatch cannot be called from a batch or future method" really mean?

I am  getting this error when trying to call a batch from an after insert after update trigger.  The batch works when called from the scheduler.  I have no @future methods in my org and a Trigger is not a batch method, so anyone knows what the deal is?

Best Answer chosen by Admin (Salesforce Developers) 
MarceldeBMarceldeB

Hi, could it be that you update the same objecttype your trigger is based on within your batchjob? this would then cause the trigger to fire again, but now from within the batch job.

All Answers

MarceldeBMarceldeB

Hi, could it be that you update the same objecttype your trigger is based on within your batchjob? this would then cause the trigger to fire again, but now from within the batch job.

This was selected as the best answer
MTBRiderMTBRider

That looks to have been the problem.  I know the documentation on batches states that you need to be cautious not to get in a loop where the batch causes a trigger to fire which then recalls the batch.  That was not my situation.   I knew that the batch would fire the same object trigger that originally fired the batch, but I had logic in the trigger that would prevent the batch from being called again.  Apparently that is not good enough and if your batch is called from an object's trigger, you cannot re-enter the trigger again while the batch is executing even if you have logic that prevents looping.  The Apex documentation is not clear on this and the error message (ie. "cannot be called from a batch or future method") is useless.

 

Thanks for your suggestion!