User not getting added to group in Run Script activity
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 09:55 PM
HI , I have a workflow which is used to add or remove users from assignment groups.
The script below that i am using is creating a record in the sys_grp_member table but only user is getting populated , te group is coming as empty.
I tried putting logs for both user and group and verified that for both sys_id's are coming.
The workflow.scratchpad.grp is defined in previous activity of the workflow. And here in line no. 7 when I'm putting setDisplayValue then only a record is getting created for which group is empty , Otherwise no record gets created.
var type = current.variables.var_what_are_you_looking_to_do;
var grp = workflow.scratchpad.grp;
var user1 = current.variables.var_hr_who_do_you_want_to_add_remove_access_for.user;
if (type == 'Add User Access') {
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group = grp.setDisplayValue();
gr.user = user1;
gs.log('AM1' +grp +user1);
gr.insert();
workflow.scratchpad.Status = 'Add';
}
else if (type == 'Remove User Access') {
gr.addQuery('group', grp);
gr.addQuery('user', datta);
gr.query();
if (gr.next()) {
gr.deleteRecord();
workflow.scratchpad.Status = 'Remove';
}
}
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 10:06 PM
Hi Amey,
Just try to set the value directly instead of display value as shown below:
var grp = workflow.scratchpad.grp.toString();
// Your code
gr.group = grp;
Please mark this as correct and helpful if it resolved the query or lead you in right direction.
Thanks,
Mohit Kaushik
Community Rising Star 2022
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 10:11 PM
Hi Mohit ,
I tried updating like this , but still group is coming as empty.
var grp = workflow.scratchpad.grp.toString();
// My code
gr.group = grp;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 10:10 PM
Hi,
2 things
1) if the workflow.scratchpad.grp is storing group sysId then do this
gr.group = grp;
OR
2) if the workflow.scratchpad.grp is storing group name then do this
gr.setDisplyValue('group',grp);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 10:25 PM
var type = current.variables.var_what_are_you_looking_to_do;
var grp = workflow.scratchpad.grp;
var user1 = current.variables.var_hr_who_do_you_want_to_add_remove_access_for.user;
if (type == 'Add User Access') {
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.setValue("group", grp); // SHould be only grp as you are setting reference field, and you have sys_id
gr.setValue("user",user1);// Please use setValue as per best practice
gs.log('AM1' +grp +user1);
gr.insert();
workflow.scratchpad.Status = 'Add';
}
else if (type == 'Remove User Access') {
gr.addQuery('group', grp);
gr.addQuery('user', datta);
gr.query();
if (gr.next()) {
gr.deleteRecord();
workflow.scratchpad.Status = 'Remove';
}
}
Copy as is
Aman Kumar