ArrayUtil indexOf/contains not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2021 01:52 PM
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]);
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 06:01 AM
Now this If statement is not working. It works if you split it into two separate if statements, but not with the &&.
var myArray = gs.getUser().getRoles().toString().split(",");
if(gs.getUser().hasRole('itil')) && (myArray.length>0){
gs.info('yes');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 06:07 AM
try this
var myArray = gs.getUser().getRoles().toString().split(",");
if(gs.getUser().hasRole('itil') && myArray.length>0){
gs.info('yes');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 06:21 AM
Now it prints yes no matter what I put in the hasRole parentheses.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 06:30 AM
because admin have all the role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 06:33 AM
I put nonsense in the parentheses and it still prints yes.