Query a true/false field and if true, add user to a user group

Tim60
Kilo Explorer

I have a field in a form on the `change_task` table.

The form has a true/false checkbox. There is already a business rule that checks it based on other conditions. And that works correctly.

What I want to do is if that checkbox is marked (aka `== true`), then the email in the field next to that checkbox, gets added to a specific group.

Here's what I have below. I get no error messages. And not sure how to make the script work correctly.

if (current.u_custom_checkbox == true) {
  var group = new GlideRecord("sys_user");
  group.addQuery("group", "ldka4465tgsfgsgtrrtsret"); // sys_id of user group
  group.addQuery("user", current.u_custom_url_field_next_to_checkbox); // this is the field that holds the email address of the person who should be added to the group
  group.addQuery("user", current.sys_id); // grab the sys_id associated witht that email???? That is my intention, but I don't think it is working
  group.query();
}
if (!group.next()) {
  group.initalize();
  group.group = "ldka4465tgsfgsgtrrtsret";
  group.user = current.sys_id;
  group.insert();
} else {
  var removeUserFromGroup = new GlideRecord("sys_user");
  removeUserFromGroup.addQuery("group", "ldka4465tgsfgsgtrrtsret");
  removeUserFromGroup.addQuery("user", current.sys_id);
  removeUserFromGroup.query();
  if (removeUserFromGroup.next()) {
    removeUserFromGroup.deleteRecord();
  }
}
6 REPLIES 6

Thank you for the information. It's important to note that I have been getting red toaster messages on scripts that don't work. It is unclear to me what logic/conditions in ServiceNow has allowed this so far.

Dave Smith1
ServiceNow Employee

Unless you add in something that informs you, you'll still be stuck in the dark. This isn't really a ServiceNow thing, it's defensive programming technique.

You know how to use the Script Debugger?  You attended one of SN's scripting courses at all?