You need to sign in to do that
Don't have an account?
Abhishek 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');
}
}
//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');
}
}
have you been solved your query? if yes, please let us know by marking it as the best answer.
Thank you
All Answers
You cannot put try catch block like this. You are putting all the methods in try block which is wrong.
try this way :-
For more reference :-
https://salesforce.stackexchange.com/questions/113775/error-handling-batch-apex
Kindly mark it as the best answer.
Thanks,
Priya Ranjan
you can try below code.
don't forget to mark it as the best answer.
Thank you
have you been solved your query? if yes, please let us know by marking it as the best answer.
Thank you