- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2017 01:28 PM
I am trying to run a script that looks for active users with the title 'Customer' and add them to a group, GroupA, if they are not already members. Not sure what I am missing. Here is what I have so far:
var rec = new GlideRecord('sys_user');
rec.addQuery('title', '=', 'CUSTOMER');
rec.addActiveQuery();
rec.query();
while (rec.next()){
var rec1 = new GlideRecord('sys_user_grmember');
if (rec1.user !== rec.sy_id){
rec1.initialize();
rec1.user = rec.sys_id;
rec1.group.setDisplayValue('GroupA');
rec1.insert();
gs.log('##### ' + rec.getDisplayValue('user_name') + ' ADDED');
} else {
//gs.log('##### ' + rec.getDisplayValue('user_name') + ' already exists');
}
}
Thoughts?
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2017 01:44 PM
Hi Machael,
Try this way.
var rec = new GlideRecord('sys_user');
rec.addQuery('title', '=', 'CUSTOMER');
rec.addActiveQuery();
rec.query();
while (rec.next())
{
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('user' , rec.sys_id);
rec1.addQuery('group' , '9005cb35db91c700c1fa7d9ebf96193a'); // put the sys_id of "GroupA" here
rec1.query();
if(!rec1.next())
{
rec1.initialize();
rec1.user = rec.sys_id;
rec1.group = '9005cb35db91c700c1fa7d9ebf96193a'; // put the sys_id of "GroupA" here
rec1.insert();
gs.log("Group A record inserted");
}
}
Here instead of hardcoding the group sys_id you can get it from property by defining in Properties.
Regards,
Sagar
PS: Please mark as helpful or correct based on impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2017 01:44 PM
Hi Machael,
Try this way.
var rec = new GlideRecord('sys_user');
rec.addQuery('title', '=', 'CUSTOMER');
rec.addActiveQuery();
rec.query();
while (rec.next())
{
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('user' , rec.sys_id);
rec1.addQuery('group' , '9005cb35db91c700c1fa7d9ebf96193a'); // put the sys_id of "GroupA" here
rec1.query();
if(!rec1.next())
{
rec1.initialize();
rec1.user = rec.sys_id;
rec1.group = '9005cb35db91c700c1fa7d9ebf96193a'; // put the sys_id of "GroupA" here
rec1.insert();
gs.log("Group A record inserted");
}
}
Here instead of hardcoding the group sys_id you can get it from property by defining in Properties.
Regards,
Sagar
PS: Please mark as helpful or correct based on impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2017 02:00 PM
you also have a typo here: if (rec1.user !== rec.sy_id){ << sys_id
And rather than hard coding a sys_id, look up the group in sys_user_group to find its sys_id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2017 02:16 PM
Hi Micheal,
You can try this code. I suggest you to setLimit on the number of user records you are modifying.
var userRec = new GlideRecord('sys_user');
userRec.addActiveQuery();
userRec.addQuery('title', 'CUSTOMER');
userRec.setLimit(1);
userRec.query();
while(userRec.next()) {
usr = gs.getUser().getUserByID(userRec.user_name);
isMemeberCheck = isMemberOfGroup(usr,groupAsys_id);
if(isMemeberCheck == false)
addToGroup(usr,groupAsys_id);
}
function isMemberOfGroup (usr,group) {
gs.info("isMemeberCheck: "+usr.isMemberOf(group));
return usr.isMemberOf(group);
}
function addToGroup(usr,group) {
var gr = new GlideRecord('sys_user_grmember');
gr.user = usr;
gr.group = group; //sys_id of Group A
gr.insert();
}
Best,
Darshak