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
Ramssf70Ramssf70 

how to get failure job record id in batch class?

Hi Folks,

I want to get failure job record Id's when the batch fails can any one  tell that ?
Best Answer chosen by Ramssf70
Amit Singh 1Amit Singh 1
Hello,

Below is Sample code for getting the failure records Ids.
// Create two accounts, one of which is missing a required field
Account[] accts = new List<Account>{
    new Account(Name='Account1'),
    new Account()};
Database.SaveResult[] srList = Database.insert(accts, false);

// Iterate through each returned result
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + sr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}
Below is Sample code for the batch class.
global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{

   global final String query;
   global Set<Id> failureIdsSet;
   global Map<Id, Account> accountmap;
   blobal SummarizeAccountTotal(String q){
          Query = 'Select Id,Name From Account Limit 10';
   }
   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }
   
   global void execute(Database.BatchableContext BC, List<sObject> scope){
          failureIdsSet = new Set<Id>();
          Database.SaveResult[] srList = Database.insert(scope, false);
          // Iterate through each returned result 
          for (Database.SaveResult sr : srList) {
             if (sr.isSuccess()) {
             }else{
                    for(Database.Error err : sr.getErrors()) {
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        failureIdsSet.add(err.getId());
                    }
             }
         }
   
   }
global void finish(Database.BatchableContext BC){

}

let me know if this helps :)

Thanks!
Amit Singh
 

All Answers

Amit Singh 1Amit Singh 1
Hello,
You need to implement Database.Statefull interface in your batch class and then use Database method for making the DML operation and can get failure records ids in finish method.
Refer below links for more info.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database_result_classes.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm

Let me know if this helps :)

Thanks!
Amit
Ramssf70Ramssf70
Hi Amit,

Thak you so much  for your reply Can you implement  the database.stateful  because i don't know how to implement the database.stateful and give that code

Thank you
Amit Singh 1Amit Singh 1
Hello,

Below is Sample code for getting the failure records Ids.
// Create two accounts, one of which is missing a required field
Account[] accts = new List<Account>{
    new Account(Name='Account1'),
    new Account()};
Database.SaveResult[] srList = Database.insert(accts, false);

// Iterate through each returned result
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + sr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}
Below is Sample code for the batch class.
global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{

   global final String query;
   global Set<Id> failureIdsSet;
   global Map<Id, Account> accountmap;
   blobal SummarizeAccountTotal(String q){
          Query = 'Select Id,Name From Account Limit 10';
   }
   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }
   
   global void execute(Database.BatchableContext BC, List<sObject> scope){
          failureIdsSet = new Set<Id>();
          Database.SaveResult[] srList = Database.insert(scope, false);
          // Iterate through each returned result 
          for (Database.SaveResult sr : srList) {
             if (sr.isSuccess()) {
             }else{
                    for(Database.Error err : sr.getErrors()) {
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        failureIdsSet.add(err.getId());
                    }
             }
         }
   
   }
global void finish(Database.BatchableContext BC){

}

let me know if this helps :)

Thanks!
Amit Singh
 
This was selected as the best answer
Ramssf70Ramssf70
Hi,

Thank you so much for you reply.
Mark TroupeMark Troupe
Amit Singh 1 no such method as err.getId()
Sonu Kamble 6Sonu Kamble 6

Hello Sir,

In my code there is an error
"Method does not exist or incorrect signature: void getId() from the type Database.Error"

what should i do

swagat patelswagat patel
instead of err.getId() use sr.getId() in above program because there is no method defined in class database.error with name getId().

Refer
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_error.htm
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_saveresult.htm#apex_methods_system_database_saveresult