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

Use this logic

script:modify below line to pass both name and email.

getEmail.push(gr2.name +","+gr2.email);

emsil script: split the paramater and use those values.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var gr = new GlideRecord("x_408194_loading_e_scoped_data_table");
    gr.addEncodedQuery("sys_idIN" + event.parm1.toString());
    gr.query();
    while (gr.next()) {
        var getCCId = gr.id2;
        var getEmail = event.param2.split(",");
        email.addAddress("cc", getEmail[0], getEmail[1]);
        template.print(getEmail.name);
        template.print(gr.number);
        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

@Rohila Voona 

Thanks for your suggestion..

You didn't get my query... from schedule job I am passing few values.. those needs to send as "TO" user... 

and I am using some query in email script, based on that script I will get few more mail id's those I need to pass in the "CC" 

 

So please suggest me on how to do it.

@Rohila Voona 

The below one is the exact code....

I want to pass "getEmail.push(gr2.email);" these mails in TO 

and I want to pass "getEmail.push(gr3.email);" these mails in CC 

 

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]);
        }
    }
	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()) {
                getEmail.push(gr3.email);
            }
        }
    }	
	gs.info("Get All Email Id's " +getEmail[i]);
    gs.eventQueue('x_408194_loading_e.incident.over30days', gr, incidents.toString(), getEmail[i]);
}
}

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

HI @Rohila Voona 

In parm1 I am passing few record sys_id's, it will be missed if I remove that..

Can you please check the below link once.. It was the perfect code and also written the issue clearly.. please have a look on it once..

community.servicenow.com/community?id=community_question&sys_id=10d5bcba1b768514ed6c9979b04bcb8d