Skip to content

Instantly share code, notes, and snippets.

@rsoesemann
Created March 12, 2019 10:53
Show Gist options
  • Save rsoesemann/abdba8fa87e20b2b6da4af387c62b134 to your computer and use it in GitHub Desktop.
Save rsoesemann/abdba8fa87e20b2b6da4af387c62b134 to your computer and use it in GitHub Desktop.
Batch that can do every work
public class WorkBatch implements Database.Batchable<Work>, Database.Stateful {
private StateInfo state = new StateInfo();
// PUBLIC
public List<Work> start(Database.BatchableContext context) {
return new List<Work>{ new AccountWork(), new ContactWork(), new OpportunityWork() };
}
public void execute(Database.BatchableContext context, List<Work> scope) {
scope[0].execute(state);
}
public void finish(Database.BatchableContext context) {
if(!finished()) {
Database.executeBatch(new WorkBatch());
}
}
// INNER
public class StateInfo {
public List<Account> accounts = new List<Account>();
public List<Contact> contacts = new List<Contact>();
public List<Opportunity> opps = new List<Opportunity>();
}
public interface Work {
void execute(StateInfo state);
}
public class AccountWork implements Work {
void execute(StateInfo state) {
// ...
}
}
public class OpportunityWork implements Work {
void execute(StateInfo state) {
// ...
}
}
public class ContactWork implements Work {
void execute(StateInfo state) {
// ...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment