1

I need the apex class to be scheduled at 1st January 12:00 AM every year how to achieve it?

global class autoCreateIS implements Schedulable {

    global void execute(SchedulableContext arc) {
        autoCreateIS schedule =new autoCreateIS();
        String sch = '0 0 0 1 1 ?';
        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;
     }
}
  • 1
    looks like your cron expression is correct. What problem do you have? – Oleksandr Berehovskyi Jan 23 '19 at 10:38
  • Try using this cron Expression - 0 0 0 1 1 ? *. (Add a Star after question mark at the end) – Jahnvi Jasani Jan 23 '19 at 10:43
2

Your job does not need to reschedule itself, just schedule it to run annually from an execute anonymous script. You can take out the System.schedule logic. You can call the schedule logic directly in execute anonymous.

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

You can user below cron expression to schedule the batch every year

0 0 0 1 1 ? *

it means

0 Seconds 0 Minutes 0 Hours 1 Day_of_month 1 Month Any Day_of_week Any optional_year

autoCreateIS p = new autoCreateIS();
String sch = '0 0 0 1 1 ? *';
system.schedule('Name for Job', sch, p);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.