From 74bd1273d709a80027de24cad5c668b1275e9a78 Mon Sep 17 00:00:00 2001 From: Durgesh ahire Date: Wed, 8 Oct 2025 11:05:36 +0530 Subject: [PATCH 1/3] Update Watchlist of any Table based on payload --- .../UpdateWatchlistFromJSON/README.md | 13 +++++++++ .../UpdateWatchlistFromJSON/script.js | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md create mode 100644 Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js diff --git a/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md b/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md new file mode 100644 index 0000000000..33e23468ce --- /dev/null +++ b/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md @@ -0,0 +1,13 @@ +UseCase - + +Update the watchlist of any table record with the provided JSON payload which should maintain the previous watchlist user and add new one from the payload + +Payload can be Array, String, List of String + +// 🧩 Usage Example (JSON Payload) +var payload = '43435efdsre4t5953439434,43434343436fdfsd343,frtgr6565hg676767gt'; +updateWatchlistFromJSON('incident','a1b2c3d4e5f678901234567890abcdef', payload); + +// 🧩 Usage Example (JSON Payload) +var payload = '[43435efdsre4t5953439434,43434343436fdfsd343]'; +updateWatchlistFromJSON('incident','a1b2c3d4e5f678901234567890abcdef', payload); \ No newline at end of file diff --git a/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js b/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js new file mode 100644 index 0000000000..fbb78be0ff --- /dev/null +++ b/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js @@ -0,0 +1,28 @@ +function updateWatchlistFromJSON(tableName, recordSysId, jsonPayload) { + // Check for valid 'watch_list' key + if (jsonPayload) { + gs.error("JSON must not be empty"); + return; + } + var data = JSON.parse(jsonPayload); + var newWatchers = Array.isArray(data) ? data : data.split(','); + + var gr = new GlideRecord(tableName); + if (gr.get(recordSysId)) { + var existingWatchlist = gr.watch_list ? gr.watch_list.split(',') : []; + + for (var i = 0; i < newWatchers.length; i++) { + var watcher = newWatchers[i]; + existingWatchlist.push(watcher); + } + + } + var watcherSet = new Set(existingWatchlist); + gr.watch_list = Array.from(watcherSet).join(','); + gr.update(); + + gs.info("Watchlist updated successfully for Table "+tableName+": " + gr.number); + } else { + gs.error("Record not found for Table "+tableName+": " + incidentSysId); + } +} From 8b06c4498f4618eaed4d4ecec45ad2a0d696f1da Mon Sep 17 00:00:00 2001 From: Durgesh ahire Date: Wed, 8 Oct 2025 11:07:27 +0530 Subject: [PATCH 2/3] Update Watchlist of any Table based on payload --- .../Script Includes/UpdateWatchlistFromJSON/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md b/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md index 33e23468ce..2257a7d41e 100644 --- a/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md +++ b/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md @@ -4,10 +4,10 @@ Update the watchlist of any table record with the provided JSON payload which sh Payload can be Array, String, List of String -// 🧩 Usage Example (JSON Payload) +//Passing List of String of SysId of users var payload = '43435efdsre4t5953439434,43434343436fdfsd343,frtgr6565hg676767gt'; updateWatchlistFromJSON('incident','a1b2c3d4e5f678901234567890abcdef', payload); -// 🧩 Usage Example (JSON Payload) +//Passing Array of String of SysId of users var payload = '[43435efdsre4t5953439434,43434343436fdfsd343]'; updateWatchlistFromJSON('incident','a1b2c3d4e5f678901234567890abcdef', payload); \ No newline at end of file From da980b1ce4db6266b9d5695d472eb0cb6d60780f Mon Sep 17 00:00:00 2001 From: Durgesh ahire Date: Wed, 8 Oct 2025 11:20:49 +0530 Subject: [PATCH 3/3] Update Watchlist of any Table based on payload --- .../UpdateWatchlistFromJSON/script.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js b/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js index fbb78be0ff..781113a2d6 100644 --- a/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js +++ b/Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js @@ -1,28 +1,34 @@ function updateWatchlistFromJSON(tableName, recordSysId, jsonPayload) { - // Check for valid 'watch_list' key + // Check for valid 'watch_list' data if (jsonPayload) { gs.error("JSON must not be empty"); return; } var data = JSON.parse(jsonPayload); + // Check if data is array else convert List of string to array var newWatchers = Array.isArray(data) ? data : data.split(','); + // fetch the record by table name and record sys id var gr = new GlideRecord(tableName); if (gr.get(recordSysId)) { + // get the existing watchlist data var existingWatchlist = gr.watch_list ? gr.watch_list.split(',') : []; - + + // Add the new watchlist sys ids into exisitng watchlist for (var i = 0; i < newWatchers.length; i++) { var watcher = newWatchers[i]; existingWatchlist.push(watcher); } } + // Remove the duplicate by using SET var watcherSet = new Set(existingWatchlist); + // Update the newlist into watchlist gr.watch_list = Array.from(watcherSet).join(','); gr.update(); gs.info("Watchlist updated successfully for Table "+tableName+": " + gr.number); } else { - gs.error("Record not found for Table "+tableName+": " + incidentSysId); + gs.error("Record not found for Table "+tableName+": " + recordSysId); } }