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
Abhishek Sharma 527Abhishek Sharma 527 

batch process with try-catch block

Hello There, I have created batch class for Lead object, it's working but I  want to add try-catch block in that, it showing error when I placed these blocks, I'm not able to figure out right place to write these block maybe. can anyone plz help in this.

//this is my code

global class BatchDemo_Lead implements Database.Batchable<sObject>{
    try{
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([Select id from Lead]);
    }
        
    }
    
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        
        for(Lead l : scope)
        {
            if(l.State == 'VA'){
            l.State = 'NJ';
        }
        }
        update scope;
        }
    
    global void finish(Database.BatchableContext bc){
        system.debug('Batch process finished.');
    }
    catch(Exception e){
           return Database.getQueryLocator(Select id, State from Lead);
            System.debug('DML operation executed from catch block');
    }
    }
    
Best Answer chosen by Abhishek Sharma 527
ravi soniravi soni
hi Abhishek,
have you been solved your query? if yes, please let us know by marking it as the best answer.
Thank you

All Answers

PriyaPriya (Salesforce Developers) 
Hey Abhishek,

You cannot put try catch block like this. You are putting all the methods in try block which is wrong. 

try this way :- 
global class BatchDemo_Lead implements Database.Batchable<sObject>{
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([Select id from Lead]);
    }
        
   
    
    global void execute(Database.BatchableContext bc, List<Lead> scope){
 try{
        
        for(Lead l : scope)
        {
            if(l.State == 'VA'){
            l.State = 'NJ';
        }
        
        update scope;
}catch(Exception e) {
            errors.add(e);
        }
        }
    
    global void finish(Database.BatchableContext bc){
        system.debug('Batch process finished.');
    }
    
    }

For more reference :- 
https://salesforce.stackexchange.com/questions/113775/error-handling-batch-apex
Kindly mark it as the best answer.

Thanks,
Priya Ranjan
ravi soniravi soni
Hi Abhiskek,
you can try below code.
global class BatchDemo_Lead implements Database.Batchable<sObject>{
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([Select id from Lead]);
        
    }
    
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        try{
        for(Lead l : scope){
            if(l.State == 'VA'){
                l.State = 'NJ';
            }
        }
        
        update scope;    
        }
        catch(DMLException  ex){
            system.debug('ex=====> ' + ex.getMessage());
            
        }
            
        }
   global void finish(Database.BatchableContext bc){
        system.debug('Batch process finished.');
    }
   }

don't forget to mark it as the best answer.
Thank you
ravi soniravi soni
hi Abhishek,
have you been solved your query? if yes, please let us know by marking it as the best answer.
Thank you
This was selected as the best answer
Abhishek Sharma 527Abhishek Sharma 527
thanks ravi, yes query resolved.