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
ms Scottms Scott 

Need an apex scheduler class to update a date field every night

I need a apex scheduler that will update a date field every 24 hours with the current date.

My application track students employment activities, when they start, when job ends, how many days they works, wages per day...etc.

My object name = Grad_Employment_Detail__c
My field name = current_Date__c

More details - this is my work around for not being able to use the "today ()" formula on a formula field because i'm using roll-up summary fields to tally up some key fields. The students might have work for several companies and I need to tally up how many days with each company. (hope this makes sense?)

When employment ends there's an employment start and stop date so it's easy to count the number of days worked.
When they are still working the employment end date field is blank. By creating a "current date" field I can calculate the number of days employed with current employer and add that number with former employment records and roll-up the total number of days worked.

I've tried with workflows and can't seem to get them to work properly....and it's time time for me to go to the next level in development and learn about Apex classes.

Can someone show me the code to write an Apex Trigger that will update a Date Field every 24 hours with the current date?
And I'll need the Apex test class as well.

My object name = Grad_Employment_Detail__c
My field name = current_Date__c

Would like the field to be updated every night at 1am..or anytime after nignight. (I know about the scheduler to set the time)

Thanks in Advance!
M Scott 
VamsiVamsi
Hi,

I guess you need a batch apex to process large number of records in an object.
 
global class UpdateCurrentdate implements Database.Batchable<Grad_Employment_Detail__c>{

   global Database.QueryLocator start(Database.BatchableContext BC)

  {   string query = 'select id, current_Date__c from Grad_Employment_Detail__c';
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<Grad_Employment_Detail__c>scope )
   {
list<Grad_Employment_Detail__c > Ge = new list<Grad_Employment_Detail__c >();
        for(Grad_Employment_Detail__c s : scope)
           {
              s.current_Date__c = system.today;
             Ge.add(s);
            }      
          update Ge;
     }

   global void finish(Database.BatchableContext BC){

   }

}
global class scheduledMerge implements Schedulable 
{
   global void execute(SchedulableContext SC) 
   {
      UpdateCurrentdate  UC = new UpdateCurrentdate ();
      database.executebatch(UC);

    }
}
Now you can schedule this above schedulable class using dev console or through UI 


 
ms Scottms Scott
Getting this error;
Error: Compile Error: expecting right curly bracket, found '02' at line 2 column 0