In my custom object ERT_Case_Type__c,in that i have a check box IsProcessed which is by default false
Now I need to make this IsProcessed Flag set to True whenever a batch Job is executed ,here is Pseudo code, now what i am looking is ,what exact changes do i need to make in below pseudo batch apex code to make this code work with IsProcessed Flag set to True after every batch processed
global class copyertbatch6am implements Database.Batchable {
global Database.QueryLocator start(Database.BatchableContext BC) {
// collect the batches of records or objects to be passed to execute
String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c FROM ERT_Case_Type__c where createddate = today and IsProcessed Flag = False';
System.debug('ERT Case No is =====>' +query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<ERT_Case_Type__c> exeList) {
// process each batch of records
List<Case_Type__c> listCTD = new List<Case_Type__c>();
System.debug('ERT Case No is =====>' +exeList);
for(ERT_Case_Type__c exe : exeList)
{
listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
IsProcessed Flag = True
}
try {
System.debug('ERT Case No is =====>' +listCTD);
insert listCTD;
} catch(Exception e) {
System.debug(e);
}
}
global void finish(Database.BatchableContext BC) {
// execute any post-processing operations
}
}
Your help is higly appreciated
Thanks and Regards,
Carolyn
I would implement Database.Stateful in this instance. Sorry, SO didn't like the line breaks.
global class ertcopybatch3pm implements Database.Batchable<sObject>, Database.Stateful {
private List<ERT_Case_Type__c> processedRecords;
global Database.QueryLocator start(Database.BatchableContext BC) {
processedRecords = new List<ERT_Case_Type__c>();
// collect the batches of records or objects to be passed to execute
String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c FROM ERT_Case_Type__c where createddate = today and IsProcessed__c = False';
System.debug('ERT Case No is =====>' +query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<ERT_Case_Type__c> exeList) {
// process each batch of records
List<Case_Type__c> listCTD = new List<Case_Type__c>();
System.debug('ERT Case No is =====>' +exeList);
for(ERT_Case_Type__c exe : exeList)
{
listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
exe.IsProcessed__c = true;
}
try {
System.debug('ERT Case No is =====>' +listCTD);
insert listCTD;
//only successful batches will be processed in the finish() method
processedRecords.addAll(exeList);
} catch(Exception e) {
System.debug(e);
}
}
global void finish(Database.BatchableContext BC) {
// execute any post-processing operations
update processedRecords;
}
}