Business rule generating duplicate event entries

matthew_hughes
Kilo Sage

I'm trying to generate emails based on the below business rules that will trigger two events:

matthew_hughes_0-1746530970226.png

 

I'm using the below script:

(function executeRule(current, previous /*null when async*/) {
    // Arrays to track processed emails and SYS IDs
    var processedEmails = [];
    var processedSysIds = [];

    // Function to trigger event if email and SYS ID are not already processed
    function triggerEvent(eventName, email, sysId, manager) {
        if (processedEmails.indexOf(email) === -1 && processedSysIds.indexOf(sysId) === -1) {
            gs.info("gs.eventQueue('" + eventName + "', " + current + ", " + email + ")");
            gs.eventQueue(eventName, current, email, manager);
            processedEmails.push(email);
            processedSysIds.push(sysId);
        }
    }

    if (current.state == 'pending_recertification') {
        var managerEmail = current.group_manager.email;
        var lineManagerEmail = current.group_manager.manager.email;
        var managerSysId = current.group_manager.sys_id;
        var lineManagerSysId = current.group_manager.manager.sys_id;

        gs.info('MH - managerEmail ' + managerEmail);
        gs.info('MH - lineManagerEmail ' + lineManagerEmail);
        gs.info('MH - managerSysId ' + managerSysId);
        gs.info('MH - lineManagerSysId ' + lineManagerSysId);

        // Trigger events for managerEmail and lineManagerEmail
        triggerEvent('x_lbg_group_recert.lbg.group.recert.grou', managerEmail, managerSysId, current.group_manager);
        triggerEvent('x_lbg_group_recert.lbg.group.recert.line', lineManagerEmail, lineManagerSysId, current.group_manager.manager);
    }
})(current, previous);
 
However, what I've noticed is that it's triggering the same events more than once when it should appear once if the user's email and sys ID already appears:
matthew_hughes_1-1746531083978.png

 

 

5 REPLIES 5

JenniferRah
Mega Sage

You don't have any code checking to see if something is already processed. This BR runs when something changes and adds the 2 events. Then if something else changes, it will run again and process the 2 events again. There's nothing in there that checks the event queue to see if something is already there for the given emails or sys_ids. Your variables processedEmails and processedSysIds are re-created each time the BR runs.

Hi @JenniferRah What would I need to include in the business rule script?

Abhijit4
Mega Sage

Hi @matthew_hughes 

 

Based on your latest output logs, I see that Peter has two different entries with same event that is line manager event.

 

If you have inserted single record and it is still triggering multiple events to same user which means most probably your system has any before/after BR with current.update() which triggers your BR twice resulting in duplicate event entries.

 

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

JenniferRah
Mega Sage

Maybe change your function to this? You wouldn't need the processedEmails and processedSysIds variables anymore.

function triggerEvent(eventName, email, sysId, manager) {
    var gr = glideRecord('sysevent');
    gr.addQuery('name', eventName);
    gr.addQuery('instance', current);
    gr.addQuery('parm1', email);
    gr.query();
    if(!gr.next()){
        gs.info("gs.eventQueue('" + eventName + "', " + current + ", " + email + ")");
        gs.eventQueue(eventName, current, email, manager);
    }
}