How to prevent duplicate emails via business rule and events

matthew_hughes
Kilo Sage

I've created the following business and used the below code to stop duplicate emails being sent to the same user if they are in multiple roles within a Business Application:

 

 

matthew_hughes_1-1704809912729.png

 

(function executeRule(current, previous /*null when async*/ ) {
    gs.log("MH - The previous DAO is " + previous.u_business_owner.getDisplayValue());
    gs.log("MH - The current DAO is " + current.u_business_owner.getDisplayValue());
    newOwnersLoop = [];
    var newBusinessApplicationSteward = current.u_cmdb_data_steward;
    var newTechnicalApplicationOwner = current.u_custodian;
    var newBusinessApplicationOwner = current.u_business_owner;
    if (previous.getDisplayValue('u_cmdb_data_steward.manager') == current.getDisplayValue('u_cmdb_data_steward')) {
        if (newOwnersLoop.indexOf(newBusinessApplicationSteward) == -1) {
            newOwnersLoop.push(newBusinessApplicationSteward);
            gs.log('MH - This should display lbg.notify.reassignBAS');
            gs.eventQueue('lbg.notify.reassignBAS', current, newBusinessApplicationSteward, previous.u_cmdb_data_steward.getDisplayValue());
        }
    }


    if (previous.getDisplayValue('u_custodian.manager') == current.getDisplayValue('u_custodian')) {
        if (newOwnersLoop.indexOf(newTechnicalApplicationOwner) == -1) {
            newOwnersLoop.push(newTechnicalApplicationOwner);
            gs.log('MH - This should display lbg.notify.reassignTAO');
            gs.eventQueue('lbg.notify.reassignTAO', current, newTechnicalApplicationOwner, previous.u_custodian.getDisplayValue());
        }
    }


    if (previous.getDisplayValue('u_business_owner.manager') == current.getDisplayValue('u_business_owner')) {
        if (newOwnersLoop.indexOf(newBusinessApplicationOwner) == -1) {
            newOwnersLoop.push(newBusinessApplicationOwner);
            gs.log('MH - This should display lbg.notify.reassignDAO');
            gs.eventQueue('lbg.notify.reassignDAO', current, newBusinessApplicationOwner, previous.u_business_owner.getDisplayValue());
        }
    }
    // Add your code here

})(current, previous);
 
However, when I check the emails, I still see that some users are getting more than one email for the same business application. How do I prevent this so that only email is sent per user?

 

3 REPLIES 3

Hi @nowgpt ai 

 

I've tried the following code:

 

(function executeRule(current, previous /*null when async*/ ) {
    gs.log("MH - The previous DAO is " + previous.u_business_owner.getDisplayValue());
    gs.log("MH - The current DAO is " + current.u_business_owner.getDisplayValue());
    newOwnersLoop = [];
    var newBusinessApplicationSteward = current.u_cmdb_data_steward;
    var newTechnicalApplicationOwner = current.u_custodian;
    var newBusinessApplicationOwner = current.u_business_owner;
    if (previous.getDisplayValue('u_cmdb_data_steward.manager') == current.getDisplayValue('u_cmdb_data_steward')) {
        if (newOwnersLoop.indexOf(newBusinessApplicationSteward.sys_id) == -1) {
            newOwnersLoop.push(newBusinessApplicationSteward.sys_id);
            gs.log('MH - This should display lbg.notify.reassignBAS');
            gs.eventQueue('lbg.notify.reassignBAS', current, newBusinessApplicationSteward, previous.u_cmdb_data_steward.getDisplayValue());
        }
    }


    if (previous.getDisplayValue('u_custodian.manager') == current.getDisplayValue('u_custodian')) {
        if (newOwnersLoop.indexOf(newTechnicalApplicationOwner.sys_id) == -1) {
            newOwnersLoop.push(newTechnicalApplicationOwner.sys_id);
            gs.log('MH - This should display lbg.notify.reassignTAO');
            gs.eventQueue('lbg.notify.reassignTAO', current, newTechnicalApplicationOwner, previous.u_custodian.getDisplayValue());
        }
    }


    if (previous.getDisplayValue('u_business_owner.manager') == current.getDisplayValue('u_business_owner')) {
        if (newOwnersLoop.indexOf(newBusinessApplicationOwner.sys_id) == -1) {
            newOwnersLoop.push(newBusinessApplicationOwner.sys_id);
            gs.log('MH - This should display lbg.notify.reassignDAO');
            gs.eventQueue('lbg.notify.reassignDAO', current, newBusinessApplicationOwner, previous.u_business_owner.getDisplayValue());
        }
    }
    // Add your code here

})(current, previous);
 
 
However, it's still producing duplicate emails. What else do I need to change in my code?

