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
snehil kumarsnehil kumar 

Get the Id for success updated record each time in batch class

Hi All,

Write a batch class:

update Total Revenue field to 5000 over account.

Total number of records to update is 20.

Batch size is 5.

So the number of times the class is executed is 4.

Track the Id fields for each execution as:

Batch Job 1 : Ids of success records

Batch job 2 : Ids of success records
Batch job 3: Ids of success records
Batch job 4: Ids of success records
Batch job 5:Ids of success records

 

Thanks in Advance

mukesh guptamukesh gupta
Hi snehil,


Please use below code :-
global class BatchAccountUpdate implements Database.Batchable<sObject>,Database.Stateful {
    public integer count = 0;

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT TotalRevenue FROM Account WHERE TotalRevenue != 5000  LIMIT 20';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> accList) {
         for(Account a : accList)
         {
             a.TotalRevenue = 5000;            
         }
       try{
      
        database.saveresult[] ds =  Database.update(accList,false);
            for(database.SaveResult d : ds){
                if(d.issuccess()){
                    count++;
                   system.debug('Batch Job'+ count +' : Ids of success records');

                }
                
            }
        }
        catch(exception e){
        system.debug('update failed');
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}


Anonymous window:-
 
BatchAccountUpdate cls = new BatchAccountUpdate();
Database.executeBatch(cls, 5);

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Snehil,
//Apex Class Code
public class UpdateBatch implements Database.Batchable<sObject> {
    integer recordCount = 0;
    public Database.QueryLocator start(Database.BatchableContext bc )
    {
        return Database.getQueryLocator('SELECT Id,Total_Revenue__c FROM Account WHERE TotalRevenue != 5000  LIMIT 20');
    }
    
    public void execute(Database.BatchableContext bc, List<Account> accList) {
        for(Account acc : accList)
        {
            acc.Total_Revenue__c = 5000;            
        }
        try{
            
            database.saveresult[] saveData =  Database.update(accList,false);
            for(database.SaveResult data : saveData){
                if(data.issuccess()){
                    recordCount++;
                    system.debug('Batch Job'+ recordCount +' : Ids of success records');
                    
                }
                
            }
        }
        catch(exception e){
            system.debug('Exception due to----->: '+e.getCause());
        }
        
    }   
    
    public void finish(Database.BatchableContext BC) {
        {
            AsyncApexJob job=[SELECT Id,Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:bc.getJobId()];
            System.debug('job---->: '+job);
        }   
    }
}
Anonymous window:-
Id batchJobId = Database.executeBatch(new UpdateBatch(),5);



If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi