Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

List of recipients in notification via workflow

KB15
Giga Guru

I think I have most of the components to have this work but i'm missing something.

I have workflow triggering an event to fire an notification. The event contains a parameter script to get a list of email addresses from a list of users from a list collector. I can generate the array no problem.

Create Event activity:

(function() {

var arrayEmail = [];

var gr = new GlideRecord('sc_req_item');

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

gr.query();

if (gr.next()) {

var arrayUtil = new ArrayUtil();

var list = gr.variables.proj_employee_remove_list.toString();

var arrayList = list.split('.');

l = arrayList.length;

for (var i=0; i<=l; i++) {

var ue = new GlideRecord('sys_user');

ue.addQuery('sys_id', 'IN', arrayList[i]);

ue.query();

while (ue.next()) {

arrayEmail += (ue.email + ',');

}}

return "arrayEmail";

}

}());

The problem is passing the email addresses to the notification. I have the parameter 1 selected in the notification. The event fires but returns the RITM that's associated with workflow. What else am i missing or doing incorrectly?

1 ACCEPTED SOLUTION

KB15
Giga Guru

Looks like I had an error with the first part of the query. It should have been a 'sys_id', current.sys_id


Second, the parameter needs to pass the sys_id of the users so the second half of the script was wholly unnecessary.


I'm not sure if the toString at the end is necessary since it's already in a string format but it didn't hurt.



Thanks for the suggestions!



(function() {



      var arrayEmail = [];



      var gr = new GlideRecord('sc_req_item');


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


      gr.query();



      if (gr.next()) {


              var arrayUtil = new ArrayUtil();


              var list = gr.insertvariablename.toString();


              var arrayList = list.split(',');


              l = arrayList.length;



          return arrayList.toString();


      }


View solution in original post

10 REPLIES 10

SanjivMeher
Mega Patron
Mega Patron

I corrected line 24. Also line 12, I think you should split based on comma instead of '.'



  1. (function() {  
  2.  
  3. var arrayEmail = [];  
  4.  
  5. var gr = new GlideRecord('sc_req_item');  
  6. gr.addQuery('number', current.sys_id);  
  7. gr.query();  
  8.  
  9. if (gr.next()) {  
  10. var arrayUtil = new ArrayUtil();  
  11. var list = gr.variables.proj_employee_remove_list.toString();  
  12. var arrayList = list.split('.');  
  13. l = arrayList.length;  
  14.  
  15. for (var i=0; i<=l; i++) {  
  16. var ue = new GlideRecord('sys_user');  
  17. ue.addQuery('sys_id', 'IN', arrayList[i]);  
  18. ue.query();  
  19.  
  20. while (ue.next()) {  
  21. arrayEmail += (ue.email + ',');  
  22. }}  
  23.  
  24. return arrayEmail;  
  25. }  
  26. }());

Please mark this response as correct or helpful if it assisted you with your question.

Justin Abbott
Giga Guru

Wouldn't that script always return the string, "arrayEmail"?



I would've imagined the code to look something like below.



(function() {



      var arrayEmail = [];



      var gr = new GlideRecord('sc_req_item');


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


      gr.query();



      if (gr.next()) {


              var arrayUtil = new ArrayUtil();


              var list = gr.variables.proj_employee_remove_list.toString();


              var arrayList = list.split('.');


              l = arrayList.length;



              for (var i = 0; i <= l; i++) {


                      var ue = new GlideRecord('sys_user');


                      ue.addQuery('sys_id', 'IN', arrayList[i]);


                      ue.query();



                      while (ue.next()) {


                              arrayEmail.push(ue.getValue('email'));


                      }


              }



              return arrayEmail.toString();


      }


}());



Also, what does your event call look like?


I tried both options. Still only shows the request item number in the parm1 field in the event queue. I would expect the list of email addresses to be displayed instead.


I originally had returned the arrayEmail variable as is but thought quotes would work as the docs explained it.



The event logs look like this



find_real_file.png


What does your gs.eventQueue() call look like? Are you sure you're aligning the arguments properly?



For example, parm1 in the call below is 'current.marketing_event'.



gs.eventQueue('x_snc_marketing_ev.attendee.added', current, current.marketing_event, current.email);