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
Priyanka DumkaPriyanka Dumka 

there is one Batch Apex running. I want id’s of Failure records and Success records.

There  is one Batch Apex running. I want id’s of Failure records and Success records.
SwethaSwetha (Salesforce Developers) 
HI Priyanka,
A similar ask was posted in the past: https://developer.salesforce.com/forums/?id=9060G0000005qnQQAQ .You can make use of this code to get started. 

You can try below example code for handling failed record Ids
public class MyBatch implements Database.Batchable<sObject>, Database.Stateful {
    public List<Id> failedIds = new List<Id>();
    
    public void execute(Database.BatchableContext context, List<sObject> scope) {
        try {
            // process records
            update scope;
        } catch (Exception e) {
            // add failed record ID to list
            for (sObject record : scope) {
                failedIds.add(record.Id);
            }
        }
    }
    
    public void finish(Database.BatchableContext context) {
        // do something with failedIds list
    }
}




If this information helps, please mark the answer as best. Thank you
Priyanka DumkaPriyanka Dumka
Hi ,

Can you help me how  to get count of failed and success records .
SwethaSwetha (Salesforce Developers) 
@Priyanka,

You can try something like the below code
global class MyBatch implements Database.Batchable<sObject>, Database.Stateful {
    public Integer successCount = 0;
    public Integer failureCount = 0;
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // Your query to fetch records goes here
        return Database.getQueryLocator('SELECT Id, Name FROM Account');
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope) {
        try {
            // Your code to process the record goes here
            update scope;
            successCount += scope.size();
        } catch (Exception e) {
            // Your code to handle exceptions goes here
            failureCount += scope.size();
        }
    }

    global void finish(Database.BatchableContext BC) {
        System.debug('Success Count: ' + successCount);
        System.debug('Failure Count: ' + failureCount);
        // Your code to handle the end of the batch job goes here
    }
}

Related: https://salesforce.stackexchange.com/questions/45836/find-apex-batch-status-programmatically

If this information helps, please mark the answer as best. Thank you