Worknotes being added twice is user is has multiple roles

thrsdy1512
Tera Expert

I have a Business Rule which updates a worknote when a field value is changed and there is a simple script which checks who has updated the field and applies the appropriate worknote. 

The issue I have is that there is overlap. A user can have the architure domain admin/manager role AND also be a business steward or tech owner, in which case the worknote is updating twice. How can I tidy this up to avoid this happening? Please suggested simple low code solution, thank you.

 

Script is: 

 

(function executeRule(current, previous /*null when async*/) {

    var businessApplicationSteward = current.getValue('u_cmdb_data_steward');

    var businessApplicationStewardName = current.u_cmdb_data_steward.name+"";

    var technicalApplicationOwner = current.getValue('u_custodian');

    var technicalApplicationOwnerName = current.u_custodian.name+"";

   

 

//If the field changes business app steward

    if ((current.u_primary_capability.changes() && gs.getUserID() == businessApplicationSteward)) {

            current.work_notes = 'The Primary Capability field has been updated by the Technical owner representative '+ businessApplicationStewardName;

    }

   

    //If the field changes  by the Technical Owner

    if ((current.u_primary_capability.changes() && gs.getUserID() == technicalApplicationOwner)) {

            current.work_notes = 'The Primary Capability field has been updated by the Technical Owner '+ technicalApplicationOwnerName;

 

 

 

    //If the field changes and is updated by users with the architecture_domain_admin OR architecture_domain_manager role

    if (current.u_primary_capability.changes() &&

    (gs.hasRole('lbg_architecture_domain_admin') || gs.hasRole('lbg_architecture_domain_manager'))) {

    var userName = gs.getUserDisplayName();

    current.work_notes = "The Primary Capability field has been updated by " + userName + ".";

}

   

})(current, previous);

9 REPLIES 9

Brad Bowman
Kilo Patron
Kilo Patron

You can make the second and third if statements else if statements.

Ankur Bawiskar
Tera Patron
Tera Patron

@thrsdy1512 

try these changes

  • The check for u_primary_capability change happens once.
  • The user identity condition checks are done in order with else if so only the first applicable note is set.
  • This prevents multiple overwrites and duplicate work notes when users have multiple roles.
(function executeRule(current, previous /*null when async*/) {
    var businessApplicationSteward = current.getValue('u_cmdb_data_steward');
    var businessApplicationStewardName = current.u_cmdb_data_steward.name + "";
    var technicalApplicationOwner = current.getValue('u_custodian');
    var technicalApplicationOwnerName = current.u_custodian.name + "";

    if (current.u_primary_capability.changes()) {
        var currentUserID = gs.getUserID();

        if (currentUserID == businessApplicationSteward) {
            current.work_notes = 'The Primary Capability field has been updated by the Technical owner representative ' + businessApplicationStewardName;
        } else if (currentUserID == technicalApplicationOwner) {
            current.work_notes = 'The Primary Capability field has been updated by the Technical Owner ' + technicalApplicationOwnerName;
        } else if (gs.hasRole('lbg_architecture_domain_admin') || gs.hasRole('lbg_architecture_domain_manager')) {
            var userName = gs.getUserDisplayName();
            current.work_notes = "The Primary Capability field has been updated by " + userName + ".";
        }
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar I have shared the part of the script that is relevant the issue I'm facing but the actual script is much larger and updates worknotes for when other fields are changed tooand it has all been done as IF statements.

So would ALL of the IF statements in the script or just the ones that relate to the primary capability field? 

@thrsdy1512 

do you have any other business rule running with current.update?

yes please use IF ELSE logic.

share the complete script.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader