Activity › Forums › Salesforce® Discussions › How to find specific record Id failed in Salesforce Batch?
-
How to find specific record Id failed in Salesforce Batch?
Posted by Tanu on September 9, 2016 at 1:21 PMHii All,
I want to know which records actually failed to update in a chunk.
sushant replied 9 years, 3 months ago 2 Members · 1 Reply -
1 Reply
-
Hi Tanu,
Implementing Database.stateful in your Batch class can help you in getting Failed Record id in Your Batch operation.
global class NBTypeBatchUpdate implements Database.Batchable<sObject>, Database.Stateful {
global String log = ”; // stateful variable to keep track of errors across batches
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = ‘SELECT Id, Name, NBType__c ‘;
query += ‘FROM Opportunity ‘;
query += ‘WHERE ContractStartDate__c != null ‘;
query += ‘AND ContractStartDate__c > 2014-01-01 ‘;
//query += ‘AND NBType__c != null ‘;return Database.getQueryLocator(query);
}global void execute(Database.BatchableContext BC, List<sObject> scope)
{
system.debug(‘Processing ‘ + scope.size() + ‘ opportunities’);for (Opportunity opportunity : (List<Opportunity>)scope){
system.debug(‘Processing ‘ + opportunity.Name);
opportunity.NBType__c = null;
}Database.SaveResult[] srList = database.update(scope, false); // allOrNone = false
for (Integer i = 0; i < srList.size(); i++)
if (!srList[i].isSuccess())
this.log += ‘\n Error in Opportunity: ‘ + scope[i].name + ‘. Error msg=’ +
srList[i].getErrors()[0].getMessage();This code may help You.
Thanks
Log In to reply.