+ Start a Discussion
Luison Lassala 13Luison Lassala 13 

Apex scheduler to update a field on a specific date each Year

I need to set the value of a custom Contact date field to a date on THAT date (e.g. set the field "Next Class Update" to "YYYY/7/1" every year on July 1st).
Searching for a solution on the Developers Community I have come across "Apex Scheduler". I've never written any Apex code and would take me ages to learn. Is there any help I can get to write this very simple "schedule": <set the field "Next Class Update" to "YYYY/7/1" on THAT date and no sooner or later>.
This change should only apply to Contact records with Record_Type = "Child".
Many thanks in advance. Luison
Akshay_DhimanAkshay_Dhiman
Hi Luison,
I have gone through your question and following is the answer :

Apex Class: DevCommunityQ1 
global with sharing class DevCommunityQ1 implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext Bc){
        String query='SELECT ID,NAME,Next_Class_Update__c,RecordTypeId FROM CONTACT';
         return Database.getQueryLocator(query);        
    }
    global void execute(Database.BatchableContext Bc,List<Contact> contList){
        list<Contact>updateContactList = new list<Contact>();
         Id recordTypeChildId = Schema.SObjectType.Contact.RecordTypeInfosByName.get('Child').RecordTypeId;
        if(contList!=null){
            for(Contact cont:contList){
               if(cont.RecordTypeId ==recordTypeChildId){
            cont.Next_Class_Update__c=Date.today();
            updateContactList.add(cont);
               }
            }
        
        }
        system.debug('-----> updateContactList'+updateContactList);
        if(updateContactList.size()>0){
            update updateContactList;
        }
    }
    global void finish(Database.BatchableContext Bc){
        
    }
}
Apex Class : ScheduleDevCommunityQ1
Apex Class : ScheduleDevCommunityQ1

    global with sharing class ScheduleDevCommunityQ1 implements Schedulable {
    global void execute(SchedulableContext ctx){
        Database.executeBatch(new DevCommunityQ1());
    }
}

Developer Console execute this Script :
System.Schedule('Next Class Update','0 0 0 1 7 ? *',new ScheduleDevCommunityQ1());

Explanation:
To write Apex Scheduler code you first need to write Batch class (check my DevCommunityQ1 Apex class).The Batch class have 3 parts or you can say, It is divided into three Sections.

 Section 1: global Database.QueryLocator start(Database.BatchableContext Bc)
 This section is used to write the query in String format.
 Database.getQueryLocator(query) will execute your query and return List to Section 2.
 
 Section 2: global void execute(Database.BatchableContext Bc,List<Contact> contList)
 This section will allow you do all the manipulation 
 
Section 3: global void finish(Database.BatchableContext Bc)
This section is just use for cleanup purpose.You have to another Apex class that will schedule your Batch class (Go through the ScheduleDevCommunityQ1 Apex Class) , but you haven't executed any of your Code.  Now What you need to do is in Developer Console you have to execute the following Script: 
System.Schedule('Next Class Update','0 0 0 1 7 ? *',new ScheduleDevCommunityQ1());
The System.Schedule method takes three arguments: a name for the job, an expression used to represent the time and date the job is scheduled to run, and the third argument will be object of the your Schedule class because it has the object of the Batch Class.
Hope it will work for you.
 
 Regards,
 Akshay
Luison Lassala 13Luison Lassala 13
Thank you Akshay for your help.
I will test it and confirm whether it worked or not.
Akshay_DhimanAkshay_Dhiman
It's my pleasure to help you! Please mark my answer as a best answer, if it is helpful.