Unable to add record to table 'sys_user_grmember' via script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2016 06:49 AM
Have a Scheduled Job script to add users to a custom table, then also want to add the user to the 'sys_user_grmember' table, but it only will add users when the 'Can Create' tickbox is enabled. I've tried different ACLs on the table, running the scheduled job as the system admin account. Everything else with the script has been running fine.
This is the code I'm using to add the record into the table:
var addUsr = restGR.approver_name; //The user's username
var toGrp = '40931f7d6fde1a002915da55eb3ee45e';
var rec1 = new GlideRecord('sys_user_grmember');
rec1.initialize();
rec1.user = addUsr;
rec1.group = toGrp;
if(rec1.insert() == null)
{
gs.error("Failed to add: " + hrApprovers[i].userID + " to the HR Associate Approvers group.");
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2016 06:59 AM
You were very close... try the following:
var addUsr = restGR.approver_name; //The user's username
var toGrp = '40931f7d6fde1a002915da55eb3ee45e';
var rec1 = new GlideRecord('sys_user_grmember');
rec1.initialize();
rec1.user = getUsr(addUsr);
rec1.group = toGrp;
if(rec1.insert() == null)
{
gs.error("Failed to add: " + hrApprovers[i].userID + " to the HR Associate Approvers group.");
}
function getUsr(addUsr){
var gr = new GlideRecord('sys_user');
gr.addQuery('user_name',addUsr);
gr.query();
if(gr.next()){
return gr.sys_id;
}
}
Also keep in mind that group roles will not be added to user's when they are added to a group via script, so you may want to provide them with the correct roles as well. Let me know if you need help with that script.
Edit: Added user lookup... good catch @Christopher Johnson
Please mark helpful or correct if either or both.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2016 07:06 AM
Looking at the script, it appears that you are using the user's username to identify the user. You do have the sys_id of the group you want to add to, so that will identify the group, but you need the sys_id of the user since the user field on the sys_user_grmember table is a reference field that accepts the sys_id. You may want to do some logging to see what value you get for addUsr.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2016 07:20 AM
Thanks for the suggestions Aric and Christopher.
I've tried your suggestion Aric, but still failed to insert the record.
I've then tried your suggestion Christopher, with hard setting the SYS_ID for the user, but still fails to add the record. Then I commented out the add user value, but still failed to add the record.
Is there any way to get some better logging around this area?
Thanks
Matt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2016 07:34 AM
I tested the code I provided above in my own instance and it seems to work correctly. Please try the updated version based off of Chris's observation; if that still doesn't work I can only assume the group sys_id is wrong or there is a problem with restGR.approver_name.
Regardless, you should be getting entries in sys_user_grmember with user = '' and/or group = '' if there is a problem with a sys_id.
Try looking at the table and sorting by created date. That should help narrow down the source of your difficulties.