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){
}
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){
}
Below is Sample code for getting the failure records Ids. Below is Sample code for the batch class.
let me know if this helps :)
Thanks!
Amit Singh
All Answers
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
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
Below is Sample code for getting the failure records Ids. Below is Sample code for the batch class.
let me know if this helps :)
Thanks!
Amit Singh
Thank you so much for you reply.
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
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