Skip to content

Instantly share code, notes, and snippets.

@xgeek-net
Created January 8, 2017 11:07
Show Gist options
  • Save xgeek-net/1db1a9e39571cbaccb89c6c5153d609e to your computer and use it in GitHub Desktop.
Save xgeek-net/1db1a9e39571cbaccb89c6c5153d609e to your computer and use it in GitHub Desktop.
SampleBatch
global class SampleBatch implements Database.Batchable<sObject> {
// The batch job starts
global Database.Querylocator start(Database.BatchableContext bc){
String query = 'SELECT Id, Name FROM BatchData__c LIMIT 1000';
return Database.getQuerylocator(query);
}
// The batch job executes and operates on one batch of records
global void execute(Database.BatchableContext bc, List<sObject> scope){
System.debug('>>> execute start at ' + DateTime.now().format('yyyy/MM/dd hh:mm:ss'));
// Reference: https://www.xgeek.net/salesforce/a-way-to-make-thread-sleep-in-apex/
Long startTime = DateTime.now().getTime();
Long finishTime = DateTime.now().getTime();
while ((finishTime - startTime) < 5000) {
//sleep 5s
finishTime = DateTime.now().getTime();
}
System.debug('>>> execute end at ' + DateTime.now().format('yyyy/MM/dd hh:mm:ss'));
}
// The batch job finishes
global void finish(Database.BatchableContext bc){
AsyncApexJob job = [SELECT Id, Status FROM AsyncApexJob WHERE Id = :bc.getJobId()];
System.debug('>>>> finish ' + job.Status);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment