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
Aman Wadhwa 8Aman Wadhwa 8 

scheduale apex problem

I want to run a schedule apex after every 2 years is it possible to do so and if yes how to do it.
Thanks in advance.
Saravana Bharathi 1Saravana Bharathi 1
Hi Aman,

Scheduling through cron expression, is not possible for every 2 years. instead you can try with process builder with scheduled action if there is a custom object to store the next scheduled date and triggering event.

If scheduling is not based on record detail, or if the object has more transaction frequently and not able to consider the filters, then

Create a custom setting to hold the next scheduled date, and create a record with 2 years later date.
Schedule a apex class daily, to query the custom setting record's next scheduled date = today,
if yes, do the <processing> and update the next scheduled date = today+2 years.
If custom setting record's next scheduled date not equal to today, then skip the <processing>.

If it solves the problem, mark it as resolved.

Thanks
 
Divs@newbieDivs@newbie
Hello Aman,

Seems this could be possible.

You can use cron expression and may be set specific date and month and year .

Please find the references on how we can use the cron expressions and scheduler simultaneously.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

https://www.forcetalks.com/blog/using-cron-expression-in-salesforce/

Hope this helps.
ShirishaShirisha (Salesforce Developers) 
Hi Aman,

Greetings!

Can you please check the cron expression to schedule the apex class for every year and try by adding 2 to the year in the below expression:

https://salesforce.stackexchange.com/questions/247663/schedule-apex-class-to-run-every-year

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
AnudeepAnudeep (Salesforce Developers) 
Yes it is possible. I recommend using this cron maker tool to build cron expressions. For example, the cron format for every 2 hours is 
0 0 0/2 1/1 * ? *

Here is an example

If you use the following chron string for your scheduled job, it will run every 4 hours (at set times) Monday through Friday.
 
apexScheduledJob j = new apexScheduledJob (); String sch = '0 0 0,4,8,12,16,20 ? * MON-FRI'; System.schedule('My Job', sch, j);

If you wanted it to run every day, you would just change it to
 
String sch = '0 0 0,4,8,12,16,20 ? * *';

Let me know if this helps