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
SFDC New learnerSFDC New learner 

How to handle more than one record with batch

Hi All,

I am trying to execute a batch which needs to handle more than one record.
My batch will run daily and checks for the status scheduled and batch rundate should be today. Then  I am getting the email template picklist and a related case in it and updating the case picklist field. I am identifying the records which are needed to be updated and looping those list of records and getting the caseid.
Now if there are multiple records that are scheduled on the same day which are having different caseID's and with different email templates. How to handle more than one record using batch?
Below is my code:
global class BatchClassSample implements Schedulable,Database.Batchable<SObject> {
  
    Date todaysdate = system.today() ;
    
    global database.QueryLocator start(Database.BatchableContext bc){
        return database.getQueryLocator('SELECT Id,Name,Case__c,Email_Template_Name__c,Email_Template__c from Email_Notification__c where Status__c =\'Scheduled\' AND Scheduled_Date__c = :todaysdate');
        
    }
    
    global void execute(Database.BatchableContext bc, List<Email_Notification__c> scope){
        list<Case> updateReminderFlag = new list<Case>();
        Map<String,Email_Notification__c> UpdateReminderFlagmap = new Map<String,Email_Notification__c>();
        Map<Id,Case> casemap = new Map<Id,Case>();
        Set<id> caselist = new set<id>(); 
       
        system.debug('scope'+scope);
        for(Email_Notification__c sc : scope){
            caselist.add(sc.case__c);
            UpdateReminderFlagmap.put(sc.Email_Template__c,sc);
        }
        List<Case> c1 = [select id,Email_Template__c from case where id IN :caselist];
        
        for(Email_Notification__c em : UpdateReminderFlagmap.values()){
           
            for(Case c :c1){
               
                if(em.Email_Template_Name__c != NULL){
//put the email template in case and update the case picklist
                    c.Email_Template__c = em.Email_Template__c;
                    
                    updateReminderFlag.add(c);
                    
                }
            } 
        }
        
        casemap.putall(updateReminderFlag);
        if(casemap.size()>0)
            
            Update casemap.values();
            
        } 
    global void finish(Database.BatchableContext bc){
        
    }
    global void execute(SchedulableContext scon) {
      Database.executeBatch(new BatchClassSample(),2000);
   }
}

will my code works for multiple records also.Please anyone help me on this.
Any help will be greatly appreciated.
Thanks,
Sirisha
@anilbathula@@anilbathula@
Hi Sirisha,

Your code wont worry for multiple records as you are using map of (sc.Email_Template__c,sc ) if all the records have same template it works only for one record ,so change it to list it will work for bulk records.
use the below code.
global class BatchClassSample implements Schedulable,Database.Batchable<SObject> {
  
    Date todaysdate = system.today() ;
    
    global database.QueryLocator start(Database.BatchableContext bc){
        return database.getQueryLocator('SELECT Id,Name,Case__c,Email_Template_Name__c,Email_Template__c from Email_Notification__c where Status__c =\'Scheduled\' AND Scheduled_Date__c = :todaysdate');
        
    }
    
    global void execute(Database.BatchableContext bc, List<Email_Notification__c> scope){
        list<Case> updateReminderFlag = new list<Case>();
        list<Email_Notification__c> UpdateReminderFlagmap = new list<Email_Notification__c>();
        Map<Id,Case> casemap = new Map<Id,Case>();
        Set<id> caselist = new set<id>(); 
       
        system.debug('scope'+scope);
        for(Email_Notification__c sc : scope){
            caselist.add(sc.case__c);
            UpdateReminderFlagmap.add(sc);
        }
        List<Case> c1 = [select id,Email_Template__c from case where id IN :caselist];
        
        for(Email_Notification__c em : UpdateReminderFlagmap){
           
            for(Case c :c1){
               
                if(em.Email_Template_Name__c != NULL){
//put the email template in case and update the case picklist
                    c.Email_Template__c = em.Email_Template__c;
                    
                    updateReminderFlag.add(c);
                    
                }
            } 
        }
        
        casemap.putall(updateReminderFlag);
        if(casemap.size()>0)
            
            Update casemap.values();
            
        } 
    global void finish(Database.BatchableContext bc){
        
    }
    global void execute(SchedulableContext scon) {
      Database.executeBatch(new BatchClassSample(),2000);
   }
}

Thanks
Anil.B