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.

Email notification is not working using email script

mahesh105
Tera Contributor

I am trying to add group on email notification using email script. but it is not sending notification to group members.

Please refer the code and suggest me :

 

(function runMailScript(current, template, email, email_action, event) {

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery("request", current.request.sys_id);
ritm.query();
if (ritm.next()) {
var loc = current.u_item_for.location.name;   //user location
gs.log("xyz" + loc);

}
var emails = [];
var userName = [];
var gr = new GlideRecord('sys_user_grmember');
if (loc.includes('Operations') && loc.slice(-2) == 'MA') {    //if users location contain Operations and Ma notification goes to ServiceNow.Application.Approver.Metrc.MAGrowCompliance group.

gr.addQuery("group.name", "ServiceNow.Application.Approver.Metrc.MAGrowCompliance"); 
gr.query();
while (gr.next()) {
emails.push(gr.user.email.toString());
userName.push(gr.user.toString());
email.addAddress("cc", gr.user.email.toString(), gr.user.getDisplayValue());
gs.info("Tanuj" + emails.toString());
gs.info("Mahesh" + userName.toString());
}

6 REPLIES 6

Shubham_Shinde
Giga Guru

Hello @mahesh105 ,

Below thread might be helpful for you:
mail_script why does it not populate the cc field?

Please mark this comment as correct/helpful if it helps you.
Regards,
Shubham

art_jones
Kilo Sage

Hi, a couple things to look at that may help:

  • confirm that your dot-walking is actually returning a value
    • add a log statement after the email.addAdress line:  gs.info("CC: " + gr.user.email.toString() + ", " + gr.user.getDisplayValue()); and see if the values are what you expect
  • make sure that your mail_script tag is in the body of the email

-Art

Hi @art_jones : It is printing email address in logs bit not sending any notification

Upender Kumar
Mega Sage

Try Below

 

 

(function runMailScript(current, template, email, email_action, event) {
var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery("request", current.request.sys_id);
	ritm.query();
	if (ritm.next()) {
		var loc = current.u_item_for.location.name;   //user location
		gs.log("xyz" + loc);
		var emails = [];
		var userName = [];
		var gr = new GlideRecord('sys_user_grmember');
		if (loc.includes('Operations') && loc.slice(-2) == 'MA') {    //if users location contain Operations and Ma notification goes to ServiceNow.Application.Approver.Metrc.MAGrowCompliance group.
			gr.addQuery("group.name", "ServiceNow.Application.Approver.Metrc.MAGrowCompliance"); 
			gr.query();
			while (gr.next()) {
				emails.push(gr.user.email.toString());
				userName.push(gr.user.toString());
				email.addAddress("cc", gr.user.email.toString(), gr.user.getDisplayValue());
				gs.info("Tanuj" + emails.toString());
				gs.info("Mahesh" + userName.toString());
			}
		}
	}
})(current, template, email, email_action, event);