function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

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

Hi Gurus,

In my custom object ERT_Case_Type__c, 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 batch apex code to make this code work with IsProcessed Flag
 
global class copyertbatch6am implements Database.Batchable<sObject> {
  
                    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,
Best Answer chosen by fiona gentry
ANUTEJANUTEJ (Salesforce Developers) 
Hi Fiona,

Can you try the below code once:
 
global class copyertbatch6am implements Database.Batchable<sObject> {
  
                    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);

list<ERT_Case_Type__c> to_update_list = new list<ERT_Case_Type__c>();

                        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));
                            ERT_Case_Type__c temporary = new ERT_Case_Type__c();
                            temporary.id= exe.id;
                            temporary.IsProcessed Flag = True;
                            to_update_list.add(temporary);

                        }
                        
                        try {
                            System.debug('ERT Case No is =====>' +listCTD);
                            insert listCTD;
                            update to_update_list;
} catch(Exception e) {
                            System.debug(e);
                        }
                        
                    }   
                    
                    global void finish(Database.BatchableContext BC) {
                        // execute any post-processing operations
                  }
                }

I have highlighted the changes in bold for understanding.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.