Email sent when request item is commented

Sam Ogden
Tera Guru

Hi All,

I've written the below script to send an email notification when the request item is commented.   This needs to be sent to the assigned_to of each of the tasks that are linked to the request item.   If 1 or all the tasks are not assisnged the it should send it to the assignment_group.manager.

I've just been testing and if I have a request that has 2 tasks and both tasks the assigned_to is empty then the manager of each of the assignment_group get the email as expected.

If the first task assigned_to is empty and the 2nd taks is assigned to someone then task 1 sends to the manager and task 2 sends to the individual as expected.

If the first task is assigned to an individual and the 2nd is empty it is only sending the email to the assigned_to of the first task and nothing is sent to the manager of the second task - we need both to get the email.

If both tasks are assigned to someone the first task sends the email but the second does not - We need both to get the email

Any help on where I've gone wrong with the code will be greatly appreciated.

Business Rule:

runs after change of additional comment field.

(function executeRule(current, previous /*null when async*/) {

var email_to = [];
var sc_task = new GlideRecord('sc_task');
sc_task.addQuery('request_item',current.sys_id+'');
sc_task.addActiveQuery();                                                                                                                                  
sc_task.query();
while (sc_task.next()){
  if (sc_task.assigned_to == "" || sc_task.assigned_to == null){                
    email_to.push(sc_task.assignment_group.manager);
  }
  else{                                                                                                                                                                                                
    email_to.push(sc_task.assigned_to);
  }
}
gs.eventQueue("sc_req_item.commented.itil", current, email_to.join(), gs.getUserName());
})(current, previous);

Email Notification:

find_real_file.png

find_real_file.png

find_real_file.png

1 REPLY 1

ccajohnson
Kilo Sage

A few things to make sure your script is running smoothly:


1. Make sure you use .toString() when pushing to an array.


2. Unclear why you have a .join() in your event queue (I do not think it is necessary).


3. Check to make sure that your assignment groups have a manager. If they don't, no user will be notified.


4. Validate that the Assigned to and the Manager are not the same person. I believe ServiceNow will send to unique addresses.