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
AbAb 

Updating the records using Batch

Hello,

I am updating the records in below usecase:

- Get 60000 records by SOQL
- Prepare a list of 5000 records
- Update abouve records

As the operation is one time and long, how can i update the list using Batch.

thank you for rough code, please
Best Answer chosen by Ab
Footprints on the MoonFootprints on the Moon
Hi Sanrine,
To serve your purpose refer below rough code, and modify accordingly-
global class BatchApexExample implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
	
        //SOQL to collect the batches of 60000 records to be passed to execute
         
        String query = 'SELECT Id, Name FROM <your-sObject>';
        return Database.getQueryLocator(query);
    }
     
    global void execute(Database.BatchableContext BC, List<sObject> scope) {
        
        // process each batch of records as per your requirement
        for(sObject sObj : scope) {        
            // Update the record            
        }
        try {
            // Update the records into Database
            update scope;         
        } catch(Exception e) {
            System.debug(e);
        }
         
    }   
     
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations like sending email
    }
}
Let me know if this helps!