== Why is this an issue?

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

[source,apex]
----
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
        }
    }
}
----


== Resources

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


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Remove this batch job execution


'''
== Comments And Links
(visible only on this page)

=== on 9 Oct 2019, 17:27:58 Nicolas Harraudeau wrote:
*Out of Scope:* ``++System.scheduleBatch++`` may also reach the limit of batch jobs, but it is not possible to schedule two jobs with the same name. Thus it would be necessary to check if many jobs are scheduled or if the trigger only tries to schedule a job when it isn't already scheduled.

endif::env-github,rspecator-view[]
