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
CBBsfdcCBBsfdc 

Write a Batch Apex to update all the Account Name appending with text "Testing". Schedule this batch apex to run everday at 5 pm.

sai tarunsai tarun
Batch Class:
===================
Global class appendAccName imlpements database.batchable<SObject>{
  global database.queryLocator start(Database.batchableContext bc){
  string query = 'select name from account';
  return database.getQueryLocator(query);
  }
  global void execute (database.batchableContext bc, List<Accounts> scope){
  for(Account a: scope){
  a.name = a.name+' Testing';
  }
  Update Scope;
  }
  global void finish(database.batchableContext bc){
  }


Schedule class for above batch class:
===================================
Global Class scheduleAppendAccName implements Schedulable{
 global void execute(schedulable context sc){
 appendAccName an = new appendAccName();
 Id JobId = Database.ExecuteBatch(an);
 }
}

Schedule using cron expression as per your requirement:
========================================
scheduleAppendAccName sb = new scheduleAppendAccName();
string sch= '0 0 16 * * ?';
system.schedule('Daily run Batch',sch,sb);

please mark it as best answer....
Thanks,
Tarun
Deepali KulshresthaDeepali Kulshrestha
Hi CBB123456,

Please do try the code below and you will surely get the expected results.

BATCH CLASS:

global class BatchUpdateAccountField implements Database.Batchable <SObject>{
global Database.QueryLocator start (Database.BatchableContext bc)
{
    return Database.getQueryLocator('SELECT name from Account');
}
   global void execute(Database.BatchableContext bc, List<Account> acList)
   {
       for(Account ac :acList)
       {
          ac.name = ac.name + ' Testing';
           
       }
       
       update acList;
   }
    
    global void finish(Database.BatchableContext bc)
    {
        System.debug('>>>Finish');
    }
    
}

SCHEDULE CLASS:

global class ScheduleBatchAccFieldUpdate implements Schedulable {
    global void execute(SchedulableContext sc)
    {
        BatchUpdateAccountField ba =new BatchUpdateAccountField();
        Database.executeBatch(ba);
    }

}

CODE TO BE WRITTEN IN EXECUTE ANONYMOUS WINDOW TO EXCUTE SCHEDULE CLASS:

ScheduleBatchAccFieldUpdate sbb= new ScheduleBatchAccFieldUpdate();
String sch= '0 0 17 1/1 * ? *';
String jobID= system.schedule('AccountUpdateDaily',sch, sbb);


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha