+ Start a Discussion
raj_sfdccraj_sfdcc 

How to insert failed records into new custom object and how to reprocess them in batch class.

if(!conList.isEmpty()){
            Database.SaveResult[] srList = Database.insert(conList, false);
            
            
            system.debug('srList'+srList);
            integer size=srList.size();
             system.debug('srList size'+size);
               List<Database.SaveResult> Result=srList;
                //Failed Records
                failedconlist=new List<contact>();
                for (Integer i = 0; i<srList.size(); i++) {
                Database.SaveResult s = Result[i];
                contact origRecord = conList[i];
                system.debug('origRecord'+origRecord);
       
                 if(!s.isSuccess()){
                 failedconlist.add(origRecord);
                  // List<Database.Error> err= s.getErrors();
                    ErrorLog__c log=new ErrorLog__c();
                    log.AccountId__c= origRecord.Accountid;
                    
                    log.AccountName__c=origRecord.Account.Name; 
                    
                    for(Database.Error err : s.getErrors()) {
                    // log.ErrorStatusCode__c=err.getStatusCode();   
                    // log.ErrorMessage__c=err.getMessage();
                   // log.ErrorFields__c=err.getFields();
                    }
                    logList.add(log); 
                } 
            }

Hello Experts ,
i have batch class which will insert thousand's of records .so if some records fail  i am trying to insert into error log object .
Help Needed :
1.while i am creating these records into custom object i am not able to insert error messages from DataBase.saveresult .
2.After inserting how to re-process them other than manual process .

Below is my sample code :
when i try to assign errormessage or statuscode to text fileds to not allowing .

Please give me some sample code or suggestion .

Thankyou in advance.
 
Arun MohanArun Mohan
Hi Raj,

Please refer the below code which contains the logic which fulfills your need.
Map<String, String> errorMap =new Map<String, String>();
  // i have used Map<String, SObject> for using the same code in all the batch classes. you can also replace the sobject with your object api name
     Map<String, SObject> IdToSObjectMap = new Map<String, SObject>(); 


 //this is a custom object
 List<US_PM_Service_Request__c> newivsrList = new List<US_PM_Service_Request__c>();
 .......................
 ......// adding data to the list, which needs to be inserted...........
 .......................
 //checking whether the list is empty
  if(!newivsrList.isEmpty()){
 //here the list is inserted.
 List<Database.SaveResult> dsrs = Database.Insert(newivsrList, false);
 Integer index = 0;
			//iterating the list of saveResult
            for(Database.SaveResult dsr : dsrs){
			//checking whether the record is not success (means not inserted records)
                if(!dsr.isSuccess()){
                    // iterating the list of error details
                    for(Database.Error error : dsr.getErrors()){
                     //getting the error message  
                    String errMsg = error.getMessage();
					//adding error details in the map<string,string>
					//<record id, error message >.  here record id is not possible, so we can have any identifiction for the record here. I have given as record1, record2, etc..
                    errorMap.put('record '+index, errMsg);
                   
				   //this map contains <record id, object>. that is the same record id which is used in the errorMap and the record which got failed.
                    IdToSObjectMap.put('record '+index, newivsrList[index]);
                    
                    }
                }
                index++;
            }
			
 }
 
 
  //checking whether the errorMap is not empty
  if(!errorMap.isEmpty()){
 // iterating the errorMap
   for(String id  : errorMap.keySet()){
               string err = errorMap.get(id);
			   //getting the record which with the key value from the errorMap. This is the reason the key(record id) for both errorMap and IdToSObjectMap is kept same.
                SObject sr = IdToSObjectMap.get(id);
            //Now the error message is available in err and record is available in sr. 
			//create a new list of your list<error log> instance before if statement.
			//create a new error log object instance and add the details you need  
            
            } 

  }
  //here insert your list<error log> object here  or pass the value to any future methods.
  }

please let me know if you need more clarifications.

Thanks 
Arun
Adam Cadman 4Adam Cadman 4
Arun,
Thanks - that makes sense, and is helpful - it's just such a shame that such processes are necessary. Surely, it should be easier than this!