gs.eventqueue() only triggering once inside loop

Singh3
Tera Contributor

Hi,
I have a requirement where if a user gets deactivated, all assigned tasks to him should be unassigned as well as the user who is getting deactivated, all those tasks in which he gets unassigned from, the assignment group of those tasks should get an email but only once per assignment group.
I have created a business rule which you can see below:


var group_user = [];
var user_task = new GlideRecord("task");
user_task.addActiveQuery();
user_task.addQuery("assigned_to", current.sys_id);
user_task.orderBy('assignment_group');
user_task.query();
while (user_task.next()) {
if(group_user.indexOf(user_task.assignment_group.name.toString()) == -1)
{
group_user.push(user_task.assignment_group.name.toString());
gs.eventQueue('trigger.EmailforInactiveuserGroup',user_task,user_task.assignment_group);
}
user_task.assigned_to = "";
user_task.update();
}

 

It is only triggering 1 email even if there are multiple assignment groups.

Also please could anyone help me regarding the email. I need list of the tasks that were unassigned from the user for the group.
I made the email on sys_user table and it is not populating the ${name} of the deactivated user and it is just showing as blank and I am not aware of how the list will be made.

Thanks in advance!!

4 REPLIES 4

Sagar Pagar
Tera Patron

Hi @Singh3,

Try by adding/printing getRowCount(). It might be due to IF condition.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Could you help me with the email part?

Upender Kumar
Mega Sage

Share notification also. Because servicenow will not send the duplicate notifications (same content, subject).

t_sadahisa
Giga Guru

Hello, @Singh3 

 

I think you should separate updating target task and sending email to group.

I write sample code. (It is not testing...sorry)

var gr_task = new GlideRecord("task");

var releaseTasks = {};


// search target task and update
gr_task.addActiveQuery();
gr_task.addQuery("assigned_to", current.sys_id);
gr_task.orderBy('assignment_group');
gr_task.query();

while (gr_task.next()) {
    releaseTasks[gr_task.assignment_group].push(gr_task.sys_id);

    gr_task.assigned_to = "";
    gr_task.update();
}

// send email
for (task in releaseTasks){
    // input target tasks by using event parameter 2
    // In notification email script, you can get the information from this infomation.
    gs.eventQueue('trigger.EmailforInactiveuserGroup', task, releaseTasks[task].join());
}