ArrayUtil indexOf/contains not working

Rachel55
Mega Guru

Hello.  I am trying to create a script for a login rule.  I am testing in backgroud scripts.  The script is supposed to look for a specific role for the logged in user.  Without the if statement, the array will print, and I can see that it contains itil. When the if statement is added, it gives the error message "Javascript compiler exception: Java class "[Ljava.lang.String;" has no public instance field or method named "size". (sys_script_include.fb32a2d8c0a80a6000e907036f484b5b.script; line 61) in:".  I get the same error whether I am using "contains" or "indexOf".

 

var arrUtil = new ArrayUtil();

var u = gs.getUser();

var myArray = u.getRoles().toString().split(",");

if (arrUtil.contains(myArray, "itil")){

gs.print("yes");

}

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

gs.print(myArray[i]);

}

12 REPLIES 12

Tiffany Royer
Giga Guru

You can just check if the user has the 'itil' role, without getting all their roles.

gs.hasRole('itil')

 

If this was able to answer your question, please mark it as the Accepted Solution so the question will appear as resolved for other users who may have a similar question in the future.

Brian Lancaster
Tera Sage

Agree with @Tiffany Royer however get all the roles the user has you can remove line 2 and 3 with the following code.  When I tried to run what you had in scripts back ground it was erroring our.

var myArray = gs.getUser().getRoles();

Bala Krishna1
Kilo Contributor

If you are not in scoped application try using below code:

 var u = gs.getUser();
var myArray = u.getRoles().toString().split(",");

if (myArray.indexOf( "itil"))

{

gs.print("yes");

}

for (i=0; i<myArray.length;i++)

{

gs.print(myArray[i]);

}

 

If you in scoped application arrayUtil cannot be accessed and you can replace gs.print with gs.info.

var myArray = new global.ArrayUtil(); //for scoped application

myArray = u.getRoles().toString().split(",");