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,

The user table query is taking a lot of time resulting in timeout, so i changing code to related list table query but this one is working , what am i missing ?

var userList = [];
var userRec = new GlideRecord("sn_si_m2m_task_affected_user");
userRec.addQuery('task',incidentNumber);
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(), "");

Hi @Servicenow lear 

From where are you passing that Incident number in add query ?

userRec.addQuery('task',incidentNumber);

Is it variable which is storing incident sys_id or like that ? Because If you didn't declare that 'incidentNumber' then it will be undefined error. 

Please check that and confirm.


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

Incident number is the SIR number for secureity incident : I meant in previous comment not working

 

Servicenowlear_0-1670267783414.png

 

In which table you are writing this code ?

If you wanted to pass the dynamic value then either use 

userRec.addQuery('task',current.incidentNumber); //check proper back-end name of incident number from dictionary of field. I'm doubtful on the field name. 

Use current If you are using a Business rule.


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

I want to pull email address of the users from m2m table sn_si_m2m_task_affected_user.

the notification is event triggered.