best way to set recipients on a notification?

patricklatella
Mega Sage

Hi gang,

I want to put together a notification that will send to users who are entered into a reference field on child tasks of a parent record.

Here's the scenario:

I have a custom table [x_cur_oc_feedback_oc_feedback] that has a [comments] field; this table is extended from task.   Records in this table can have related child tasks.   The child tasks have a custom field [business_contact] for assigning the task.

If the [comments] section on the parent record changes, I want a notification to be sent to the users entered in the [business_contact] field on the child tasks associated with the parent record (if there are any).

What's the best way to do this?

Should I write an email script to do a GlideRecord query to get the names in the [business_contact] field on the child tasks?   How do I then set that list of names as the recipient of the email?

Or should I create an event that triggers a script action that queries and passes the names for the recipients of the email?

not sure which way to go...thanks!

1 ACCEPTED SOLUTION

Based upon the recent suggestions,here is the script for the Business Rule:


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


// Query for existing child tasks and gather list of any actionees


      var gr = new GlideRecord('x_cur_oc_feedback_tasks');


      gr.addQuery('parent',current.sys_id);


      gr.query();


      var bcArray = [];//array of business contacts from child tasks


      while(gr.next()){


              if (gr.business_contact !=''){


                      if (bcArray.toString().indexOf(gr.business_contact) == -1){


                      bcArray.push(gr.business_contact.toString());


                      }


              }


      }


      gs.eventQueue('x_cur_oc_feedback.comments.added.notify',current,bcArray,'','');


})(current, previous);


View solution in original post

39 REPLIES 39

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Patrick,



In this case, you can create an event and then trigger it via a Business rule.


Events


patricklatella
Mega Sage

hi Pradeep, thanks for the reply.



Ok so the overall plan would be:



Business rule triggers the event > the event triggers a notification?



where do I put the script the looks up the recipients and sets them as the "who will recieve"?


Right. So you will GlideRecord and then pass the gr object and business contact user in the method gs.eventQueue('Event Name', gr, gr.business_contact.getDisplayValue());


https://developer.servicenow.com/app.do#!/lp/new_to_servicenow/app_store_learnv2_automatingapps_ista...


and in your example script:



gs.eventQueue('Event Name', gr, gr.business_contact.getDisplayValue());



is this only passing the "parm1"?   and that "parm1" will contain all the recipients (as gathered by the GlideRecord)



correct?