Last active
December 20, 2015 23:10
-
-
Save martyychang/fd344de39a80de3cd888 to your computer and use it in GitHub Desktop.
Apex class that illustrates how a completely self-contained Batchable actually works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Database.Batchable that can be executed, assuming you have a custom object | |
* named Experiment__c that already exists in your org, with a custom | |
* Text field named Title__c | |
*/ | |
global class BatchableExperiment | |
implements Database.Batchable<Task>, Iterable<Task>, Iterator<Task> { | |
/* | |
* The ID of the Experiment record created for this batch job | |
*/ | |
private Id experimentId; | |
/* | |
* A fun, memorable title that will be repeated to create | |
* subsequent titles. Try to keep the title at 4 or fewer characters | |
* in length. | |
*/ | |
private String experimentTitle; | |
/* | |
* A numeric counter used to keep track of when key Batchable, | |
* Iterable and Iterator methods are called | |
*/ | |
private Integer stepNumber; | |
/* | |
* Default constructor which sticks in an experiment title, "BaFa" | |
*/ | |
global BatchableExperiment() { | |
this('BaFa'); | |
} | |
/* | |
* @param experimentTitle | |
* The value to store in the new Experiment record's Title field | |
*/ | |
global BatchableExperiment(String experimentTitle) { | |
this.experimentTitle = experimentTitle; | |
stepNumber = 0; | |
experimentId = createNewExperiment(experimentTitle); | |
recordNewStepNumber('BatchableExperiment()'); | |
} | |
/* | |
* @param title The Title value for the new Experiment record | |
* @return the Experiment ID of the newly created record | |
*/ | |
private static Id createNewExperiment(String title) { | |
Experiment__c newExperiment = new Experiment__c( | |
Title__c = title); | |
insert newExperiment; | |
return newExperiment.Id; | |
} | |
/* | |
* Create a new Note record associated with the experiment, | |
* to record the execution of a new key step in the batch job | |
* | |
* @param description | |
* A brief description of the step number | |
*/ | |
private void recordNewStepNumber(String description) { | |
stepNumber++; // Move to the next step number | |
String noteBody = 'BatchableExperiment: ' + this + '\n\n'; | |
noteBody += 'Step ' + stepNumber + ': ' + description; | |
Note experimentNote = new Note( | |
Body = noteBody, | |
ParentId = experimentId, | |
Title = 'BatchableExperiment step ' + stepNumber); | |
insert experimentNote; | |
} | |
/* | |
* Implementation of Database.Batchable.execute() | |
*/ | |
global void execute(Database.BatchableContext context, List<Task> records) { | |
recordNewStepNumber('execute(' + records + ')'); | |
} | |
/* | |
* Implementation of Database.Batchable.finish() | |
*/ | |
global void finish(Database.BatchableContext context) { | |
recordNewStepNumber('finish()'); | |
if (experimentTitle.length() < 10) { | |
Database.executeBatch( | |
new BatchableExperiment(experimentTitle + experimentTitle)); | |
} | |
} | |
/* | |
* Implementation of Iterator.hasNext() | |
*/ | |
global Boolean hasNext() { | |
recordNewStepNumber('hasNext()'); | |
return Math.random() >= 0.25; | |
} | |
/* | |
* Implementation of Iterable.iterator() | |
*/ | |
global Iterator<Task> iterator() { | |
recordNewStepNumber('iterator()'); | |
return this; | |
} | |
/* | |
* Implementation of Iterator.next() | |
*/ | |
global Task next() { | |
recordNewStepNumber('next()'); | |
return new Task(Subject = 'Step ' + stepNumber); | |
} | |
/* | |
* Implementation of Database.Batchable.start() | |
*/ | |
global Iterable<Task> start(Database.BatchableContext context) { | |
recordNewStepNumber('start()'); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment