I am passing some email ids through event param2, and I want to add some more email ids in CC.. is it possible?

Sathwik1
Tera Expert

I am passing some email ids through event param2, and I want to add some more email ids in CC.. is it possible?

1 ACCEPTED SOLUTION

Ok Got it,

Since we cannot set TO address from script, pass these values as param1 (modify notification to include param1 as recipents)

For parameter2, try this

create a json and pass incidents,cc address values.

var assigneeArr = [];
var assigneeArrr=[];
var incidentArr = [];
var getEmail = [];
var gr = new GlideRecord("x_408194_loading_e_scoped_data_table");
gr.query();
while (gr.next()) {
    incidentArr.push(gr.sys_id.toString());
    assigneeArr.push(gr.id.toString());
    assigneeArrr.push(gr.id2.toString());
}
var uniqueAssignees = new global.ArrayUtil().unique(assigneeArr);
var uniqueAssigneees = new global.ArrayUtil().unique(assigneeArrr);

gs.info("Unique TO Users " +uniqueAssignees);
gs.info("Unique CC Users " +uniqueAssigneees);

for (var i = 0; i < uniqueAssignees.length; i++) {
    var incidents = [];
    for (var j = 0; j < assigneeArr.length; j++) {
        if (uniqueAssignees[i] == assigneeArr[j]) {
            var gr2 = new GlideRecord('sys_user');
            gr2.addQuery('user_name', assigneeArr[j]);
            gr2.query();
            if (gr2.next()) {
                getEmail.push(gr2.email);
            }
            incidents.push(incidentArr[j]);
        }
    }
var cc=[];
	for ( i = 0; i < uniqueAssigneees.length; i++) {
    for ( j = 0; j < assigneeArrr.length; j++) {
        if (uniqueAssigneees[i] == assigneeArrr[j]) {
            var gr3 = new GlideRecord('sys_user');
            gr3.addQuery('user_name', assigneeArrr[j]);
            gr3.query();
            if (gr3.next()) {
                cc.push(gr3.email);
            }
        }
    }	
var obj={};
obj['incidents'] = incidents.toString();
obj['cc'] = cc.toString();
	gs.info("Get All Email Id's " +getEmail[i]);
    gs.eventQueue('x_408194_loading_e.incident.over30days', gr, getEmail, obj);
}
}

In email-script access param2 

var cc = event.param2.cc;

var inc = event.param2.incidents

 

param1 will have TO address, param2 will have both cc and incidents info.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

22 REPLIES 22

is there any possibility to store TO and CC users in event parm2 and need to separte after moving to notification email script? - No

We cannot send TO emails from email-script.

 

The above script I suggested is just an approcah on how you can consolidate the parameters and use JSON.

You should keep logs and verify param1 values to send all the required emails.

 

for below part of the code , are you mapping json values properlly from your script?

var getCCIds = event.parm2.cc;

gs.info("Get CC Id Details "+getCCIds);


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Hi @Rohila Voona 

Thanks for your help..

The below schedule job is perfectly fine... but in email script I am facing issues to parse the incident sysids and cc mail ids..

Please help me to correct this.....

Schedule job

var assigneeArr = [];
var assigneeArrr = [];
var incidentArr = [];
var getEmail = [];
ar= [];
var gr = new GlideRecord("x_408194_loading_e_scoped_data_table");
gr.query();
while (gr.next()) {
    incidentArr.push(gr.sys_id.toString());
    assigneeArr.push(gr.id.toString());
    assigneeArrr.push(gr.id2.toString());
}
var uniqueAssignees = new global.ArrayUtil().unique(assigneeArr);
var uniqueAssigneees = new global.ArrayUtil().unique(assigneeArrr);

gs.info("New Unique TO Ids " + uniqueAssignees);
gs.info("New Unique CC Ids " + uniqueAssigneees);


for (var i = 0; i < uniqueAssignees.length; i++) {
    var gr2 = new GlideRecord('sys_user');
    gr2.addQuery('user_name', uniqueAssignees[i]);
    gr2.query();
    if (gr2.next()) {
        getEmail.push(gr2.email);
    }
	gs.info("Print all emails for TO Users " +getEmail);
    var incidents = [];
    for (var j = 0; j < assigneeArr.length; j++) {
        if (uniqueAssignees[i] == assigneeArr[j]) {
            incidents.push(incidentArr[j]);
        }
    }
	
    var cc = [];
    for (k = 0; k < uniqueAssigneees.length; k++) {
        var gr3 = new GlideRecord('sys_user');
        gr3.addQuery('user_name', uniqueAssigneees[k]);
        gr3.query();
        if (gr3.next()) {
            cc.push(gr3.email);
        }
    }
	
    var obj = {};
    obj['incidents'] = incidents.toString();
    obj['cc'] = cc.toString();
	ar.push(obj);
    gs.info("New Email Id's " + getEmail);
    gs.eventQueue('x_408194_loading_e.incident.over30days', gr, getEmail[i], JSON.stringify(ar));
}

 

Email Script

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template, /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action, /* Optional GlideRecord */ event) {


    var parm2Values = event.parm2.toString();
    var ccUsersObj = JSON.parse(parm2Values);
    for (var i = 0; i < ccUsersObj.length; i++) {
   
    var gr = new GlideRecord("x_408194_loading_e_scoped_data_table");
    gr.addQuery("sys_id", ccUsersObj[i].incidents);
    gr.query();
    while (gr.next()) {
    var gr1 = new GlideRecord('sys_user');
    gr1.addQuery('email', ccUsersObj[i].cc);
    gr1.query();
    if(gr1.next())
    {
    email.addAddress("cc", ccUsersObj[i].incidents, ccUsersObj[i].cc);
        template.print(gr.name);
        template.print('\n');
        template.print('\n');
}}
}
})(current, template, email, email_action, event);

Dont convert param2 to string.

use below line

var parm2Values = event.parm2;

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Try this:

I modified the logic, validate it again.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template, /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action, /* Optional GlideRecord */ event) {


    var parm2Values_cc = event.parm2.cc;
    var ccUsersObj = parm2Values.toString().split(",");

    var param2Values_inc=event.parm2.incidents;
    var incObj = param2Values_inc.toString().split(",");

    for (var i = 0; i < ccUsersObj.length; i++) {
   
    var gr = new GlideRecord("x_408194_loading_e_scoped_data_table");
    gr.addQuery("sys_id", incObj[i]);
    gr.query();
    while (gr.next()) {
    var gr1 = new GlideRecord('sys_user');
    gr1.addQuery('email', ccUsersObj[i]);
    gr1.query();
    if(gr1.next())
    {
    email.addAddress("cc", incObj[i], ccUsersObj[i]);
        template.print(gr.name);
        template.print('\n');
        template.print('\n');
}}
}
})(current, template, email, email_action, event);

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Tried, in event logs for param2 I am getting like this [object Object]..

is it required to do JSON.parse something like ...  if u check my latest code which I shared above..

can you suggest how to use JSON.parse in email scripts..

if I used this I am get some output.. that needs to parse properly...then I may achieve the requirement

 

   var obj = {};
    obj['incidents'] = incidents.toString();
    obj['cc'] = cc.toString();
	ar.push(obj);
    gs.info("New Email Id's " + getEmail);
    gs.eventQueue('x_408194_loading_e.incident.over30days', gr, getEmail[i], JSON.stringify(ar));
}