Apex Chainable Batch Codacy Badge

Deploy to Salesforce

Apex Batches can be chained by calling the successor batch from the finish() method of the previous batch. But such hardcoding makes this model inflexible. It's hard to build the chain from outside, neighter from a central class nor on runtime dependant on business logic.

With Chainable

The Chainable wrapper class of this repository overcomes those drawbacks.

  • No need to hardcode successor batch in finish() method
  • Created batch chains of arbitrary length without changing existing Batch classes
  • Allows asynchronous and synchronous testing of Batch chains
      new SecondBatch()
            .then(FirstBatch())
            .then(ThirdBatch())
            ...
            .execute();

Without Chainable

class FirstBatch implements Batchable<SObject> {
    Iterator<SObject> start(BatchableContext ctx) { ... }

    void execute(BatchableContext ctx, List<Account> scope) { ... }

    void finish(BatchableContext ctx) {
        Database.enqueueBatch(new SecondBatch()); 
    }
}
class SecondBatch implements Batchable<SObject> {
    Iterator<SObject> start(BatchableContext ctx) { ... }

    void execute(BatchableContext ctx, List<Account> scope) { ... }

    void finish(BatchableContext ctx) {
        Database.enqueueBatch(new ThirdBatch()); 
    }
}

Apex Chainable Batch

Chain Batches in a readable and flexible way without hardcoding the successor.

Apex Chainable Batch Info

⭐ Stars 15
🔗 Source Code github.com
🕒 Last Update 10 months ago
🕒 Created 2 years ago
🐞 Open Issues 0
➗ Star-Issue Ratio Infinity
😎 Author rsoesemann