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
sp13sp13 

how to schedule apex class yearly

how can i schedule this code yearly?
i want it to run every January 1 at 12am
global class autoCreateIS implements Schedulable {
         global void execute(SchedulableContext arc) {
                    Date dateToday = system.Today();
                    Integer year = dateToday.year();
                    Integer nextYear = (year + 1);
       
                    IS__c createIS = new IS__c();
                    createIS.Name = String.valueOf(nextYear);
                    insert createIS;
         }
}

when i go to Apex Classes > Schedule Apex, the only choices are weekly and monthly
what if i only want it to run yearly?
Best Answer chosen by sp13
Sri549Sri549
Hello Sp,

The way you were is wrong, the code which i have sent ,please paste in developer console and execute and see the status in Administration Setup-->Monitoring-->Scheduled Jobs it will be showing up as required.

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer


 

All Answers

Sri549Sri549
Hello sp,
As per your requirement i am sending code to schedule the class at 12 Am at every January 1.


autoCreateIS schedule =new autoCreateIS();
String sch ='0 0 0 1 1 ?';
System.schedule('YOurchoiceofname Scheduled',sch, schedule);

if you get any kind of issue in this ping reply me.

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer
sp13sp13

tnx @Sri549!
should i still go to Apex Classes > Schedule Apex to create new schedule for this?
i added your code in my code but used a different time and date(February 17, 7:40PM) to test if it's working:

global class autoCreateIS implements Schedulable {
         global void execute(SchedulableContext arc) {
                  
autoCreateIS schedule =new autoCreateIS();
                   String sch = '0 40 19 17 2 ?';
                   System.schedule('autoCreateISsched',sch, schedule);

                    Date dateToday = system.Today();
                    Integer year = dateToday.year();
                    Integer nextYear = (year + 1);
      
                    IS__c createIS = new IS__c();
                    createIS.Name = String.valueOf(nextYear);
                    insert createIS;
         }
}

but it didnt work :(

Sri549Sri549
Hello Sp,

The way you were is wrong, the code which i have sent ,please paste in developer console and execute and see the status in Administration Setup-->Monitoring-->Scheduled Jobs it will be showing up as required.

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer


 
This was selected as the best answer
Namina P VNamina P V
Hi,
I was executing the script,, It got queued, still not running the job. Is it some salesforce default issue.?

Thanks 
Namina