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.

How to send multiple emails from business rule

servicetrout
Tera Expert

I want to understand how to send emails to a list of people, since the OOTB functionality in notifications doesn't fit my use case.

I have a business rule that triggers an event based on a condition for a single department  entry which sends a single email notification to the department submitter.

gs.eventQueue("my.UserSubmission", current);

 

What I want is the notification to go to half a dozen people in the department.  Please advise if this is correct. 

1) Add a function to my business rule advanced script section that generates a comma separated list of either sys_ids for the users-to-be-notified from the user table or comma separated email addresses for the users-to-be-notified.

fx() =  parm1="user1@gmail.com,user2@gmail.com,user3@gmail.com";

or 

fx() = parm1="6f20d12b6f82850023bd5965bd3ee001, 6f20d12b6f82850023bd5965bd3ee002,6f20d12b6f82850023bd5965bd3ee003";

 

2) Pass that to the event via a call, such as, 

 gs.eventQueue('my.UserSubmission', current, parm1);

3) In the notification make sure the "Event parm 1 contains recipient' box is checked. 

 

Is that right?  Or is there anything I've overlooked? 

Thank you in advance. 

G

 

PS, bonus questions:  

All of which begs the question is there an easy way to get the sys_id's for all users assigned a specific role?

 

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

This all looks correct.  As far as getting all users with a specific role, you will need to query for them like this...

var usrArr = [];
var users = new GlideRecord('sys_user_has_role');
users.addQuery('role.name', 'YOUR_ROLE_NAME');
users.query();
while (users.next()) {
    usrArr.push(users.getValue(user));
}

// Convert usrArr to string
var usrString = usrArr.join();

View solution in original post

7 REPLIES 7

Mark Stanger
Giga Sage

This all looks correct.  As far as getting all users with a specific role, you will need to query for them like this...

var usrArr = [];
var users = new GlideRecord('sys_user_has_role');
users.addQuery('role.name', 'YOUR_ROLE_NAME');
users.query();
while (users.next()) {
    usrArr.push(users.getValue(user));
}

// Convert usrArr to string
var usrString = usrArr.join();

I am trying to do something similar.  I am not getting any of my logs for the roles.  Can someone see if it is obvious where I am going wrong.  Here is my code from my business rule:

 

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

var admins = [];
var userGR = new GlideRecord('sys_user');
userGR.addEncodedQuery('companyLIKENIBCO, Inc.');
userGR.query();
while (userGR.next()) {
admins.push(userGR.sys_id);
var list = admins.toString();
var arrayList = list.split(",");

for (i = 0; i < arrayList.length; i++) {
var usrArr = [];
var uhrRec = new GlideRecord('sys_user_has_role');
uhrRec.addQuery('user', arrayList[i]);
uhrRec.addEncodedQuery('role=9b94fc24d7700200e5982cf65e610358');
uhrRec.query();
while (uhrRec.next()) {
gs.info("Stacys uhrRec is " + uhrRec.sys_id);
usrArr.push(uhrRec.getValue(user));

var parm1 = usrArr.join();
gs.info("Stacys parm1 is " + parm1);
gs.eventQueue("sn_customerservice.NibcoAdmin", current, parm1);
}
}

}

})(current, previous);

 

 

Thanks,

Stacy

servicetrout
Tera Expert

Thank you, Mark - appreciate the quick response. 

No problem!  Please mark my answer above as correct if I've answered your question.