Code for adding multiple user emails in the event queue

Servicenow lear
Tera Contributor

Hello All,

 

We have an Event which triggers a notification on Security Incident response.

I am looking for code of adding Users email address to event so that the notification can be sent to email address of users in Affected user related list.

Table : sn_si_m2m_task_affected_user

Event : gs.eventQueue('sn_si.secops.trigger.spam.email', current, '', ''); 

1 ACCEPTED SOLUTION

Okay then writedown code as below. But make sure you have push the correct email address by writing proper field name for email in below script.

 

 

var userList = [];
var userRec = new GlideRecord("sn_si_m2m_task_affected_user");
userRec.addQuery('task',current.sys_id);
userRec.query();
while (userRec.next()) {
if (userList.toString().indexOf(userRec.sys_id) == -1) {
userList.push(userRec.sys_id + "");
}
}


gs.eventQueue('sn_si.trigger.spam.email', current, userList.toString(),"");


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

13 REPLIES 13

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Servicenow lear ,

 

You can pass multiple values separated by a comma.   The email notifications just need a comma separated list of user sys_id's and you don't need to pass the name unless you want them to show up in the email to line.   If you don't pass it, then just the email address will be present.   Here is some example code to get a list of users and create an array that you can then pass to an event:

 

var userList = [];
var userRec = new GlideRecord("sys_user");
userRec.addEncodedQuery(YOUR-ENCODED-QUERY);
userRec.query();
while (userRec.next()) {
      if (userList.toString().indexOf(userRec.sys_id) == -1) {
              userList.push(userRec.sys_id + "");
      }
}
gs.eventQueue("EVENT-NAME", current, userList.toString());


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

hello Gunjan,

I did this but still no notification: 

var userList = [];
var userRec = new GlideRecord("sys_user");
userRec.addEncodedQuery(email!=empty);
userRec.query();
while (userRec.next()) {
if (userList.toString().indexOf(userRec.sys_id) == -1) {
userList.push(userRec.sys_id + "");
}
}


gs.eventQueue('sn_si.trigger.spam.email', current, userList.toString(), "");

 

Servicenowlear_0-1670258195399.png

 

Enclosed encoded query in quotes.

 

var userList = [];

var userRec = new GlideRecord("sys_user");

userRec.addEncodedQuery("email!=empty");

userRec.query();

while (userRec.next()) {

if (userList.toString().indexOf(userRec.sys_id) == -1) {

userList.push(userRec.sys_id + "");

}

}

 

 

gs.eventQueue('sn_si.trigger.spam.email', current, userList.toString());


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Thank you Gunjan,  You have been really helpful.

Somehow the email query is becoming is so huge that it times out the transaction, not able to test it .  

Let me fix the encoded query and get back to you