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

Hi Ankur,

The days will vary , that is why we are passing them in event.param2.

Can you help me to provide the correct replacement code for using days in subject and content?

As of now, i was directly using ${event.param2} but how to use it for using xdays and cc managers as well. Please suggest.

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

Hi Ankur,

Thanks for the solution. I have modified my requirements and I don't need to split the event parameter as we need to pass only manager recipients and xdays as separate parameters. But after updating my code , my recipients are showing up on logs as below :

 

The highlighted part is the cc manager recipient ("email":"Test.User1@abc.com.in"}) , but its not coming up in correct format through mail script or scheduled job like user recipients.

 

Below is my Scheduled Job and Mail Script code. Please suggest how to correct the cc email :

(function runMailScript(current, template, email, email_action, event) {
    var parm2Values = event.parm1.toString(); // get the json string 
    var ccUsersObj = JSON.parse(parm2Values); // parse it
    for (var i = 0; i < ccUsersObj.length; i++) {
        email.addAddress("cc", ccUsersObj.email, ccUsersObj.name);
    }

})(current, template, email, email_action, 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 = '10';
// ----------------------------------------------
// 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) {
        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 scheduleNotif = new GlideRecord('sysevent_email_action');
scheduleNotif.get('dfc47d0a1bdb5050782ca68b274bcb25'); //Sys ID of your Notification Configured
scheduleNotif.recipient_users = usr_recipients.join(',');
scheduleNotif.update();

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

Hi Navneet,

I believe it is not proper to ask a secondary question on the same community post as it leads to confusion to future readers. Also this is answered.

It's better to create a second post so there's a separation in the questions being asked.

Regards
Ankur

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