How to pass dynamic recipients in CC for email notification through a Scheduled Job ?

Navneet Arora
Mega Expert

I have created a Scheduled Job (as shown below) to pass dynamic recipients in email notification and trigger the notification through event in the script itself . But the issue is I have to separate users in TO and CC fields , i am unable to do so. In the below code , I have to pass users in TO field and manager in CC field but as of now they are going in To field through event:

// This scheduled job will send the group notification for all the Assignees of the closed issues in last x days
// CONFIGURATION SECTION
// -----------  Configure the DAYS --------------
var xdays = '5';
// ----------------------------------------------
// Configure to 'Yes'/'No' to send/exclude notifying managers. 
var notifyManager = 'Yes';
// ----------------------------------------------

//Get all the Closed Issues in last x days
var usr_recipients = [];
var mgr_recipients = [];
var gr = new GlideRecord('sn_grc_issue');
var gQuery = 'active=false^parent_issueISEMPTY^closed_atRELATIVEGT@dayofweek@ago@';
gQuery = gQuery + xdays;

gr.addEncodedQuery(gQuery);
gr.query();

// Get the list of usr_recipients
while (gr.next()) {
    if (usr_recipients.indexOf(gr.assigned_to.sys_id.toString()) == -1)
        usr_recipients.push(gr.assigned_to.sys_id.toString());

    if (mgr_recipients.indexOf(gr.item.owner.toString()) == -1)
        mgr_recipients.push(gr.item.owner.toString());
}

var recipient_users = usr_recipients.join(',');

if (notifyManager == 'Yes')
  recipient_users = recipient_users + ',' + mgr_recipients.join(',');

//var recipient_mgrs = mgr_recipients.join(',');

// Trigger the event to send the Notification
gs.eventQueue("sn_compliance.issue.closed.lastxdays", gr, recipient_users, xdays) ;

In the above code , my purpose is to pass usr_recipients in TO field and mgr_recipents in CC field through event and trigger the notification. Please suggest how to do same.

1 ACCEPTED SOLUTION

Hi Navneet,

for setting the subject you will have to use email script then

In that case keep the code I gave in my 1st reply

Create new email script and call it in notification and also handle the managers as cc via parsing the json

Email Script: update to the one I already mentioned above

Name: show_message

var days = event.parm2.toString().split('@')[0]; // get the days and use if you require to print that

email.setSubject('Issues - Closed in last ' + days + ' days');

// print here your entire email body using template.print

var parm2Values = event.parm2.toString().split('@')[1]; // get the json string after split

var ccUsersObj = JSON.parse(parm2Values); // parse it

var days = event.parm2.toString().split('@')[0]; // get the days and use if you require to print that

for(var i=0;i<ccUsersObj.length;i++){
    email.addAddress("cc", ccUsersObj.email, ccUsersObj.name);
}


Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you will have to use email script to set the CC users.

it cannot be set directly using eventQueue()

So if you know which are the cc users send that in event parm2 and then in email script access that parameter and send the cc users the email

Example scripting for email notifications

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

updated script as per my previous comment

Job Script:

var xdays = '5';
var notifyManager = 'Yes';
var usr_recipients = [];
var mgr_recipients = [];
var gr = new GlideRecord('sn_grc_issue');
var gQuery = 'active=false^parent_issueISEMPTY^closed_atRELATIVEGT@dayofweek@ago@';
gQuery = gQuery + xdays;

gr.addEncodedQuery(gQuery);
gr.query();

// Get the list of usr_recipients
while (gr.next()) {
    if (usr_recipients.indexOf(gr.assigned_to.sys_id.toString()) == -1)
        usr_recipients.push(gr.assigned_to.sys_id.toString());

    if (mgr_recipients.indexOf(gr.item.owner.toString()) == -1){
        var obj = {};
obj["name"] = gr.item.owner.getDisplayValue(); // push the name
obj["email"] = gr.item.owner.email.toString(); // push the email

mgr_recipients.push(obj);
    }
}

var recipient_users = usr_recipients.join(',');

if (notifyManager == 'Yes')
  recipient_users = recipient_users + ',' + mgr_recipients.join(',');

//var recipient_mgrs = mgr_recipients.join(',');

// Trigger the event to send the Notification
gs.eventQueue("sn_compliance.issue.closed.lastxdays", gr, recipient_users, xdays + '@' + JSON.stringify(mgr_recipients));

Email Script - if you don't have one then please create or use existing one

var parm2Values = event.parm2.toString().split('@')[1]; // get the json string after split

var ccUsersObj = JSON.parse(parm2Values); // parse it

var days = event.parm2.toString().split('@')[0]; // get the days and use if you require to print that

for(var i=0;i<ccUsersObj.length;i++){
    email.addAddress("cc", ccUsersObj.email, ccUsersObj.name);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

Thanks for prompt response.

But I have a concern if I use the below code:

gs.eventQueue("sn_compliance.issue.closed.lastxdays", gr, recipient_users, xdays + '@' + JSON.stringify(mgr_recipients));

then i wont be able to use event parameter 2 for xdays which I am using in my notification already.

I have to pass param2 i.e. xdays in my Subject and Email content as well. How can i pass that?

 

find_real_file.png

 

So i would need a way to seperately pass users , managers , xdays through event queue. But in event queue we can only pass 2 parameters. So i need to find a way to use xdays and managers seperately .

Hi,

if the days will always be constant then directly hard-code that in subject and email body and just send the cc users in event.parm2

If not then set the subject from email script and also the entire email body from email script

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader