Mohit Kaushik
Mega Sage
Mega Sage

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

Link to my Virtual Agent implementation article

Comments
New Developer_S
Giga Sage

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

Mohit Kaushik
Mega Sage
Mega Sage

@New Developer_S

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.

 

 

Version history
Last update:
‎10-13-2022 05:51 AM
Updated by:
Contributors