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
Ishan Singh 1Ishan Singh 1 

I want to update a field in lead everyday with current date and the number of leads are around 40k. So can any one suggest any batch class CODE or anything?

Danish HodaDanish Hoda
hi Ishan,
You can refer below code:
global class LeadUpdateBatch implements Database.Batchable<sObject> {

   global Database.QueryLocator start(Database.BatchableContext BC){
       
       String queryString = 'SELECT Id, Field__c FROM Lead';      //Assuming Field__c is the Date field you need to update

       return Database.getQueryLocator(queryString);
   }

   global void execute(Database.BatchableContext BC, List<Lead> scope){
       List<Lead> leadsToUpdate = new List<Lead>();
       for(Lead leadObj : scope){
           leadObj.Field__c = Date.Toda();
           leadsToUpdate.add(leadObj);
       }
       if(!leadsToUpdate.isEmpty()){
           update leadsToUpdate;
       }
    }

   global void finish(Database.BatchableContext BC){}
}

Create a scheduler class as below:
global class scheduledBatchable implements Schedulable {
   global void execute(SchedulableContext sc) {
      LeadUpdateBatch b = new LeadUpdateBatch(); 
      database.executebatch(b);
   }
}

You can schedule this class using
      Configuration : Setup -> Apex Class -> Schedule Apex (Daily, any time yu want)   
      Using Cron expression