How to check user is already added in List type variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2024 01:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2024 01:44 AM
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2024 02:09 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2024 02:12 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2024 02:22 AM
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.');
}
}