https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm[There is a limit] to the number of batch jobs which can be executed at the same time. Executing batch jobs in triggers or in loops is complex as it can easily reach this limit (example: after a bulk update), thus it is recommended to avoid it. Note that checking if the limit is already reached is not enough as nothing prevents the addition of a job between the check and the subsequent call to ``++executeBatch++``.


This rule raises an issue when \``++Databasee.executeBatch()++`` is called in a Trigger or in a loop.


== Noncompliant Code Example

----
trigger MyBatchTrigger on Account (after insert) {
    MyBatch batch = new MyBatch();
    Database.executeBatch(batch);  // Noncompliant
}

public class MyBatchLoop {
    static void createManyBatch() {
        for (integer i = 0; i < 1000; ++i) {
            MyBatch batch = new MyBatch();
            Database.executeBatch(batch);  // Noncompliant
        }
    }
}
----


== See

* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm#apex_batch_best_practices[Using Batch Apex]

