Search code examples
salesforceapexapex-codesalesforce-lightningsoql

How To Make IsProcessed Flag Is Set To True for records whenever a batch Job is executed?


enter image description hereIn 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


Solution

  • 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;
                        }
                    }
    
    1. Implement Database.Stateful in the class definition
    2. Declare a private property to store the processed records upon class instantiation
    3. In the start() method initialize an empty list so you can add items to it
    4. In the execute() method set the flag on the successfully-processed records and add them to the list
    5. In the finish() method update all successfully-processed records from all batches at once