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

M Iftikhar
Mega Sage

Hi there,

 

The duplicate worknotes are coming from the fact that your script allows more than one condition to evaluate as true at the same time. Since a single user can have multiple roles, more than one block runs and the work_notes field gets set twice.

A simple way to fix this is to control the flow so only one branch executes. You can do that by using else if instead of separate if statements, or by adding an additional check before writing to work_notes (for example, only update if it’s still empty in that transaction). This way even if the user has overlapping roles, the system will only log one clear worknote.

Keeping it structured like that will keep the logic simple and avoid the duplicates without needing heavy customization.

Hope this helps!
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.

Bhuvan
Kilo Patron

@thrsdy1512 

 

Why do you need to create 3 IF loops to make it work, replace it with IF and ELSE IF condition and it should work.

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

the actual script is much larger and also updates workntoes for other types of fields too, all following the same format as the snipped I have shared. 

Would I have to update ALL of the IF statements in the script or just the ones that relate to the problematic field? 

@thrsdy1512 

 

You can share the script and if you need, I can optimize and share it.

 

IF condition will run every time the condition is met, hence you are seeing multiple work notes being added if a user with multiple roles [Application Owner or Application Steward or has one of roles lbg_architecture_domain_admin, lbg_architecture_domain_manager] and field value changes. You can optimize it to IF and ELSE IF condition so that it will have only one work notes added. If needed you can use AND && OR || conditions in the IF and ELSE IF loop to create unique combinations based on your requirements.

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

dzmitryKon
Tera Contributor

Hi, there are multiple way how it could be done; 
simplest just to add some flag to track if work-notes were added notesAdded then set it if added and add to conditions;

(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+"";
    var notesAdded = false;
   

//If the field changes business app steward
    if (((current.u_primary_capability.changes() && gs.getUserID() == businessApplicationSteward))&& !(notesAdded) ){
            current.work_notes = 'The Primary Capability field has been updated by the Technical owner representative '+ businessApplicationStewardName;
            notesAdded = true;

    }

    //If the field changes  by the Technical Owner
    if (((current.u_primary_capability.changes() && gs.getUserID() == technicalApplicationOwner)) && !(notesAdded) ) {

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

    //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'))) && !(notesAdded) ){
    var userName = gs.getUserDisplayName();
    current.work_notes = "The Primary Capability field has been updated by " + userName + ".";
     notesAdded = true;

}

 

})(current, previous);

 
Overall this script could be optimised even more: 
1. i would move "u_primary_capability" - "changes" into business rule condition itself, so if not changed BR is not triggered at all

2. to use return

ar userSysId = gs.getUserID();
  var userName  = gs.getUserDisplayName();

  // Pull IDs safely (these are reference fields; getValue returns sys_id)
  var stewardId = current.getValue('u_cmdb_data_steward');
  var ownerId   = current.getValue('u_custodian');

  // Optional: pull display values defensively (in case refs are empty)
  var stewardName = current.getDisplayValue('u_cmdb_data_steward');
  var ownerName   = current.getDisplayValue('u_custodian') ;

  // Priority 1: architecture roles
  if (gs.hasRole('lbg_architecture_domain_admin') || gs.hasRole('lbg_architecture_domain_manager')) {
    current.work_notes = 'The Primary Capability field has been updated by ' + userName + '.';
    return; // prevent other branches from running
  }

  // Priority 2: technical owner
  if (userSysId === ownerId) {
    current.work_notes = 'The Primary Capability field has been updated by the Technical Owner ' + ownerName + '.';
    return;
  }

  // Priority 3: business application steward
  if (userSysId === stewardId) {
    current.work_notes = 'The Primary Capability field has been updated by the Business Application Steward ' + stewardName + '.';
    return;
  }

 

I haven't run the code, but you can get the idea.
Please mark solved/helpful if solved/helpful 🙂