The CreatorCon Call for Content is officially open! Get started here.

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

sachin_namjoshi
Kilo Patron
Kilo Patron

 

what table you are running business rule?

 

You need to update your addQuery()

 

group.addQuery("user", current.u_custom_url_field_next_to_checkbox); // you need to pass user sys_id instead of email

 

You don't need below line in your code

 

group.addQuery("user", current.sys_id);

 

You should add sys_user reference field in your addQuery.

Which field holds sys_user reference in your table?

 

Regards,

Sachin

chrisyork
Giga Contributor

The table that contains the membership of groups is sys_user_grmember

therefore line 2 should be:

var group = new GlideRecord("sys_user_grmember");

 

 

ARG645
Tera Guru

There are multiple bugs in your script.

If your Business Rule is on change_task table , current.sys_id will return the sys_id of the record in the change_tasks. 

As previous suggested, use sys_user_grmember table in  Line 2

Your code should look something like below

if (current.u_custom_checkbox == true) {
	var user_email = current.u_custom_url_field_next_to_checkbox;
	var user_Glide = findUserfromEmail(user_email);
	
	var group = new GlideRecord("sys_user_grmember");
	group.addQuery("group", "ldka4465tgsfgsgtrrtsret"); // sys_id of user group
	group.addQuery("user", user_Glide.getValue('sys_id')); // grab the sys_id associated witht that email
	group.query();
	
	if (!group.next()) {
		group.initalize();
		group.group = "ldka4465tgsfgsgtrrtsret";
		group.user = user_Glide.getValue('sys_id');
		group.insert();
	} else {
		var removeUserFromGroup = new GlideRecord("sys_user");
		removeUserFromGroup.addQuery("group", "ldka4465tgsfgsgtrrtsret");
		removeUserFromGroup.addQuery("user", user_Glide.getValue('sys_id'));
		removeUserFromGroup.query();
		if (removeUserFromGroup.next()) {
			removeUserFromGroup.deleteRecord();
		}
	}
}
function findUserfromEmail(userEmail)
{
	var user = new GlideRecord('sys_user');
	user.get('email',userEmail);
	return user;
}

Dave Smith1
ServiceNow Employee
ServiceNow Employee

"I get no error messages." - you also get no insight into what's going on without any debug messages.

Consider adding some logging then check system logs to see what happened. Or writing out some messages using addInfoMessage().

Or even turning on debugging to see what the platform believes is happening.