check if roles from gliderecord output are in array

JoaoV
Tera Expert

Hi guys,

 

I need to check if the roles from a gliderecord output are inside my array.

 

Example:

 

var gr = new GlideRecord("sys_group_has_role");

gr.addQuery("group", current.sys_id);

gr.query();

while (gr.next()) {

     !!!CHECK if gr.role is inside my arr!!!

}

 

 

I have tried to add the array directly to the query but it is useless because it checks if there are ocurrences but I will not know which one matches and which one doesn't

 

I have tried indexOf but it is not working for me:

 

while (gr.next()) {
     if (arr.indexOf(gr.role)) {
 
Any tips?
 
Thanks
2 ACCEPTED SOLUTIONS

RaghavSh
Kilo Patron
 if (arr.indexOf(gr.role)>-1) {
} // if your array has sysid of roles.
 
if (arr.indexOf(gr.role.name)>-1) {
} // if your array has name of roles.
 

Raghav
MVP 2023
LinkedIn

View solution in original post

Thanks @RaghavSh it works great. Before of that I had to convert the array to string!

View solution in original post

5 REPLIES 5

RaghavSh
Kilo Patron
 if (arr.indexOf(gr.role)>-1) {
} // if your array has sysid of roles.
 
if (arr.indexOf(gr.role.name)>-1) {
} // if your array has name of roles.
 

Raghav
MVP 2023
LinkedIn

Thanks @RaghavSh it works great. Before of that I had to convert the array to string!

Sagar Pagar
Tera Patron

Hi @JoaoV,

 

Try this updated scritps -

 

 

var arr = ["admin", "test", "impersonator"];

for (var i = 0; i < arr.length; i++) {

	var gr = new GlideRecord("sys_group_has_role");
	gr.addQuery("group", current.getUniqueValue());
	gr.addQuery("role.name", arr[i]);
	gr.query();
	gs.info(gr.getRowCount());
	while (gr.next()) {

		//     !!!CHECK if gr.role is inside my arr!!!
		gs.info(arr[i]);
	}

}

 

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar , Thanks for that but as I said in my post I cannot pass the entire array in the query because I will not now which is duplicate and which not.