You need to sign in to do that
Don't have an account?
venkateshyadav1243
Error Handling on batch apex class csv file.
Hi All,
Am trying to handle errors in my csv file before processing batch class
can any one please tell me how to handle errors in batch class and send error list to user email.
I written batch class for inset and update records its working fine,but now i have to handle few validation rule before insert/update and if any record is contain proper value ,i have to stop the batch process can one please help me how can i do this.
Below is my batch apex class.
global class CustomIterableBatchForGSSAccount implements Database.Batchable<String>,Database.Stateful{
global Map<Id, String> errorMap {get; set;}
global Map<Id, SObject> IdToSObjectMap {get; set;}
Public String CSVFile;
Set<string> strset=new Set<string>();
Map<string,id> Offmap=new map<string,id>();
Public List<GSS_Split__c> Gsstoupload;
Public List<GSS_Split__c> Gsstoupdate;
Public List<GSS_Split__c> Gsssize;
Public String Errormsgs;
//Constructor to hold the uploaded CSV file
global CustomIterableBatchForGSSAccount(String CSVFile){
this.CSVFile= CSVFile;
errorMap = new Map<Id, String>();
IdToSObjectMap = new Map<Id, SObject>();
}
//Start Method
global Iterable<String> start(Database.BatchableContext bc){
return new CustomIterable(CSVFile);
}
//execute method
global void execute(Database.BatchableContext bc,List<String> Scope){
Gsstoupload = new List<GSS_Split__c>();
Gsstoupdate = new List<GSS_Split__c>();
Gsstoupload.clear();
Gsstoupdate.clear();
for(GSS_Split__c gss:[select name,Request_Number__c,Account_Index__c,Period_End_text__c,Period_Start_text__c,HQ__c,GRS_REPTYPE__c,GRS_PLATFORM__c,Territory__c,Region__c,WGSS_Perc__c,GSS_Perc__c,PeriodStart__c,Period_End__c,AX_login__c,Primary_AX__c from GSS_Split__c])
{
Strset.add(gss.Request_Number__c);
Offmap.put(gss.Request_Number__c,gss.id);
System.debug('All GSS ACCOUNT OBJECT RECORDS'+gss);
}
List<String> inputvalues;
try{
System.debug('**scope size**'+scope.size());
for (Integer i=0;i<scope.size();i++)
{
inputvalues = new List<String>();
//split the rows
inputvalues = scope[i].split(',');
if(Strset.contains(inputvalues[0]))
{
GSS_Split__c a = new GSS_Split__c(id=offmap.get(inputvalues[0]));
//split the rows
// inputvalues = scope[i].split(',');
a.Request_Number__c=inputvalues[0];
a.Account_Index__c= inputvalues[1];
a.HQ__c= inputvalues[2];
a.GRS_REPTYPE__c= inputvalues[3];
a.GRS_PLATFORM__c= inputvalues[4];
a.Territory__c= inputvalues[5];
a.Region__c= inputvalues[6];
a.GSS_Perc__c= integer.valueof(inputvalues[7]);
a.WGSS_Perc__c=integer.valueof(inputvalues[8]);
a.Period_Start_text__c=inputvalues[9];
a.Period_End_text__c=inputvalues[10];
a.AX_login__c= inputvalues[11];
a.Primary_AX__c= inputvalues[12];
Gsstoupdate.add(a);
}
else{
GSS_Split__c a = new GSS_Split__c(id=offmap.get(inputvalues[4]));
System.debug('gsss recordsssssss'+a);
a.Request_Number__c=inputvalues[0];
a.Account_Index__c= inputvalues[1];
a.HQ__c= inputvalues[2];
a.GRS_REPTYPE__c= inputvalues[3];
a.GRS_PLATFORM__c= inputvalues[4];
a.Territory__c= inputvalues[5];
a.Region__c= inputvalues[6];
a.GSS_Perc__c= integer.valueof(inputvalues[7]);
a.WGSS_Perc__c=integer.valueof(inputvalues[8]);
a.Period_Start_text__c=inputvalues[9];
a.Period_End_text__c=inputvalues[10];
a.AX_login__c= inputvalues[11];
a.Primary_AX__c= inputvalues[12];
Gsstoupload.add(a);
}
}
Savepoint sp = Database.setSavepoint();
try{
if(Gsstoupdate.size()>0)
{
System.debug('######## gsstoupdate'+Gsstoupdate);
update Gsstoupdate;
}
if(Gsstoupload.size()>0)
{
System.debug('######## gsstoupload'+Gsstoupload);
Database.insert(Gsstoupload,false);
}
}
Catch (Exception e)
{
Database.rollback(sp);
System.debug('Exception--->'+e+'%%%'+e.getmessage());
}
}Catch(Exception e){
system.debug('Exception--->'+e+'%%%'+e.getmessage());
}
}
global void finish(Database.BatchableContext bc) { }
}
Regards,
Venkatesh.
Am trying to handle errors in my csv file before processing batch class
can any one please tell me how to handle errors in batch class and send error list to user email.
I written batch class for inset and update records its working fine,but now i have to handle few validation rule before insert/update and if any record is contain proper value ,i have to stop the batch process can one please help me how can i do this.
Below is my batch apex class.
global class CustomIterableBatchForGSSAccount implements Database.Batchable<String>,Database.Stateful{
global Map<Id, String> errorMap {get; set;}
global Map<Id, SObject> IdToSObjectMap {get; set;}
Public String CSVFile;
Set<string> strset=new Set<string>();
Map<string,id> Offmap=new map<string,id>();
Public List<GSS_Split__c> Gsstoupload;
Public List<GSS_Split__c> Gsstoupdate;
Public List<GSS_Split__c> Gsssize;
Public String Errormsgs;
//Constructor to hold the uploaded CSV file
global CustomIterableBatchForGSSAccount(String CSVFile){
this.CSVFile= CSVFile;
errorMap = new Map<Id, String>();
IdToSObjectMap = new Map<Id, SObject>();
}
//Start Method
global Iterable<String> start(Database.BatchableContext bc){
return new CustomIterable(CSVFile);
}
//execute method
global void execute(Database.BatchableContext bc,List<String> Scope){
Gsstoupload = new List<GSS_Split__c>();
Gsstoupdate = new List<GSS_Split__c>();
Gsstoupload.clear();
Gsstoupdate.clear();
for(GSS_Split__c gss:[select name,Request_Number__c,Account_Index__c,Period_End_text__c,Period_Start_text__c,HQ__c,GRS_REPTYPE__c,GRS_PLATFORM__c,Territory__c,Region__c,WGSS_Perc__c,GSS_Perc__c,PeriodStart__c,Period_End__c,AX_login__c,Primary_AX__c from GSS_Split__c])
{
Strset.add(gss.Request_Number__c);
Offmap.put(gss.Request_Number__c,gss.id);
System.debug('All GSS ACCOUNT OBJECT RECORDS'+gss);
}
List<String> inputvalues;
try{
System.debug('**scope size**'+scope.size());
for (Integer i=0;i<scope.size();i++)
{
inputvalues = new List<String>();
//split the rows
inputvalues = scope[i].split(',');
if(Strset.contains(inputvalues[0]))
{
GSS_Split__c a = new GSS_Split__c(id=offmap.get(inputvalues[0]));
//split the rows
// inputvalues = scope[i].split(',');
a.Request_Number__c=inputvalues[0];
a.Account_Index__c= inputvalues[1];
a.HQ__c= inputvalues[2];
a.GRS_REPTYPE__c= inputvalues[3];
a.GRS_PLATFORM__c= inputvalues[4];
a.Territory__c= inputvalues[5];
a.Region__c= inputvalues[6];
a.GSS_Perc__c= integer.valueof(inputvalues[7]);
a.WGSS_Perc__c=integer.valueof(inputvalues[8]);
a.Period_Start_text__c=inputvalues[9];
a.Period_End_text__c=inputvalues[10];
a.AX_login__c= inputvalues[11];
a.Primary_AX__c= inputvalues[12];
Gsstoupdate.add(a);
}
else{
GSS_Split__c a = new GSS_Split__c(id=offmap.get(inputvalues[4]));
System.debug('gsss recordsssssss'+a);
a.Request_Number__c=inputvalues[0];
a.Account_Index__c= inputvalues[1];
a.HQ__c= inputvalues[2];
a.GRS_REPTYPE__c= inputvalues[3];
a.GRS_PLATFORM__c= inputvalues[4];
a.Territory__c= inputvalues[5];
a.Region__c= inputvalues[6];
a.GSS_Perc__c= integer.valueof(inputvalues[7]);
a.WGSS_Perc__c=integer.valueof(inputvalues[8]);
a.Period_Start_text__c=inputvalues[9];
a.Period_End_text__c=inputvalues[10];
a.AX_login__c= inputvalues[11];
a.Primary_AX__c= inputvalues[12];
Gsstoupload.add(a);
}
}
Savepoint sp = Database.setSavepoint();
try{
if(Gsstoupdate.size()>0)
{
System.debug('######## gsstoupdate'+Gsstoupdate);
update Gsstoupdate;
}
if(Gsstoupload.size()>0)
{
System.debug('######## gsstoupload'+Gsstoupload);
Database.insert(Gsstoupload,false);
}
}
Catch (Exception e)
{
Database.rollback(sp);
System.debug('Exception--->'+e+'%%%'+e.getmessage());
}
}Catch(Exception e){
system.debug('Exception--->'+e+'%%%'+e.getmessage());
}
}
global void finish(Database.BatchableContext bc) { }
}
Regards,
Venkatesh.
1. Key the CSV records with Unique numbers (like 1, 2, 3 ...)
2. Use the Lists to do the inserts (lists are generally ordered).
You can basically know which record exactly failed. and then throw an exception. (when you throw an exception, everything in DB will be rolled back for that batch chunk.
Hope this helps