+ Start a Discussion
Durai SaravananDurai Saravanan 

Schedule the current schedule job after 5 days through apex code

Hi,

I have requirement to schedule the below code on the third friday of every month, which i can do through "Schedule apex" option in UI itself.
 
global class ApexScheduledClass Implements Schedulable
{
   global void execute(SchedulableContext sc)
        {
        if(/*Today is not last working day*/) {
            sendmail();
           // ApexScheduledClass.Schedulable.execute(System.param1);
        } else if (/*Today is the last working day*/) {
            sendremaindermail();
        }
    }
    
    public void sendmail()
    {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        string [] toaddress= New string[]{'durairaj1696@gmail.com'};
        email.setSubject('Testing Apex Scheduler-Subject');
        email.setPlainTextBody('Testing Apex Scheduler-Body');
        email.setToAddresses(toaddress);
        Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
    }
    
    public void sendremaindermail()
    {
       Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       string [] toaddress= New string[]{'durairaj1696@gmail.com'};
       email.setSubject('Testing Apex Scheduler-Subject');
       email.setPlainTextBody('Testing Apex Scheduler-Body');
       email.setToAddresses(toaddress);
       Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
    }
}

Now, when it runs on third friday of every month, it must again schedule itself to run after 5 days from then.

How can i do that. Any suggestion is welcome. Thanks in advance.

Thanks,
Durairaj


 
Best Answer chosen by Durai Saravanan
Lokesh KumarLokesh Kumar
0 0 20 ? 1/1 FRI#3 *

Here are the cron expressions for the schedule.
Every 3rd Friday of a Month at 8 PM

The code to run in developer console anonymous block to schedule the jobs
System.schedule('2nd Friday Schedule', '0 0 20 ? 1/1 FRI#3 *', new YourApexClass());