The CreatorCon Call for Content is officially open! Get started here.

Business rule to remove inactive users from watchlists

Jesus Nava
Tera Guru

Hello experts, please I need your help, I'm creating a BR that removes inactive users from a watchlist, this is what I have and I found out something, if the watchlist has mre than one user, this does not work, this works when there is just a single user =( what do I need to modify as well?

 

JesusNava_0-1677701137382.png

This is working when I have the user just in "Watch list", how can I add another condition to check another field called: Worknotes list

JesusNava_2-1677701484584.png

 

how can I add another condition to remove the user from Worknotes watch list too? I tried:

JesusNava_1-1677701276566.png

but it is not working, what am I doing wrong? 

I need to add 2 more custom watchlists, so I will need to add a total of 4 conditions to remove the inactive users from those 4 watch lists once the other 2 are created, could you please guide me?

Thank you

 

1 ACCEPTED SOLUTION

Hi,

Thanks. So you'd need to take the current watch_list and make it into an array and if they're found, remove them, and then set the watch_list to the final value (could be none or could be all the other names still there but the inactive user is removed).

So something like:

 

 

var array = tsks.watch_list.toString().split(',');
if (array.indexOf(current.getValue('sys_id')) > -1) {
var index = array.indexOf(current.getValue('sys_id'));
array.splice(index, 1);
tsks.watch_list = array;
}
var array2 = tsks.work_notes_list.toString().split(',');
if (array2.indexOf(current.getValue('sys_id')) > -1) {
var index2 = array2.indexOf(current.getValue('sys_id'));
array2.splice(index2, 1);
tsks.work_notes_list = array2;
}
tsks.update();

 

 

The above is an example to add to your code after the while(tsks.next()) { line.

Please follow similar format array3, array4, etc. as you need. There are other ways to do this, but I've provided one example. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hello,

Well, it's not an "else if"...which means it only runs that portion of code IF they don't meet the other if statement. So you'd have multiple if statements, it appears. Additionally, most of those are "list" fields...not a single field value. So the watch list could "contain" the current user, but may not be equal to it.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello Allen, you're right, if I have more users in the watchlists, it does not work, just if there is a single user there.

Regards

Hi,

 

You need to use indexOf in the watchlist to find the value and remove the specific user.

 

refer below content

 

https://www.servicenow.com/community/itsm-forum/to-remove-element-from-an-array/m-p/810379

Hi,

Thanks. So you'd need to take the current watch_list and make it into an array and if they're found, remove them, and then set the watch_list to the final value (could be none or could be all the other names still there but the inactive user is removed).

So something like:

 

 

var array = tsks.watch_list.toString().split(',');
if (array.indexOf(current.getValue('sys_id')) > -1) {
var index = array.indexOf(current.getValue('sys_id'));
array.splice(index, 1);
tsks.watch_list = array;
}
var array2 = tsks.work_notes_list.toString().split(',');
if (array2.indexOf(current.getValue('sys_id')) > -1) {
var index2 = array2.indexOf(current.getValue('sys_id'));
array2.splice(index2, 1);
tsks.work_notes_list = array2;
}
tsks.update();

 

 

The above is an example to add to your code after the while(tsks.next()) { line.

Please follow similar format array3, array4, etc. as you need. There are other ways to do this, but I've provided one example. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!