I was wondering if it is possible to call a batch from a @future
method? The reason I am asking as you may have guessed is because I am trying to call batches from a trigger. Eg: Leads are inserted then call batch after insert trigger on Leads.
I know it is possible to call Database.executeBatch
, however there is a limitation to the number of batches you can send. With current FlexQueue
implementation you can invoke up to 100 batch calls per a transaction.
But my question is what if I need to send more than that per a transaction?
I was thinking of calling a @future
method and in that method I call the batch, but I get this error:
System.AsyncException: Database.executeBatch cannot be called from a batch start, batch execute, or future method.
Pseudocode:
trigger leadTrigger on Lead(after insert) {
//some code
TriggerHelper.callTheBatch();
}
public without sharing class TriggerHelper {
@future
public static void callTheBatch() {
MyBatchClass myBatchClass1 = new MyBatchClass();
Id batchId = database.executebatch(myBatchClass1, 50);
}
}
Any suggestion or idea?