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 

Need to write a trigger that will update a date field every 24 hours

AbhishekAbhishek (Salesforce Developers) 
Hi Ishan,

Your query is answered in the below discussion,

https://success.salesforce.com/answers?id=9063A0000019alZ

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

Thanks.
sachinarorasfsachinarorasf
Hi Ishan,

If you want to update it in every 24 hours then please use batch class and schedular class. The Apex Scheduler lets you delay execution so that you can run Apex classes at a specified time. This is ideal for daily or weekly maintenance tasks using Batch Apex. To take advantage of the scheduler, write an Apex class that implements the Schedulable interface, and then schedule it for execution on a specific schedule.

Please mark it as best answer if you find it helpful.

Kind regards,
Sachin Arora
Ishan Singh 1Ishan Singh 1
Hello Sachinarorasf,
Can you suggest any code for Batch Apex? It would be very helpful.

Regards,
Ishan
sachinarorasfsachinarorasf
Hi Ishan,

You can take help from the below code. Add fields in the query as per your requirement.
​​​​​​
/***** Batch Class ********************/
global with sharing class AccountUpdateDaily implements Database.Batchable<sObject>, Database.Stateful{
    global Database.QueryLocator start(Database.BatchableContext bc){
        try{
            String query = 'Select ID,Name FROM Account';
            return Database.getQueryLocator(query);
        }
        catch(Exception e){
            System.debug('Exception is--'+e.getMessage()+' at line number--'+e.getLineNumber());
        }
        return null;
    }
    
    global void execute(Database.BatchableContext bc, List<Account> accountList){
        try{
            /***** write your code to update the field as per your requirement 
			            **************** /
            
        }
        catch(Exception e){
            System.debug('Exception is--'+e.getMessage()+' at line number--'+e.getLineNumber());
        }
    }
    global void finish(Database.BatchableContext bc){
        System.debug('--In finish--');
        
    }
}


/************* Schedular class ****************/
global  with sharing class AccountUpdateDailySchedular implements Schedulable{
    global void execute(SchedulableContext sc){
      
        AccountUpdateDaily batchObj = new AccountUpdateDaily();
        database.executebatch(batchObj);
		

// Run below-commented code in execute anonymous to schedule the class for daily update the field after 24 hrs.
       /* AccountUpdateDailySchedular sbdt= new AccountUpdateDailySchedular();
        string cronExp=  '0 0 12 1/1 * ? *' ;
        System.schedule('Update Account Daily', cronExp, sbdt);*/
    }
}

Please mark it as best answer if you find it helpful.

Kind regards,
Sachin Arora