diff --git a/Client-Side Components/Client Scripts/Auto populate watchlist/README.md b/Client-Side Components/Client Scripts/Auto populate watchlist/README.md new file mode 100644 index 0000000000..783f88730e --- /dev/null +++ b/Client-Side Components/Client Scripts/Auto populate watchlist/README.md @@ -0,0 +1,12 @@ +## Automatically Populate Watch List When Assigned-To Changes + +When the **Assigned To** field changes, this client script updates the **Watch list** to include the **Opened By**, **previous assignees**, and **new assignee**. +- Help maintains visibility of all users involved on incident record. +- Helps track assigned-to transitions and ensures clarity during assignment change. +- A confirmation message appears when the change is reflected in the Watch list. +- Specific script for use in the Incident record form in ServiceNow but can be used for other record forms too. +--- + +![Auto populate watchlist](./Screenshot_clientscript_autopopulate_watchlist.png) + +--- diff --git a/Client-Side Components/Client Scripts/Auto populate watchlist/Screenshot_clientscript_autopopulate_watchlist.png b/Client-Side Components/Client Scripts/Auto populate watchlist/Screenshot_clientscript_autopopulate_watchlist.png new file mode 100644 index 0000000000..f467e56141 Binary files /dev/null and b/Client-Side Components/Client Scripts/Auto populate watchlist/Screenshot_clientscript_autopopulate_watchlist.png differ diff --git a/Client-Side Components/Client Scripts/Auto populate watchlist/script.js b/Client-Side Components/Client Scripts/Auto populate watchlist/script.js new file mode 100644 index 0000000000..5b6f01a753 --- /dev/null +++ b/Client-Side Components/Client Scripts/Auto populate watchlist/script.js @@ -0,0 +1,36 @@ +function onChange(control, oldValue, newValue, isLoading, isTemplate) { + if (isLoading) return; + + // Get existing watch list first + var existing = g_form.getValue('watch_list'); + var watchList = existing ? existing.split(',') : []; + + // Get key values + var openedBy = g_form.getValue('opened_by'); + var prevAssigned = oldValue; + var newAssigned = newValue; + + if (!openedBy) { + openedBy = g_user.userID; // current logged-in user + } + + // Add new users if not already present + if (openedBy) watchList.push(openedBy); + if (prevAssigned) watchList.push(prevAssigned); + if (newAssigned) watchList.push(newAssigned); + + // Remove duplicates + var uniqueList = []; + for (var i = 0; i < watchList.length; i++) { + var val = watchList[i]; + if (val && uniqueList.indexOf(val) === -1) { + uniqueList.push(val); + } + } + + // Update watch list + g_form.setValue('watch_list', uniqueList.join(',')); + + // Display confirmation message + g_form.addInfoMessage(" Your Assigned To change is reflected and added in Watch list"); +}