Unable to add record to table 'sys_user_grmember' via script

mattsaario
Giga Contributor

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.");

        }

8 REPLIES 8

See if you can run your script through Scripts - Background (be sure to elevate your privileges to security_admin in order to see the Scripts - Background module first). Then you can test the portions of your script to make sure it is working and gathering the appropriate values you need.



You mentioned that you have a custom table. Is the approver_name field really u_approver_name? Is that field a reference field on the table, or just a plain text field? I have leveraged similar functionality on the groups table to add and remove the group manager to a group. Let me know if this type of functionality is something you could use and I will share.


Hi Matt,



For troubleshooting issues like this I typically sprinkle in some gs.print() statements and run it as a background script. That way the output of your print statements writes to the console for you to see. I suspect the issue is the input to the script being incorrect, or a before business rule if you aren't seeing any errors.



var restGr = {};


restGr.approver_name = 'jared.healy';




var user = gs.getUser().getUserByID(restGr.approver_name);


gs.print("User is: " + user.getFullName() + ", and sys_id: " + user.getID());




var memberGr = new GlideRecord('sys_user_grmember');


memberGr.initialize();


memberGr.setValue('group', 'd625dccec0a8016700a222a0f7900d06'); // Hard coded group to add them to


memberGr.setValue('user', user.getID());


var memberID = memberGr.insert();




if (!memberID) {


  gs.print("Failed to add: " + user.getFullName() + " to the HR Associate Approvers group.");


} else {


  gs.print('Created group membership record: ' + memberID);


}



Here's my script output. I'm just using a group from our dev instance so the ID is different. And I'm hard coding the user value up front to simulate what should be passed in. If this block of code executes as expected, then the error is before or after the actual insert of the record. Then you could add some additional log messaging up front to write out the "restGr" value to confirm it has what you think it does.



*** Script: User is: Jared Healy, and sys_id: 70b0e6090fdc82803d40f2a362050e18


*** Script: Created group membership record: 878184e60fae1200fc35f2a362050e96



Are you able to insert a record with this method?


Thanks everyone for the feedback and help.



Sorted the issue out by using the Background Scripts and just using the piece of code.



Turns out the scheduled job was running in a Scooped Application, and there is a security restraint around that - Problem fixed.



Once again thanks everyone for your help


xiaix
Tera Guru

I too can't seem to get this to insert new records.  I'm full admin with security_admin active.  This is a "Fix It" script I'm running:

 

 

I even tried running this in a Background Script... records didn't add that way either.