Perhaps, the best part of Salesforce is that it can be customised for companies of all sizes and integrates1a 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. Start
2. Declare and assign current time and split current time by individual time windows like minutes, hours, seconds, months and year.
3. Check current hour
a. If it is between 6 am to 6 pm
i. Add minute’s variables with integer 5.
ii. Check MINTUES , IF MINTUES > 59
1. HOURS + 1
2. MINTUES – 60
iii. Check SECONDS , IF SECONDS > 59
1. MINTUES + 1
2. SECONDS – 60
iv. Check HOURS , IF HOURS > 23
1. DATE + 1
2. HOURS – 24
v. IF MONTH, IF it is in February
1. MONTH + 1
2. DATE – 28
ELSE IF MONTH IS in April, June, September, November AND DATE > 30
3. MONTH + 1
4. DATE – 30
ELSE
5. MONTH + 1
6. DATE – 31
vi. Check MONTH > 12
1. YEAR + 1
2. MONTH – 12
vii. Convert each time window into string type so that we can CRON expression.
viii. String strJobName = ‘Job-‘ + Strsecond + ‘_’ + Strminute + ‘_’ + Strhour + ‘_’ + Strday + ‘_’ + Strmonth + ‘_’ + Stryear
b. If hour is in between 6 pm to 6 am
i. Add hours’ variables with integer 2.
ii. REPEAT Same LOGIC.
iii. Built CRON expression.
4. STOP
Example:
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);