<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question How to prevent duplicate event/email triggers in scheduled job Script? in Community Central forum</title>
    <link>https://www.servicenow.com/community/community-central-forum/how-to-prevent-duplicate-event-email-triggers-in-scheduled-job/m-p/3271688#M3190</link>
    <description>&lt;P&gt;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?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;(function() {&lt;BR /&gt;var today = new GlideDate();&lt;BR /&gt;var now = new GlideDateTime();&lt;/P&gt;&lt;P&gt;var yesterday = new GlideDate();&lt;BR /&gt;yesterday.addDaysLocalTime(-1);&lt;/P&gt;&lt;P&gt;var start = new GlideDateTime();&lt;BR /&gt;start.setDisplayValue(yesterday.getByFormat("yyyy-MM-dd") + " 00:00:00");&lt;/P&gt;&lt;P&gt;var inquiryGR = new GlideRecord('x_g_afss_inquiry_inquiries');&lt;BR /&gt;inquiryGR.addQuery('sys_created_on', '&amp;gt;=', start);&lt;BR /&gt;inquiryGR.addQuery('sys_created_on', '&amp;lt;=', now);&lt;BR /&gt;inquiryGR.query();&lt;/P&gt;&lt;P&gt;var count = 0;&lt;/P&gt;&lt;P&gt;while (inquiryGR.next()) {&lt;BR /&gt;gs.info('Processing inquiry ' + inquiryGR.getDisplayValue('number'));&lt;/P&gt;&lt;P&gt;// Check if the event has already been triggered by searching in syslog&lt;BR /&gt;if (isEventLogged(inquiryGR.getUniqueValue())) {&lt;BR /&gt;gs.info('Skipping inquiry ' + inquiryGR.getDisplayValue('number') + ' as event has already been triggered.');&lt;BR /&gt;continue;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;var hasPassbackMatchAttachment = false;&lt;/P&gt;&lt;P&gt;var attachmentGR = new GlideRecord('sys_attachment');&lt;BR /&gt;attachmentGR.addQuery('table_name', 'x_g_afss_inquiry_inquiries');&lt;BR /&gt;attachmentGR.addQuery('table_sys_id', inquiryGR.getUniqueValue());&lt;BR /&gt;attachmentGR.query();&lt;/P&gt;&lt;P&gt;while (attachmentGR.next()) {&lt;BR /&gt;var fileName = attachmentGR.getValue('file_name');&lt;BR /&gt;if (fileName &amp;amp;&amp;amp; fileName.toLowerCase().includes('passback_match')) {&lt;BR /&gt;hasPassbackMatchAttachment = true;&lt;BR /&gt;break;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (hasPassbackMatchAttachment) {&lt;BR /&gt;try {&lt;BR /&gt;gs.info('Triggering event for inquiry: ' + inquiryGR.getDisplayValue('number'));&lt;BR /&gt;gs.eventQueue('x_g_afss_inquiry.bulk_inquiry_attachment', inquiryGR, inquiryGR.getUniqueValue(), '');&lt;BR /&gt;gs.info('Event triggered for inquiry: ' + inquiryGR.getDisplayValue('number'));&lt;/P&gt;&lt;P&gt;// Log the event in syslog to mark it as triggered&lt;BR /&gt;gs.info('Marking event as triggered for inquiry ' + inquiryGR.getDisplayValue('number'));&lt;/P&gt;&lt;P&gt;// Log a custom entry in the syslog table to track that this event was triggered&lt;BR /&gt;var logGR = new GlideRecord('syslog'); // Query syslog table instead of sys_log&lt;BR /&gt;logGR.initialize();&lt;BR /&gt;logGR.message = 'Event triggered for inquiry: ' + inquiryGR.getDisplayValue('number');&lt;BR /&gt;logGR.type = 'info';&lt;BR /&gt;logGR.insert();&lt;/P&gt;&lt;P&gt;count++;&lt;BR /&gt;} catch (e) {&lt;BR /&gt;gs.error('Failed to trigger event for inquiry ' + inquiryGR.getDisplayValue('number') + ': ' + e.message);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;gs.info('Finished processing. Total triggered: ' + count);&lt;/P&gt;&lt;P&gt;// Function to check if the event has already been logged in the syslog&lt;BR /&gt;function isEventLogged(inquirySysId) {&lt;BR /&gt;gs.info('Checking syslog for event for inquiry ' + inquirySysId); // Add debug log to check if we're even reaching here&lt;BR /&gt;var logGR = new GlideRecord('syslog'); // Query syslog table&lt;BR /&gt;logGR.addQuery('message', 'Event triggered for inquiry: ' + inquirySysId);&lt;BR /&gt;logGR.query();&lt;/P&gt;&lt;P&gt;if (logGR.hasNext()) {&lt;BR /&gt;gs.info('Event already logged for inquiry ' + inquirySysId); // If log found, log this&lt;BR /&gt;return true;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;return false; // No matching log entry found&lt;BR /&gt;}&lt;BR /&gt;})();&lt;/P&gt;</description>
    <pubDate>Thu, 22 May 2025 21:19:12 GMT</pubDate>
    <dc:creator>dswider</dc:creator>
    <dc:date>2025-05-22T21:19:12Z</dc:date>
    <item>
      <title>How to prevent duplicate event/email triggers in scheduled job Script?</title>
      <link>https://www.servicenow.com/community/community-central-forum/how-to-prevent-duplicate-event-email-triggers-in-scheduled-job/m-p/3271688#M3190</link>
      <description>&lt;P&gt;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?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;(function() {&lt;BR /&gt;var today = new GlideDate();&lt;BR /&gt;var now = new GlideDateTime();&lt;/P&gt;&lt;P&gt;var yesterday = new GlideDate();&lt;BR /&gt;yesterday.addDaysLocalTime(-1);&lt;/P&gt;&lt;P&gt;var start = new GlideDateTime();&lt;BR /&gt;start.setDisplayValue(yesterday.getByFormat("yyyy-MM-dd") + " 00:00:00");&lt;/P&gt;&lt;P&gt;var inquiryGR = new GlideRecord('x_g_afss_inquiry_inquiries');&lt;BR /&gt;inquiryGR.addQuery('sys_created_on', '&amp;gt;=', start);&lt;BR /&gt;inquiryGR.addQuery('sys_created_on', '&amp;lt;=', now);&lt;BR /&gt;inquiryGR.query();&lt;/P&gt;&lt;P&gt;var count = 0;&lt;/P&gt;&lt;P&gt;while (inquiryGR.next()) {&lt;BR /&gt;gs.info('Processing inquiry ' + inquiryGR.getDisplayValue('number'));&lt;/P&gt;&lt;P&gt;// Check if the event has already been triggered by searching in syslog&lt;BR /&gt;if (isEventLogged(inquiryGR.getUniqueValue())) {&lt;BR /&gt;gs.info('Skipping inquiry ' + inquiryGR.getDisplayValue('number') + ' as event has already been triggered.');&lt;BR /&gt;continue;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;var hasPassbackMatchAttachment = false;&lt;/P&gt;&lt;P&gt;var attachmentGR = new GlideRecord('sys_attachment');&lt;BR /&gt;attachmentGR.addQuery('table_name', 'x_g_afss_inquiry_inquiries');&lt;BR /&gt;attachmentGR.addQuery('table_sys_id', inquiryGR.getUniqueValue());&lt;BR /&gt;attachmentGR.query();&lt;/P&gt;&lt;P&gt;while (attachmentGR.next()) {&lt;BR /&gt;var fileName = attachmentGR.getValue('file_name');&lt;BR /&gt;if (fileName &amp;amp;&amp;amp; fileName.toLowerCase().includes('passback_match')) {&lt;BR /&gt;hasPassbackMatchAttachment = true;&lt;BR /&gt;break;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (hasPassbackMatchAttachment) {&lt;BR /&gt;try {&lt;BR /&gt;gs.info('Triggering event for inquiry: ' + inquiryGR.getDisplayValue('number'));&lt;BR /&gt;gs.eventQueue('x_g_afss_inquiry.bulk_inquiry_attachment', inquiryGR, inquiryGR.getUniqueValue(), '');&lt;BR /&gt;gs.info('Event triggered for inquiry: ' + inquiryGR.getDisplayValue('number'));&lt;/P&gt;&lt;P&gt;// Log the event in syslog to mark it as triggered&lt;BR /&gt;gs.info('Marking event as triggered for inquiry ' + inquiryGR.getDisplayValue('number'));&lt;/P&gt;&lt;P&gt;// Log a custom entry in the syslog table to track that this event was triggered&lt;BR /&gt;var logGR = new GlideRecord('syslog'); // Query syslog table instead of sys_log&lt;BR /&gt;logGR.initialize();&lt;BR /&gt;logGR.message = 'Event triggered for inquiry: ' + inquiryGR.getDisplayValue('number');&lt;BR /&gt;logGR.type = 'info';&lt;BR /&gt;logGR.insert();&lt;/P&gt;&lt;P&gt;count++;&lt;BR /&gt;} catch (e) {&lt;BR /&gt;gs.error('Failed to trigger event for inquiry ' + inquiryGR.getDisplayValue('number') + ': ' + e.message);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;gs.info('Finished processing. Total triggered: ' + count);&lt;/P&gt;&lt;P&gt;// Function to check if the event has already been logged in the syslog&lt;BR /&gt;function isEventLogged(inquirySysId) {&lt;BR /&gt;gs.info('Checking syslog for event for inquiry ' + inquirySysId); // Add debug log to check if we're even reaching here&lt;BR /&gt;var logGR = new GlideRecord('syslog'); // Query syslog table&lt;BR /&gt;logGR.addQuery('message', 'Event triggered for inquiry: ' + inquirySysId);&lt;BR /&gt;logGR.query();&lt;/P&gt;&lt;P&gt;if (logGR.hasNext()) {&lt;BR /&gt;gs.info('Event already logged for inquiry ' + inquirySysId); // If log found, log this&lt;BR /&gt;return true;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;return false; // No matching log entry found&lt;BR /&gt;}&lt;BR /&gt;})();&lt;/P&gt;</description>
      <pubDate>Thu, 22 May 2025 21:19:12 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/how-to-prevent-duplicate-event-email-triggers-in-scheduled-job/m-p/3271688#M3190</guid>
      <dc:creator>dswider</dc:creator>
      <dc:date>2025-05-22T21:19:12Z</dc:date>
    </item>
  </channel>
</rss>

