How to check user is already added in List type variable

Community Alums
Not applicable

Hi All,
I have a requirement where I have to check if a particular user is available in a list-type variable or not; if not, I need to add it.
How do I do this?

Any help is appreciated.
Thank you.

6 REPLIES 6

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Community Alums 

 

What do you mean by  I have to check if a particular user is available in a list-type variable or not?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Community Alums
Not applicable

Hi @Dr Atul G- LNG 
eg- I want to add 'Abel Tuter ' in list, so for that first I need to check Abel Tuter is already added or not

 

Maddysunil
Kilo Sage

@Community Alums 

You can try with sample script:

 

// Retrieve the list-type variable (replace 'list_variable' with the actual name of your variable)
var userList = current.list_variable.getDisplayValue().split(',');

// Check if the user is already in the list
var userToCheck = 'user_to_check'; // Replace 'user_to_check' with the user you want to check
var isUserAlreadyAdded = userList.includes(userToCheck);

if (isUserAlreadyAdded) {
    gs.info(userToCheck + ' is already in the list.');
} else {
    gs.info(userToCheck + ' is not in the list.');
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Community Alums
Not applicable

Hi @Maddysunil 
I used below script and 'Abel Tuter' was already added in List variable. still the result is false showing as 'Abel Tuter'  is not in the list.

 

var gr =new GlideRecord('incident');
gr.addquery('sys_id','a623cdb073a023002728660c4cf6a768');
gr.query();
if(gr.next()){
var userToCheck = 'Abel Tuter';

var currentList = gr.u_list.getDisplayValue().split(',');
var isUserAlreadyAdded = currentList.includes(userToCheck);
if (isUserAlreadyAdded) {
gs.info(userToCheck + ' is already in the list.');
} else {
gs.info(userToCheck + ' is not in the list.');
}

}