Hi @nowgpt ai  I tried the following code, but it still sent the same emails to the same user:

 

var newOwnersLoop = [];

(function executeRule(current, previous /*null when async*/ ) {
gs.log("MH - The previous DAO is " + previous.u_business_owner.getDisplayValue());
gs.log("MH - The current DAO is " + current.u_business_owner.getDisplayValue());

var newBusinessApplicationSteward = current.u_cmdb_data_steward;
var newTechnicalApplicationOwner = current.u_custodian;
var newBusinessApplicationOwner = current.u_business_owner;

if (previous.getDisplayValue('u_cmdb_data_steward.manager') == current.getDisplayValue('u_cmdb_data_steward')) {
if (!newOwnersLoop.includes(newBusinessApplicationSteward.sys_id)) {
newOwnersLoop.push(newBusinessApplicationSteward.sys_id);
gs.log('MH - This should display lbg.notify.reassignBAS');
gs.eventQueue('lbg.notify.reassignBAS', current, newBusinessApplicationSteward, previous.u_cmdb_data_steward.getDisplayValue());
}
}

if (previous.getDisplayValue('u_custodian.manager') == current.getDisplayValue('u_custodian')) {
if (!newOwnersLoop.includes(newTechnicalApplicationOwner.sys_id)) {
newOwnersLoop.push(newTechnicalApplicationOwner.sys_id);
gs.log('MH - This should display lbg.notify.reassignTAO');
gs.eventQueue('lbg.notify.reassignTAO', current, newTechnicalApplicationOwner, previous.u_custodian.getDisplayValue());
}
}

if (previous.getDisplayValue('u_business_owner.manager') == current.getDisplayValue('u_business_owner')) {
if (!newOwnersLoop.includes(newBusinessApplicationOwner.sys_id)) {
newOwnersLoop.push(newBusinessApplicationOwner.sys_id);
gs.log('MH - This should display lbg.notify.reassignDAO');
gs.eventQueue('lbg.notify.reassignDAO', current, newBusinessApplicationOwner, previous.u_business_owner.getDisplayValue());
}
}
})(current, previous);

Hi @nowgpt ai 

 

I've also tried the following code:

 

(function executeRule(current, previous /*null when async*/ ) {
    gs.log("MH - The previous DAO is " + previous.u_business_owner.getDisplayValue());
    gs.log("MH - The current DAO is " + current.u_business_owner.getDisplayValue());
    newOwnersLoop = [];
    //var newBusinessApplicationSteward = current.u_cmdb_data_steward + '';
    //var newTechnicalApplicationOwner = current.u_custodian + '';
    //var newBusinessApplicationOwner = current.u_business_owner + '';
    if (previous.getDisplayValue('u_cmdb_data_steward.manager') == current.getDisplayValue('u_cmdb_data_steward')) {
        var newBusinessApplicationSteward = current.u_cmdb_data_steward + '';
        if (newOwnersLoop.indexOf(newBusinessApplicationSteward.sys_id) == -1) {
            newOwnersLoop.push(newBusinessApplicationSteward.sys_id);
            gs.log('MH - This should display lbg.notify.reassignBAS');
            gs.eventQueue('lbg.notify.reassignBAS', current, newBusinessApplicationSteward, previous.u_cmdb_data_steward.getDisplayValue());
        }
    }


    if (previous.getDisplayValue('u_custodian.manager') == current.getDisplayValue('u_custodian')) {
        var newTechnicalApplicationOwner = current.u_custodian + '';
        if (newOwnersLoop.indexOf(newTechnicalApplicationOwner.sys_id) == -1) {
            newOwnersLoop.push(newTechnicalApplicationOwner.sys_id);
            gs.log('MH - This should display lbg.notify.reassignTAO');
            gs.eventQueue('lbg.notify.reassignTAO', current, newTechnicalApplicationOwner, previous.u_custodian.getDisplayValue());
        }
    }


    if (previous.getDisplayValue('u_business_owner.manager') == current.getDisplayValue('u_business_owner')) {
        var newBusinessApplicationOwner = current.u_business_owner + '';
        if (newOwnersLoop.indexOf(newBusinessApplicationOwner.sys_id) == -1) {
            newOwnersLoop.push(newBusinessApplicationOwner.sys_id);
            gs.log('MH - This should display lbg.notify.reassignDAO');
            gs.eventQueue('lbg.notify.reassignDAO', current, newBusinessApplicationOwner, previous.u_business_owner.getDisplayValue());
        }
    }
    // Add your code here

})(current, previous);
 
The problem now is that it won't produce the email if the same user is in more than one role.