The CreatorCon Call for Content is officially open! Get started here.

Worknotes being added twice is user is has multiple roles

Kiran_25
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);

13 REPLIES 13

@Kiran_25 

 

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

Sure, heres the complete script:

 

(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 Business Application Type field changes and was updated by the Technical owner representative

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

            current.work_notes = 'The Business Application Type field has been updated by the Technical owner representative '+ businessApplicationStewardName;

    }

   

    //If the Business Application Type field changes and was updated by the Technical Owner

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

            current.work_notes = 'The Business Application Type field has been updated by the Technical Owner '+ technicalApplicationOwnerName;

    }

   

   

    //If the Description field changes and was updated by the Technical owner representative

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

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

    }

   

    //If the Description field changes and was updated by the Technical Owner

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

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

    }

   

    //If the End Date field changes and was updated by the Technical owner representative

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

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

    }

   

    //If the End Date field changes and was updated by the Technical Owner

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

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

    }

   

    //If the New Application Reason field changes and was updated by the Technical owner representative

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

            current.work_notes = 'The New Application Reason field has been updated by the Technical owner representative '+ businessApplicationStewardName;

    }

   

    //If the New Application Reason field changes and was updated by the Technical Owner

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

            current.work_notes = 'The New Application Reason field has been updated by the Technical Owner '+ technicalApplicationOwnerName;

    }

   

    //If the Primary Capability field changes and was updated by the Technical owner representative

    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 Primary Capability field changes and was updated 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 Primary Capability changes and is updated by users with the architecture_domain_admin OR architecture_domain_manager role

else if (current.u_primary_capability.changes() &&

    (gs.hasRole('architecture_domain_admin') || gs.hasRole('architecture_domain_manager'))) {

    var userName = gs.getUserDisplayName();

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

   

    }

   

    //If the Start Date field changes and was updated by the Technical owner representative

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

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

    }

   

    //If the Start Date field changes and was updated by the Technical Owner

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

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

    }

   

    //If the Strategic Status field changes and was updated by the Technical owner representative

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

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

    }

   

    //If the Strategic Status field changes and was updated by the Technical Owner

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

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

    }

   

    //If the Target Hosting Type field changes and was updated by the Technical owner representative

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

            current.work_notes = 'The Target Hosting Type field has been updated by the Technical owner representative '+ businessApplicationStewardName;

    }

   

    //If the Target Hosting Type field changes and was updated by the Technical Owner

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

            current.work_notes = 'The Target Hosting Type field has been updated by the Technical Owner '+ technicalApplicationOwnerName;

    }

   

})(current, previous);

@Kiran_25 

 

I can see @Ankur Bawiskar  already shared the optimized version.

 

Please test and share your feedback as IF and ELSE IF should make sure work notes are added only once. Please note, in the IF and ELSE IF loop, if multiple conditions evaluate to true, only the first condition is evaluated. If you want work notes to uniquely identify who made the update, group loop conditions as per your requirements.

 

As per community guidelines, you can accept more than one answer as accepted solution. If my response helped to guide you or 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 🙂