- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-03-2013 06:34 AM
I am building a form and related workflow to allocate roles based on groups selected from a slush bucket
My current script pulls the selected groups into an array and then inserts the values onto the sys_user_grmember table.
What i would like to do is say in the script "if this person is a member of that group then ignore that selection"
I'm just having trouble with the if statement, i'm pretty sure its nearly there but i'm missing something obvious with the IsMemberof()
var grp = current.variable_pool.ess_group.toString();
var array = grp.split(",");
for (var i=0; i<array.length; i++){
//insert the array values into the m2m table
var grpmember = new GlideRecord('sys_user_grmember');
grpmember.initialize();
grpmember.user = current.variable_pool.requested_for;
//check to see if this person is a member of that group and ignore it if they are
if (current.variable_pool.requested_for.isMemberOf(array<i>)){
grpmember.group = array<i>;
grpmember.insert();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2013 08:38 AM
Neil,
The isMemberOf() function is only available for server side user objects, gs.getUser(). The below script should do what you are asking; although, it is making an extra call to the server to check the group membership before attempting to create the record.
var grp = current.variable_pool.ess_group.toString();
var array = grp.split(",");
for (var i=0; i<array.length; i++) {
// Check to see if this user is a member of this group
var grMember1 = new GlideRecord('sys_user_grmember');
grMember1.addQuery('user', current.variable_pool.requested_for);
grMember1.addQuery('group', array<i>);
grMember1.query();
if (grMember1.next()) {
// Ignore if a group member
gs.print('Group membership already exists');
}
else {
// Create group membership
var grMember2 = new GlideRecord('sys_user_grmember');
grMember2.initialize();
grMember2.user = current.variable_pool.requested_for;
grMember2.group = array<i>;
grMember2.insert();
gs.print('Group membership created');
}
}
Hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2013 08:38 AM
Neil,
The isMemberOf() function is only available for server side user objects, gs.getUser(). The below script should do what you are asking; although, it is making an extra call to the server to check the group membership before attempting to create the record.
var grp = current.variable_pool.ess_group.toString();
var array = grp.split(",");
for (var i=0; i<array.length; i++) {
// Check to see if this user is a member of this group
var grMember1 = new GlideRecord('sys_user_grmember');
grMember1.addQuery('user', current.variable_pool.requested_for);
grMember1.addQuery('group', array<i>);
grMember1.query();
if (grMember1.next()) {
// Ignore if a group member
gs.print('Group membership already exists');
}
else {
// Create group membership
var grMember2 = new GlideRecord('sys_user_grmember');
grMember2.initialize();
grMember2.user = current.variable_pool.requested_for;
grMember2.group = array<i>;
grMember2.insert();
gs.print('Group membership created');
}
}
Hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-07-2013 12:59 AM
Works like a charm thanks very much Michael