Published on
March 2, 2015

Setup a consultation

How To Schedule An Apex Class To Run At Regular Intervals

Perhaps, the best part of Salesforce is that it can be customized for companies of all sizes and integrates with any business process. As a part of our on-going series in ‘Apex Programming tips and tricks’, we bring to you one such case wherein the client sought a report/invoice to be generated on minutely basis and approached us for an appropriate solution.

Our client wanted to move their existing CRM system to Salesforce CRM. Their requirement was to have the invoices (Opportunity) generated from Salesforce and sent to their chosen accounting system. The condition was that invoices should be synced every time from

Salesforce objects to opportunities.

Hence, we suggested the client to opt for an Apex schedule job which syncs invoices effortlessly. The sync process was supposed to be conducted every 5 minutes from 6 am to 8 pm and every 2 hours from 8 pm to 6 am.

Technical Objective: Due to the limitations in Salesforce, we could not achieve this functionality directly using CRON expression in Schedule Apex. Therefore, we implemented custom logic to build CRON expression dynamically based on some time period and here is the custom logic to prepare CRON expression:Algorithm:1. Start2. Declare and assign current time and split current time by individual time windows like minutes, hours, seconds, months and year.3. Check current houra. If it is between 6 am to 6 pmi. Add minute’s variables with integer 5.ii. Check MINTUES , IF MINTUES > 591. HOURS + 12. MINTUES – 60iii. Check SECONDS , IF SECONDS > 591. MINTUES + 12. SECONDS – 60iv. Check HOURS , IF HOURS > 231. DATE + 12. HOURS – 24v. IF MONTH, IF it is in February1. MONTH + 12. DATE – 28ELSE IF MONTH IS in April, June, September, November AND DATE > 303. MONTH + 14. DATE – 30ELSE5. MONTH + 16. DATE – 31vi. Check MONTH > 121. YEAR + 12. MONTH – 12vii. Convert each time window into string type so that we can CRON expression.viii. String strJobName = 'Job-' + Strsecond + '_' + Strminute + '_' + Strhour + '_' + Strday + '_' + Strmonth + '_' + Stryearb. If hour is in between 6 pm to 6 ami. Add hours’ variables with integer 2.ii. REPEAT Same LOGIC.iii. Built CRON expression.4. STOPExample:Logic to construct CRON expression for every five minutes:Integer intYear = system.now().year();Integer intMon = system.now ().month ();Integer intDate = system.now ().day ();Integer intHr = system.now ().hour ();Integer intMin = system.now ().minute () + 10;Integer intSec = system.now ().second ();if (intHr >= 8 && intHr < 18){if (intMin > 59){IntHr=intHr+1;IntMin=intMin-60;}if (intSec > 59){intMin=intMin+1;IntSec=intSec-60;}if (intHr > 23){IntDate = intDate + 1;intHr = intHr - 24;}if(intMon == 2 && intDate > 28){intMon = intMon + 1;intDate = intDate - 28;}else if( (intMon == 4 || intMon == 6 || intMon == 9 || intMon == 11 ) && ( intDate > 30 ) ){intMon = intMon + 1;intDate = intDate - 30;} else if (intDate > 31) {intMon = intMon + 1;intDate = intDate - 31;}

if(intMon>12){intYear=intYear+1;intMon=intMon-12;}String Stryear = string.valueOf(intYear);String Strmonth = string.valueOf(intMon);String Strday = string.valueOf(intDate);String Strhour = string.valueOf(intHr);String Strminute = string.valueOf(intMin);String Strsecond = string.valueOf(intSec);

String strJobName = 'Job-' + Strsecond + '_' + Strminute + '_' + Strhour + '_' + Strday + '_' + Strmonth + '_' + Stryear;String strSchedule = '0 ' + Strminute + ' ' + Strhour + ' ' + Strday + ' ' + Strmonth + ' ?' + ' ' + Stryear;

System.schedule(strJobName, strSchedule,Instanse Of Schedule class);