Activity Forums Salesforce® Discussions How to find specific record Id failed in Salesforce Batch?

  • sushant

    Member
    January 23, 2017 at 10:27 am

    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.

Popular Salesforce Blogs

Popular Salesforce Videos