Worknotes being added twice is user is has multiple roles
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Ankur Bawiskar I have shared the full script in another comment. Theres no other business rules, I've checked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
try this optimized code
(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 userID = gs.getUserID();
function note(field, stewardText, ownerText) {
if (current[field].changes()) {
if (userID == businessApplicationSteward) {
current.work_notes = stewardText + businessApplicationStewardName;
} else if (userID == technicalApplicationOwner) {
current.work_notes = ownerText + technicalApplicationOwnerName;
} else if (gs.hasRole('architecture_domain_admin') || gs.hasRole('architecture_domain_manager')) {
current.work_notes = "The " + field + " field has been updated by " + gs.getUserDisplayName() + ".";
}
}
}
note('u_business_application_type', 'The Business Application Type field has been updated by the Technical owner representative ', 'The Business Application Type field has been updated by the Technical Owner ');
note('short_description', 'The Description field has been updated by the Technical owner representative ', 'The Description field has been updated by the Technical Owner ');
note('u_end_date', 'The End Date field has been updated by the Technical owner representative ', 'The End Date field has been updated by the Technical Owner ');
note('u_new_application_reason', 'The New Application Reason field has been updated by the Technical owner representative ', 'The New Application Reason field has been updated by the Technical Owner ');
note('u_primary_capability', 'The Primary Capability field has been updated by the Technical owner representative ', 'The Primary Capability field has been updated by the Technical Owner ');
note('start_date', 'The Start Date field has been updated by the Technical owner representative ', 'The Start Date field has been updated by the Technical Owner ');
note('u_strategic_status', 'The Strategic Status field has been updated by the Technical owner representative ', 'The Strategic Status field has been updated by the Technical Owner ');
note('u_target_hosting_type_ref', 'The Target Hosting Type field has been updated by the Technical owner representative ', 'The Target Hosting Type field has been updated by the Technical Owner ');
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
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.
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
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?