- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
10-06-2021 01:04 PM - edited 10-13-2022 05:51 AM
This script will help you to remove specific element from a list of unique elements for eg sysIds.
Here I will take sys_id of the user a specific value and how can we remove it from a watch list present on an incident form.
We can write a Background script, Fix script or those use Xplore they can execute this script from there as well.
var sysId = '32 bit sysId'; //pass the 32 bit sys_id of the user you want to remove.
var inc = new GlideRecord('incident');
inc.get('number','INC0010112');// pass the correct number to get the record.
var arr = inc.watch_list.split(','); //split the watch list values and put them in an array.
var idx = arr.indexOf(sysId); //get the index of sys_id of user you want to remove from watch list.
arr.splice(idx,1); // Remove the specific user from array. idx is the index from which you want to remove and '1' is the number of elements you want to remove.
inc.watch_list = arr.join(','); // join the array and set the values back to watch list.
inc.setWorkflow(false); //if you don't want any BR to run for this update.
inc.update(); // update the record
If you like this piece of code the please Bookmark it, mark it Helpful and Subscribe it for further edition of methods.
Thanks,
Mohit Kaushik
- 3,704 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Mohit,
Thanks for this post. How could we execute the code when we have multiple incidents and we need to remove a single user from the watch list of all those incidents ? Can you help with the code ?
br,
tia
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
You can modify the query for your GlideRecord to achieve this as shown below:
var sysId = '32 bit sysId'; //pass the 32 bit sys_id of the user you want to remove.
var inc = new GlideRecord('incident');
inc.addEncodedQuery('numberIN'+'Comma separated incident numbers for which you want to remove the user');
inc.query();
while(inc.next())
{
var arr = inc.getValue('watch_list').split(','); //split the watch list values and put them in an array.
var idx = arr.indexOf(sysId); //get the index of sys_id of user you want to remove from watch list.
arr.splice(idx,1); // Remove the specific user from array. idx is the index from which you want to remove and '1' is the number of elements you want to remove.
inc.watch_list = arr.join(','); // join the array and set the values back to watch list.
inc.setWorkflow(false); //if you don't want any BR to run for this update.
inc.update(); // update the record
}
In order to test your logic put a log to print incident numbers first and then proceed with the user's removal from watchlist.