“One man’s crappy software is another man’s full time job.”
— Jessica Gaston
Last time, we wrapped up the work on creating all of the tables that we will need to support the review process. Now it is time to create that process using those tables. Before we jump into that, though, we should talk a little bit about how the process should work.
There are a couple of different ways to go about things that occur on a periodic basis. One way is to handle things the way most US states handle the renewals for driver’s licenses: each driver has their own renewal date based on something like their birthday or the day on which they originally got their license. Another way would be to handle things like the IRS does for federal income taxes: every person has the same date each year, sometime in the middle of April. One way spreads out the work across the entire year and the other results in all of the work coming at once at a particular time. Our process should be able to handle both approaches, and between the frequency value and the filter value in the configuration record, there should be a way to set things out to work out for either one.
Speaking of the frequency field, we should probably set up a list of specific choices for that guy so that our code knows what to expect. We can do that in the Choices tab of the Related Lists on the Dictionary Entry form of the frequency field.
We may come up with other options in the future, but this list will do for now. The main purpose of this field is so that the process can establish the date of the next run for this particular configuration. Now that we know the values, we can create a simple function to set the next run date based on the current run date and the specified frequency.
calculateNextRunDate: function(configurationGR) {
var runDate = new Date(configurationGR.getDisplayValue('next_scheduled_date'));
var frequency = configurationGR.getValue('frequency');
var days = 0;
var months = 0;
if (frequency == 'daily') {
days = 1;
} else if (frequency == 'weekly') {
days = 7;
} else if (frequency == 'biweekly') {
days = 14;
} else if (frequency == 'monthly') {
months = 1;
} else if (frequency == 'bimonthly') {
months = 2;
} else if (frequency == 'quarterly') {
months = 3;
} else if (frequency == 'semiannually') {
months = 6;
} else if (frequency == 'annually') {
months = 12;
} else if (frequency == 'biannually') {
months = 24;
}
if (days > 0) {
runDate.setDate(runDate.getDate() + days);
} else {
runDate.setMonth(runDate.getMonth() + months);
}
return JSON.stringify(runDate).substring(1, 11);
}
Of course, we won’t need that until the end of the run, as that will be one of the last things that we will do before we move on to the next configuration that we will be running today. So let’s back things up a bit and look at things from a little higher perspective.
To run the review process, we will set up a Scheduled Job that will run once a day, presumably at some time where the demand on the system is lower, and that job will call a Script Include function that will do all of the heavy lifting. That function will then query the database for any configuration records where the next run date is the current date. For each returned record, the process will use the filter field on the configuration record to query the table specified on the configuration record to obtain all of the items up for review. Next we will want to organize all of the items by recipient and send each recipient a notice that contains all of the items up for review for which that recipient is responsible. To house all of the functions required to perform all of thee tasks, we can create a single utility Script Include and call it PeriodicReviewUtils.
We’ll call our first function dailyProcess set up an object array called toRun and populate it with data from the returned configuration records. Then we will spin through the array and pass the values from that object to another function called processExecution that will handle the process for a single configuration.
dailyProcess: function() {
var toRun = [];
var configurationGR = new GlideRecord('x_11556_periodic_r_review_configuration');
var today = new GlideDate();
configurationGR.addQuery('next_scheduled_date', today);
configurationGR.orderBy('number');
configurationGR.query();
while (configurationGR.next()) {
var execution = {};
execution.configuration = configurationGR.getUniqueValue();
execution.table = configurationGR.getDisplayValue('table');
execution.filter = configurationGR.getDisplayValue('filter');
toRun.push(execution);
}
if (toRun.length > 0) {
gs.info('PeriodicReviewUtils.dailyProcess: Running ' + toRun.length + ' execution(s) today.');
for (var i in toRun) {
var thisRun = toRun[i];
this.processExecution(thisRun.configuration, thisRun.table, thisRun.filter);
}
gs.info('PeriodicReviewUtils.dailyProcess: ' + toRun.length + ' execution(s) completed.');
} else {
gs.info('PeriodicReviewUtils.dailyProcess: Nothing scheduled to run today.');
}
}
I like to test things as I go along, just to make sure that I didn’t fat finger something along the way, and we can stop and do that here if we temporarily stub out the processExecution function to just spit out the values that were passed to it. That would make it look something like this:
processExecution: function(configuration, table, filter) {
gs.info('PeriodicReviewUtils.dailyProcess: ' + configuration + '; ' + table + '; ' + filter);
}
We only have the one configuration record that we set up for Service Accounts right now, but that should be enough to validate the code that we have so far, which currently looks like this.
var PeriodicReviewUtils = Class.create();
PeriodicReviewUtils.prototype = {
initialize: function() {
},
dailyProcess: function() {
var toRun = [];
var configurationGR = new GlideRecord('x_11556_periodic_r_review_configuration');
var today = new GlideDate();
configurationGR.addQuery('next_scheduled_date', today);
configurationGR.orderBy('number');
configurationGR.query();
while (configurationGR.next()) {
var execution = {};
execution.configuration = configurationGR.getUniqueValue();
execution.table = configurationGR.getDisplayValue('table');
execution.filter = configurationGR.getDisplayValue('filter');
toRun.push(execution);
}
if (toRun.length > 0) {
gs.info('PeriodicReviewUtils.dailyProcess: Running ' + toRun.length + ' execution(s) today.');
for (var i in toRun) {
var thisRun = toRun[i];
this.processExecution(thisRun.configuration, thisRun.table, thisRun.filter);
}
gs.info('PeriodicReviewUtils.dailyProcess: ' + toRun.length + ' execution(s) completed.');
} else {
gs.info('PeriodicReviewUtils.dailyProcess: Nothing scheduled to run today.');
}
},
processExecution: function(configuration, table, filter) {
gs.info('PeriodicReviewUtils.dailyProcess: ' + configuration + '; ' + table + '; ' + filter);
},
calculateNextRunDate: function(configurationGR) {
var runDate = new Date(configurationGR.getDisplayValue('next_scheduled_date'));
var frequency = configurationGR.getValue('frequency');
var days = 0;
var months = 0;
if (frequency == 'daily') {
days = 1;
} else if (frequency == 'weekly') {
days = 7;
} else if (frequency == 'biweekly') {
days = 14;
} else if (frequency == 'monthly') {
months = 1;
} else if (frequency == 'bimonthly') {
months = 2;
} else if (frequency == 'quarterly') {
months = 3;
} else if (frequency == 'semiannually') {
months = 6;
} else if (frequency == 'annually') {
months = 12;
} else if (frequency == 'biannually') {
months = 24;
}
if (days > 0) {
runDate.setDate(runDate.getDate() + days);
} else {
runDate.setMonth(runDate.getMonth() + months);
}
return JSON.stringify(runDate).substring(1, 11);
},
type: 'PeriodicReviewUtils'
};
To run a quick test, we can navigate over to Scripts – Background and use this little test script.
var pru = new PeriodicReviewUtils();
pru.dailyProcess();
gs.info('Done!');
Then all we need to do is to click on the Run script button and see what happens.
Well, as usual, there is good news and bad news here. The good news is that it didn’t crash and it made all the way through without an error. The bad news is that I forgot to set the next run date field on the lone configuration record, so the process didn’t get very far. On the bright side, though, we did successfully test the scenario where there is nothing to run, so we can check that off of the list. Now let’s update the configuration and try this again.
There, that’s better! And once again, we ran through without any errors, so I think we are off to a good start. Next time, we will build out the code for that processExecution function and give that guy a little test.