-
Notifications
You must be signed in to change notification settings - Fork 908
Update Watchlist of any Table based on payload #1940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shreyawani98
merged 3 commits into
ServiceNowDevProgram:main
from
durgeshservicenow-hub:update-watchlist-from-json
Oct 8, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Server-Side Components/Script Includes/UpdateWatchlistFromJSON/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
| //Passing List of String of SysId of users | ||
| var payload = '43435efdsre4t5953439434,43434343436fdfsd343,frtgr6565hg676767gt'; | ||
| updateWatchlistFromJSON('incident','a1b2c3d4e5f678901234567890abcdef', payload); | ||
|
|
||
| //Passing Array of String of SysId of users | ||
| var payload = '[43435efdsre4t5953439434,43434343436fdfsd343]'; | ||
| updateWatchlistFromJSON('incident','a1b2c3d4e5f678901234567890abcdef', payload); |
34 changes: 34 additions & 0 deletions
34
Server-Side Components/Script Includes/UpdateWatchlistFromJSON/script.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| function updateWatchlistFromJSON(tableName, recordSysId, jsonPayload) { | ||
| // 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+": " + recordSysId); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.