determine email notification from email script

dbaril
Tera Expert

I have several email notifications that call the same email script.   Is there a way to determine from the script which email notification called it?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Don,



Yes, the email_action object will tell you what notification uses it.



See section 5 Scripting for Email Notifications - ServiceNow Wiki


View solution in original post

10 REPLIES 10

Between the two of us, we should be keeping score on what new things we learn each day.


Steven Young
Tera Guru

  I have created a "For the administrator" script that is tied to all of my email notifications.



for the admin.jpg



this is included on all email notification, and gives a direct link back to the email notification that triggered the email.



As we have 500+ email notifications, some are titled very similar, this is an easy way to click straight into the email notification.


As i have been asked to investigate why emails aren't sending, or why this or why that,   this has saved TONS of time from investigation of what triggered an email.


I'll be happy give you more detail if you would like.


Hi.Can you share steps how you managed this.If possibly, please share scripts.


Yes, absolutely.



Just FYI.   This script may not be the best "Script" out there, but it does what we want it to do.   It notify's us of who the email was sent to, and which notification triggered the email.



You will need to check the script and edit it according to what your field names are.



Create you an email script.


we titled ours   For_The_Administrator


so the email notification script would be   ${mail_script:For_The_Administrator}


you need to put   ${mail_script:For_The_Administrator}   in each email notification that you want it in.



Script is:


//gs.log("For the Administrator Email script Testing");


template.print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ <br />");


template.print("For the ServiceNow Administrator: <br /> <br />");


var emailID = email_action;


var servletUri = gs.getProperty('glide.servlet.uri');


var emaillink = servletUri + 'nav_to.do?uri=sysevent_email_action.do%3Fsys_id=' + emailID.sys_id;


template.print("Email Notification:   <a href=" + emaillink + ">" + email_action.name + "</a>" + "<br />");






// template.print("Email Notification: " + email_action.name + "<br />");


if(!email_action.template.nil()) {template.print("Email Template: " + email_action.template.getDisplayValue() + "<br />");}



template.print("Number: ${sysapproval.number}${u_number}${number}${change_request.number} <br />");






//   Who the Email will be sent to:


template.print("Notification Email to: ");


if(email_action.recipient_groups !== "") {template.print(" " + email_action.recipient_groups.getDisplayValue() + " ");}


  if(email_action.recipient_users !== "") {template.print(email_action.recipient_users.getDisplayValue() + " ");}


  if(email_action.recipient_fields.indexOf('approver') > -1){template.print(" ${approver}");}


  if(email_action.recipient_fields.indexOf('assigned_to') > -1 && email_action.recipient_fields.indexOf('u_assigned_to') == -1){template.print(" ${assigned_to},");}


  if(email_action.recipient_fields.indexOf('assigned_to.manager') > -1 && email_action.recipient_fields.indexOf('assigned_to') == -1){template.print(" ${assigned_to.manager},");}


  if(email_action.recipient_fields.indexOf('assignment_group') > -1){template.print(" ${assignment_group},");}


  if(email_action.recipient_fields.indexOf('assignment_group.email') > -1){template.print(" ${assignment_group.email},");}


  if(email_action.recipient_fields.indexOf('assignment_group.manager') > -1 && email_action.recipient_fields.indexOf('assignment_group') == -1){template.print(" ${assignment_group.manager},");}


  if(email_action.recipient_fields.indexOf('author.manager') > -1){template.print(" ${author.manager},");}


  if(email_action.recipient_fields.indexOf('caller_id') > -1){template.print(" ${caller_id},");}


  if(email_action.recipient_fields.indexOf('document_id.owner') > -1){template.print(" ${document_id.owner},");}


  if(email_action.recipient_fields.indexOf('from_user') > -1){template.print(" ${from_user},");}


  if(email_action.recipient_fields.indexOf('manager') > -1){template.print(" ${manager},");}


  if(email_action.recipient_fields.indexOf('metric_type.overdue_notify_user') > -1){template.print(" ${metric_type.overdue_notify_user},");}


  if(email_action.recipient_fields.indexOf('notify_on_expiration') > -1){template.print(" ${notify_on_expiration},");}


  if(email_action.recipient_fields.indexOf('opened_by') > -1){template.print(" ${opened_by},");}


  if(email_action.recipient_fields.indexOf('owned_by') > -1){template.print(" ${owned_by},");}


  if(email_action.recipient_fields.indexOf('parent.assigned_to}') > -1 && email_action.recipient_fields.indexOf('assigned_to') == -1){template.print(" ${parent.assigned_to},");}


  if(email_action.recipient_fields.indexOf('parent.assignment_group') > -1){template.print(" ${parent.assignment_group},");}


  if(email_action.recipient_fields.indexOf('problem.assigned_to') > -1){template.print(" ${problem.assigned_to},");}


  if(email_action.recipient_fields.indexOf('problem_id.assigned_to') > -1){template.print(" ${problem_id.assigned_to},");}


  if(email_action.recipient_fields.indexOf('request.requested_for') > -1){template.print(" ${request.requested_for},");}


  if(email_action.recipient_fields.indexOf('request.user') > -1){template.print(" ${request.user},");}


  if(email_action.recipient_fields.indexOf('requested_by') > -1){template.print(" ${requested_by},");}


  if(email_action.recipient_fields.indexOf('requested_for') > -1){template.print(" ${requested_for},");}


  if(email_action.recipient_fields.indexOf('request_item.request.requested_for') > -1){template.print(" ${request_item.request.requested_for},");}


  if(email_action.recipient_fields.indexOf('sysapproval.opened_by') > -1){template.print(" ${sysapproval.opened_by},");}


  if(email_action.recipient_fields.indexOf('sys_created_by') > -1){template.print(" ${sys_created_by},");}


  if(email_action.recipient_fields.indexOf('task.assigned_to') > -1){template.print(" ${task.assigned_to},");}


  if(email_action.recipient_fields.indexOf('task.assigned_to.manager') > -1){template.print(" ${task.assigned_to.manager},");}


  if(email_action.recipient_fields.indexOf('to_users') > -1){template.print(" ${to_users},");}


  if(email_action.recipient_fields.indexOf('user') > -1){template.print(" ${user},");}


  if(email_action.recipient_fields.indexOf('user.manager') > -1){template.print(" ${user.manager},");}


  if(email_action.recipient_fields.indexOf('watch_list') > -1){template.print(" ${watch_list},");}


  if(email_action.recipient_fields.indexOf('work_notes_list') > -1){template.print(" ${work_notes_list},");}


  if(email_action.recipient_fields.indexOf('u_assigned_to') > -1){template.print(" ${u_assigned_to},");}


  if(email_action.recipient_fields.indexOf('u_caller_id') > -1){template.print(" ${u_caller_id},");}


  if(email_action.recipient_fields.indexOf('u_downtime_date.u_director_on_call') > -1){template.print(" ${u_downtime_date.u_director_on_call},");}


  if(email_action.recipient_fields.indexOf('u_fd_contact') > -1){template.print(" ${u_fd_contact},");}


  if(email_action.recipient_fields.indexOf('u_phonemsg_call_for') > -1){template.print(" ${u_phonemsg_call_for},");}


  if(email_action.recipient_fields.indexOf('u_problem_requestor') > -1){template.print(" ${u_problem_requestor},");}


  if(email_action.recipient_fields.indexOf('u_requested_for') > -1){template.print(" ${u_requested_for},");}


  if(email_action.recipient_fields.indexOf('u_team_leads') > -1){template.print(" ${u_team_leads},");}


  if(email_action.recipient_fields.indexOf('u_task.assignment_group.manager') > -1){template.print(" ${u_task.assignment_group.manager},");}



