javascript if condition using "in" operator or indexOf

dfesenbek
Giga Contributor

I'm trying to determine if the logged in user is a member of an assignment group that was selected so I can set the "assigned to" to that user (auto assigns to user if they're in the assignment group).   I can't get either the condition "in" operator or indexOf() method to work.   I've also tried adding method toString() to VAR assignments without success.   Below is the code:

// Set assigned to if member of assignment group

var assignmentGroup = current.assignment_group;

var userGroups = gs.getUser().getMyGroups();

gs.log(">>> #1 assignmentGroup= " + assignmentGroup + " userGroups= " + userGroups);

if (assignmentGroup in userGroups) {         //(assignmentGroup.indexOf(userGroups) >= 0) { <doesn't work either

      gs.log(">>> #2 MATCH on Assignment Group!");

    current.assigned_to = gs.getUserID();

} else { gs.log(">>> #3 NO match on Assignment Group!"); }

I get the following log entries:

What am I doing wrong?   Thanks for any assistance.

1 ACCEPTED SOLUTION

Hi David,



Why not simply use:


if(gs.getUser().isMemberOf(current.assignment_group)){


//do something


}



harel


View solution in original post

4 REPLIES 4

dfesenbek
Giga Contributor

I failed to mention this is in a business rule, and the assignment groups is being set correctly.   You can see from the log entries that the assignmentGroup value (sys ID) does match the second value in userGroups, but the condition always returns false.


Hi David,



Why not simply use:


if(gs.getUser().isMemberOf(current.assignment_group)){


//do something


}



harel


dfesenbek
Giga Contributor

Figured out indexOf() issue... I had the parameters backwards on the indexOf() method.   The below condition works properly:


if (userGroups.indexOf(assignmentGroup) >= 0) {



If anyone knows why the "in" in the if condition doesn't work, I'd appreciate your reply.


I have not tried your code, but I am pretty sure you should not have spaces in there anyway:


if (assignmentGroupINuserGroups) {



harel