How to prevent duplicate event/email triggers in scheduled job Script?

dswider
Tera Contributor

Hi everyone, I have a scheduled job script that runs periodically and triggers an eventqueue for a specific record, which sends out an email. I'd like to modify the script so that before the queuing/triggering of the event/email, it checks whether the event has already been triggered for that particular record. If it has, I want to prevent it from being triggered (and emailed) again when the job runs. What's the best way to implement this kind of check?

(function() {
var today = new GlideDate();
var now = new GlideDateTime();

var yesterday = new GlideDate();
yesterday.addDaysLocalTime(-1);

var start = new GlideDateTime();
start.setDisplayValue(yesterday.getByFormat("yyyy-MM-dd") + " 00:00:00");

var inquiryGR = new GlideRecord('x_g_afss_inquiry_inquiries');
inquiryGR.addQuery('sys_created_on', '>=', start);
inquiryGR.addQuery('sys_created_on', '<=', now);
inquiryGR.query();

var count = 0;

while (inquiryGR.next()) {
gs.info('Processing inquiry ' + inquiryGR.getDisplayValue('number'));

// Check if the event has already been triggered by searching in syslog
if (isEventLogged(inquiryGR.getUniqueValue())) {
gs.info('Skipping inquiry ' + inquiryGR.getDisplayValue('number') + ' as event has already been triggered.');
continue;
}

var hasPassbackMatchAttachment = false;

var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'x_g_afss_inquiry_inquiries');
attachmentGR.addQuery('table_sys_id', inquiryGR.getUniqueValue());
attachmentGR.query();

while (attachmentGR.next()) {
var fileName = attachmentGR.getValue('file_name');
if (fileName && fileName.toLowerCase().includes('passback_match')) {
hasPassbackMatchAttachment = true;
break;
}
}

if (hasPassbackMatchAttachment) {
try {
gs.info('Triggering event for inquiry: ' + inquiryGR.getDisplayValue('number'));
gs.eventQueue('x_g_afss_inquiry.bulk_inquiry_attachment', inquiryGR, inquiryGR.getUniqueValue(), '');
gs.info('Event triggered for inquiry: ' + inquiryGR.getDisplayValue('number'));

// Log the event in syslog to mark it as triggered
gs.info('Marking event as triggered for inquiry ' + inquiryGR.getDisplayValue('number'));

// Log a custom entry in the syslog table to track that this event was triggered
var logGR = new GlideRecord('syslog'); // Query syslog table instead of sys_log
logGR.initialize();
logGR.message = 'Event triggered for inquiry: ' + inquiryGR.getDisplayValue('number');
logGR.type = 'info';
logGR.insert();

count++;
} catch (e) {
gs.error('Failed to trigger event for inquiry ' + inquiryGR.getDisplayValue('number') + ': ' + e.message);
}
}
}

gs.info('Finished processing. Total triggered: ' + count);

// Function to check if the event has already been logged in the syslog
function isEventLogged(inquirySysId) {
gs.info('Checking syslog for event for inquiry ' + inquirySysId); // Add debug log to check if we're even reaching here
var logGR = new GlideRecord('syslog'); // Query syslog table
logGR.addQuery('message', 'Event triggered for inquiry: ' + inquirySysId);
logGR.query();

if (logGR.hasNext()) {
gs.info('Event already logged for inquiry ' + inquirySysId); // If log found, log this
return true;
}

return false; // No matching log entry found
}
})();

0 REPLIES 0