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

Here is an alternative of NOT using ArrayUtil.   Should produce exact same results:


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


      var bcArray = []; //Array of found Business Contacts


      var childTask = new GlideRecord('x_cur_oc_feedback_tasks');


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


      childTask.query();


      while(childTask.next()) {


              if (childTask.business_contact != '') { //When there is a business contact defined:


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


                              bcArray.push(childTask.business_contact.toString()); //Add to Business Contacts Array


                      }


              }


      }


      gs.eventQueue('oc_feedback.commented', current, bcArray); //Send notification


})(current, previous);


ok, I put in a gs.info() to see what the script is producing, and with that script it's writing to the log each time it finds a related task, each time adding the next business contact.   If I remove this line, it only writes once with all the sys_ids of business contacts on task is this line necessary?records   if (bcArray.toString().indexOf(childTask.business_contact) == -1)



is this line necessary?


I was following Christopher's lead making sure there aren't duplicate business contacts.   It checks to make sure the next task's business contact isn't already in the array.


You will need to make sure that your eventQueue is defined with the scope as a part of the event:


      gs.eventQueue('x_cur_oc_feedback.comments.added', current, rArray); //Send notification



Here is the entire script including what Michael replaced:


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


      var bcArray = []; //Array of found Business Contacts


      var childTask = new GlideRecord('x_cur_oc_feedback_tasks');


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


      childTask.query();


      while(childTask.next()) {


              if (childTask.business_contact != '') { //When there is a business contact defined:


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


                              bcArray.push(childTask.business_contact.toString()); //Add to Business Contacts Array


                      }


              }


      }


      gs.eventQueue('x_cur_oc_feedback.comments.added', current, bcArray); //Send notification


})(current, previous);


Sorry to confuse,



You need to call the Script Include from the scope it resides in when your code (in this case, your Business Rule) is in a different scope. Since the ArrayUtil script include is in the global scope, you need to preface it with global.



I have used this method in other scoped applications, so I know it works.