Clear out assigned to field when assigned user is removed from assignment group

Tee Fortune
Kilo Explorer

Hello everyone,

I am looking to have the assigned to field cleared out automatically if the assigned to user is removed from the group. What would be the best way to go about this please? 

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

OK, let's take a step back for a moment and simplify things.

You want to remove the user from the "Assigned to" field on any active, Task-derived record when the user is removed from a group, correct?  So we need to look at all Task records where "Active" is true, the "Assignment group" is the Group the User was just removed from and the "Assigned to" field is the removed user.

To do that, we want to create a new "Advanced", "After Delete" Business Rule on the Group Member table:

find_real_file.png

So this BR will only trigger whenever a User is removed from a Group.

Here's the code you can use in the Script field:

(function executeRule(current, previous /*null when async*/) {
	var groupId = current.getValue("group");
	var groupName = current.group.getDisplayValue();
	var userId = current.getValue("user");
	var userName = current.user.getDisplayValue();

	//look for all Active Task records assigned to the Group AND User
	var gr = new GlideRecord("task");
	gr.addEncodedQuery("active=true^assignment_group=" + groupId + "^assigned_to=" + userId);
	gr.query();
	while (gr.next()) {
		//add a work note to let people know why the field was cleared
		gr.work_notes.setJournalEntry("Assigned to field cleared because '" + userName + "' was removed from the current Assignment group '" + groupName + "'");
		gr.assigned_to = "";
		gr.update();
	}
})(current, previous);

View solution in original post

26 REPLIES 26

When I inserted this script, it did not automatically clear assigned to. Assigned to was only cleared when an update was made to the ticket. This defeats the purpose of the BR as there is a potential for tickets to go unaddressed. Also, the "current.update();" line was causing a loop and other errors so I had to make the rule inactive.

This idea might work.

 

Write an after insert business rule on "sys_audit_delete" table with filter as table name is "sys_user_grmember"

 

I am taking the example of incident table where you want to clear the assigned to field or whereever it is you want to clear.

var a= new GlideRecord('sys_user_grmember');
a.query('sys_id', current.documentkey);
a.query();
if(a.next())
{
var b=new GlideRecord('incident');
b.addQuery('assignment_group',a.group);
b.addQuery('assigned_to',a.user);
b.addQuery('active',true);
b.query();

while(b.next())

{
b.assigned_to='';
b.update();
}

}

Don't forget to add a query for active to be true if you only want active incidents/tasks. Also, you would need a while loop to go through each task as opposed to an if if you want to update multiple. 

I am new to scripting and I am a bit lost here. How do I do this?

Create a new business rule:

 

Table: sys_audit_delete

Type is: After and check the "insert" box.

In the conditions section:

Table--> is--> sys_user_grmember

 

In the script, use my above script. PS: that script will work to clear out assigned to field on incident table. I am not sure what tables you're trying to clear out.