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

Hi @JoaoV,

 

I would suggest to get unique elements before passing it to query.

 

 

 

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

var arrUtil = new global.ArrayUtil();
uniqueArray = arrUtil.unique(arr);  // get Unique elements

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

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

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

}

 

 

 

Thanks,

Sagar Pagar

 

The world works with ServiceNow