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

servicetrout
Tera Expert

Final function that worked for what I needed:

 

function getAllUsersInRole (roleName) {
  var usrArr = [];
  var users = new GlideRecord('sys_user_has_role');
  users.addQuery('role.name', roleName);
  users.query();
  while (users.next()) {
      usrArr.push(users.user.email);
  }
   // Convert usrArr to string
   var usrString = usrArr.join();
   return usrString;
}

Community Alums
Not applicable

I need to send multiple notifications whenever an asset is going to expire before 30,60,90 days. Here, I have generated seperate notification and business rules for each.

But here in gs.eventQueue(''), I have created only one event registry and can I use that single event registry in all notifications and business rules?

 

Community Alums
Not applicable

Please help me out in this, 

Thanks in advance