“A person with a clear purpose will make progress on even the roughest road. A person with no purpose will make no progress on even the smoothest road.”
— Thomas Carlyle
Last time, we brought our process far enough along to send out some empty notices, and now we need to create the content for those notices that will inform the recipient of the actions required of them. Before we jump into that, though, we still need to add a little bit more code to our Script Include to wrap the process and update the execution record with the results. At the end of our sendNotices function, let’s add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // finalize the execution var itemLabel = 'item' ; if (executionGR.total_items > 1) { itemLabel = 'items' ; } var noticeLabel = 'notice was' ; if (noticeCt > 1) { noticeLabel = 'notices were' ; } executionGR.total_notices = noticeCt; executionGR.run_end = new GlideDateTime(); executionGR.state = 'Complete' ; executionGR.completion_code = 0; executionGR.description = noticeCt + ' ' + noticeLabel + ' generated and sent out covering a total of ' + executionGR.total_items + ' ' + itemLabel + '.' ; executionGR.update(); |
This will update the state of the execution and provide some statistics and a description of the execution. We also need to do one more thing to link the notice records to their corresponding email records, but before we can do that, we have to give the event time to fire and the process time to react to the event firing. We can use a gs.sleep command to do that, but since this is a scoped application, we will have to use a little workaround to get things to work.
1 2 3 | // update the references to the sent email var sleeper = new global.Sleeper(); sleeper.sleep(10000); |
Once we know that the notice has been sent out, we can use the sys_watermark table to locate the information that we need to link the notice record to the associated email record.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // update the references to the sent email var sleeper = new global.Sleeper(); sleeper.sleep(10000); noticeGR.initialize(); noticeGR.addQuery( 'review_execution' , executionGR.getUniqueValue()); noticeGR.query(); while (noticeGR.next()) { var watermark = noticeGR.getValue( 'email_watermark' ); if (watermark) { var watermarkGR = new GlideRecord( 'sys_watermark' ); if (watermarkGR.get( 'number' , watermark.substring(4))) { noticeGR.setValue( 'email' , watermarkGR.getValue( 'email' )); noticeGR.update(); } } } |
That should wrap up the process for sending out the requests to review the artifacts. At this point our entire Script Include now looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 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.processExecution: ' + configuration + '; ' + table + '; ' + filter); // fetch system properties var systemFallbackAddress = gs.getProperty( 'x_11556_periodic_r.fallback_email_address' ); var systemEmailDomain = gs.getProperty( 'x_11556_periodic_r.email_domain' ); // create execution record var executionGR = new GlideRecord( 'x_11556_periodic_r_review_execution' ); executionGR.configuration = configuration; executionGR.run_date = new GlideDate(); executionGR.run_start = new GlideDateTime(); executionGR.state = 'Running' ; executionGR.short_description = executionGR.getDisplayValue( 'run_date' ) + ' review notices' ; executionGR.total_items = 0; executionGR.total_notices = 0; executionGR.completion_code = 0; executionGR.insert(); var configurationGR = executionGR.configuration.getRefRecord(); var fallbackRecipient = configurationGR.getDisplayValue( 'fallback_recipient' ); // create temporary notice record var noticeGR = new GlideRecord( 'x_11556_periodic_r_review_notice' ); noticeGR.review_execution = executionGR.getUniqueValue(); noticeGR.recipient = configurationGR.fallback_recipient; noticeGR.short_description = 'Temporary notice record' ; noticeGR.insert(); var tempNotice = noticeGR.getUniqueValue(); // create notice item records from source table data var noticeItemGR = new GlideRecord( 'x_11556_periodic_r_review_notice_item' ); var itemCt = 0; var sourceGR = new GlideRecord(table); if (sourceGR.isValid()) { if (filter) { sourceGR.addEncodedQuery(filter); } sourceGR.orderBy(configurationGR.short_description_column); sourceGR.query(); while (sourceGR.next()) { noticeItemGR.initialize(); noticeItemGR.review_notice = tempNotice; noticeItemGR.id = sourceGR.getUniqueValue(); noticeItemGR.short_description = sourceGR.getDisplayValue(configurationGR.short_description_column); noticeItemGR.description = sourceGR.getDisplayValue(configurationGR.description_column); noticeItemGR.recipient = sourceGR.getValue(configurationGR.recipient_column); noticeItemGR.insert(); itemCt++; var recipientGR = new GlideRecord( 'sys_user' ); recipientGR.get(noticeItemGR.getValue( 'recipient' )); var notes = '' ; if (recipientGR.isValid()) { if (recipientGR.getValue( 'active' )) { var email = recipientGR.getDisplayValue( 'email' ); if (email) { if (systemEmailDomain && !email.endsWith(systemEmailDomain)) { notes = 'Recipient email address is not an authoized email address; reverting to fallback recipient' ; } } else { notes = 'Specified recipient has no email address; reverting to fallback recipient' ; } } else { notes = 'Specified recipient is not active; reverting to fallback recipient' ; } } else { notes = 'Recipient column empty on source record; reverting to fallback recipient' ; } if (notes) { noticeItemGR.notes = notes; noticeItemGR.recipient = fallbackRecipient; noticeItemGR.update(); } } if (itemCt > 0) { executionGR.total_items = itemCt; executionGR.update(); this .sendNotices(configurationGR, executionGR, noticeGR, noticeItemGR, tempNotice); } else { // finalize the execution executionGR.total_items = 0; executionGR.total_notices = 0; executionGR.run_end = new GlideDateTime(); executionGR.state = 'Complete' ; executionGR.completion_code = 0; executionGR.description = 'No items matched the filter criteria during this run, so no notices were sent out.' ; executionGR.update(); } // delete the temporary notice noticeGR.get(tempNotice); noticeGR.deleteRecord(); } else { // finalize the execution executionGR.total_items = 0; executionGR.total_notices = 0; executionGR.run_end = new GlideDateTime(); executionGR.state = 'Failed' ; executionGR.completion_code = 1; executionGR.description = 'The specified source table in the configuration record is not valid; execution cannot proceed; aborting execution.' ; executionGR.update(); } // set the next run date configurationGR.setValue( 'next_scheduled_date' , this .calculateNextRunDate(configurationGR)); configurationGR.update(); }, sendNotices: function (configurationGR, executionGR, noticeGR, noticeItemGR, tempNotice) { gs.info( 'PeriodicReviewUtils.sendNotices: ' + configurationGR.getUniqueValue() + '; ' + executionGR.getUniqueValue() + '; ' + noticeGR.getUniqueValue() + '; ' + noticeItemGR.getUniqueValue() + '; ' + tempNotice); var noticeCt = 0; var noticeItemGA = new GlideAggregate( 'x_11556_periodic_r_review_notice_item' ); noticeItemGA.addQuery( 'review_notice' , tempNotice); noticeItemGA.addAggregate( 'COUNT' ); noticeItemGA.groupBy( 'recipient' ); noticeItemGA.orderBy( 'recipient' ); noticeItemGA.query(); while (noticeItemGA.next()) { var recipient = noticeItemGA.getValue( 'recipient' ); noticeGR.initialize(); noticeGR.review_execution = executionGR.getUniqueValue(); noticeGR.recipient = recipient; noticeGR.short_description = executionGR.getDisplayValue( 'run_date' ) + ' review notice for ' + configurationGR.getDisplayValue( 'short_description' ); noticeGR.insert(); noticeItemGR.initialize(); noticeItemGR.addQuery( 'recipient' , recipient); noticeItemGR.addQuery( 'review_notice' , tempNotice); noticeItemGR.query(); while (noticeItemGR.next()) { noticeItemGR.review_notice = noticeGR.getUniqueValue(); noticeItemGR.update(); } // now you need to send out the notice, passing in the notice record for variables gs.eventQueue( 'x_11556_periodic_r.ReviewNotice' , noticeGR, noticeGR.recipient, noticeGR.getUniqueValue()); noticeCt++; } // finalize the execution var itemLabel = 'item' ; if (executionGR.total_items > 1) { itemLabel = 'items' ; } var noticeLabel = 'notice was' ; if (noticeCt > 1) { noticeLabel = 'notices were' ; } executionGR.total_notices = noticeCt; executionGR.run_end = new GlideDateTime(); executionGR.state = 'Complete' ; executionGR.completion_code = 0; executionGR.description = noticeCt + ' ' + noticeLabel + ' generated and sent out covering a total of ' + executionGR.total_items + ' ' + itemLabel + '.' ; executionGR.update(); // update the references to the sent email var sleeper = new global.Sleeper(); sleeper.sleep(10000); noticeGR.initialize(); noticeGR.addQuery( 'review_execution' , executionGR.getUniqueValue()); noticeGR.query(); while (noticeGR.next()) { var watermark = noticeGR.getValue( 'email_watermark' ); if (watermark) { var watermarkGR = new GlideRecord( 'sys_watermark' ); if (watermarkGR.get( 'number' , watermark.substring(4))) { noticeGR.setValue( 'email' , watermarkGR.getValue( 'email' )); noticeGR.update(); } } } }, calculateNextRunDate: function (configurationGR) { var runDate = new 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' }; |
We still have to build the content for the notices, and a way for the notice recipients to communicate their responses back to the system so that the appropriate action can be taken, so let’s jump into all of that next time out.