Created
January 8, 2017 11:16
-
-
Save xgeek-net/642de18d3342e725c0724fdf8913a3ed to your computer and use it in GitHub Desktop.
SampleBatchWithState batch Apex
This file contains hidden or 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
global class SampleBatchWithState implements Database.Batchable<sObject>, Database.Stateful { | |
public Integer publicValue; | |
private Integer privateValue; | |
global SampleBatchWithState() { | |
publicValue = 0; | |
privateValue = 0; | |
} | |
// 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(); | |
} | |
publicValue += 1; | |
privateValue += 1; | |
System.debug('>>> publicValue : ' + publicValue); | |
System.debug('>>> privateValue : ' + privateValue); | |
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