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
ch ranjeethch ranjeeth 

How to call Batch apex in controller? Please provide some sample code?

Ravikant kediaRavikant kedia
Here is apex batch :
global class batchAccountUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
       
        String query = 'SELECT Id,Name FROM Account';
        return Database.getQueryLocator(query);
    }
  
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         for(Account a : scope)
         {
             a.Name = a.Name + 'Updated';           
         }
         update scope;
    }  
   
    global void finish(Database.BatchableContext BC) {
    }
}

Now you can use below code for batch execute :


batchAccountUpdate batchaupdate = new batchAccountUpdate();
Database.executeBatch(batchaupdate);