//If Event Parm 1 is checked, get the display value.


//Query against the User table, for the Sys_id.   if it matches, template.print the "Name"


//If Sys_id fails, then query the user_name,   if it matches, template.print the "Name"


//if user_name fails, then query the "name" field.   if it matches, template.print the "Name"


if(email_action.event_parm_1 == true){


  var parm1 = event.parm1.getDisplayValue();



  var foundRepRec = false;


  var repRec = new GlideRecord('sys_user');


  repRec.addQuery('sys_id', parm1);


  repRec.query();


  if (repRec.next()) {


  foundRepRec = true;


  } else {


  repRec = new GlideRecord('sys_user');


  repRec.addQuery('user_name', parm1);


  repRec.query();


  if (repRec.next()) {


  foundRepRec = true;


  } else {


  repRec = new GlideRecord('sys_user');


  repRec.addQuery('name', parm1);


  repRec.query();


  if (repRec.next()) {


  foundRepRec = true;


  }


  }


  }


  if (foundRepRec === true) {


  template.print(" " + repRec.name + ", ");


  }


}




//If Event Parm 2 is checked, get the display value.


//Query against the User table, for the Sys_id.   if it matches, template.print the "Name"


//If Sys_id fails, then query the user_name,   if it matches, template.print the "Name"


//if user_name fails, then query the "name" field.   if it matches, template.print the "Name"


if(email_action.event_parm_2 == true){


  var parm2 = event.parm2.getDisplayValue();



  var foundRepRec1 = false;


  var repRec1 = new GlideRecord('sys_user');


  repRec1.addQuery('sys_id', parm2);


  repRec1.query();


  if (repRec1.next()) {


  foundRepRec1 = true;


  } else {


  repRec1 = new GlideRecord('sys_user');


  repRec1.addQuery('user_name', parm2);


  repRec1.query();


  if (repRec1.next()) {


  foundRepRec1 = true;


  } else {


  repRec1 = new GlideRecord('sys_user');


  repRec1.addQuery('name', parm2);


  repRec1.query();


  if (repRec1.next()) {


  foundRepRec1 = true;


  }


  }


  }


  if (foundRepRec1 === true) {


  template.print(" " + repRec1.name + ", ");


  }


}




if (!email_action.u_email_kb.nil()) {template.print("<br />Knowledge Base #: " + email_action.u_email_kb.getDisplayValue());}


  //-------------------------------------------------------------------------------------------------------------------------------


template.print("<br />");


template.print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ <br />");





again,   dont judge the script.   it works for us.  


Thanks for your response.So i need to create mail script or notification.or template.Where i should include it