How to add email script to email template

joshehren
Kilo Expert

Hello.   Thanks in advance for your help.   I've seen a number of discussions on how to do add an email script to an email template, but it isn't working for me.

Scenario:   I have a notification where I add header and footer templates by using a ${mail_script:header} and a ${mail_script:footer} email script.  

In the footer template, I would like to include an email script that updates the year automatically.   However, when the footer template is added into the notification, the email script (mail_script:GetYear) doesn't convert to the year.   However, it works fine if I keep it in the notification, which is not what I want...email.png

Any advice?

Thanks again,

josh

1 ACCEPTED SOLUTION

Hi Josh,


Sorry!!! I don't know why this is not working .May be what Berny mentioned above that can be the reason . If not then you can achieve this by writing a script include and calling   it from mail script.


Script Include:


Name: EmailUtils


Client Callable:Unmarked


Active:Marked


Script:


var EmailUtils = Class.create();


EmailUtils.prototype = {


  initialize: function() {


  },


  emailFooter: function(){


    var time=gs.now();


  var SplitTime=time.split('-');


  var Year=SplitTime[0];


  var Emailfooter='<table id="body" style="width: 100%; height: 100%; border-collapse: collapse; background-color: #f5f5f5;" border="0" width="100%" cellspacing="0" cellpadding="0" bgcolor="#f5f5f5"><tbody><tr><td align="center" valign="top" bgcolor="#f5f5f5" width="100%" height="100%"> </td></tr><tr><td style="width: 550px; height: 20px;" width="550" height="20"> </td></tr><tr><td style="width: 550px; font-size: 11px; font-family: Arial, Helvetica, sans-serif; color: #333; text-align: center; line-height: 19px;" align="center" width="550">Liberty University Information Technology<br /> 1971 University Boulevard &bull; Lynchburg, VA 24515 &bull; (866) 447-2869<br /> &copy; Copyright '+Year+' &bull; Liberty University is a Registered Trademark &bull; <a href="http://www.liberty.edu/index.cfm?PID=6901">Privacy Policy</a></td></tr><tr><td style="width: 550px; height: 30px;" width="550" height="30"> </td></tr></tbody></table>';


  return Emailfooter;


  },


  type: 'EmailUtils'


};


find_real_file.png


Email Notification Script:


Name:email_footer


Script:


(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,


/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,


/* Optional GlideRecord */ event) {


  var emailUtils = new EmailUtils();


  template.print("<tr><td>"+ emailUtils.emailFooter()   +"</td></tr>");


})(current, template, email, email_action, event);


find_real_file.png


Notification:


Message HTML:${mail_script:email_footer}


find_real_file.png



Notification Preview:




find_real_file.png



Let me know for further queries ............



Thanks,


Farukh



Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

10 REPLIES 10

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Josh



On which version of Snow are you working at the moment ?


I've noticed that in some cases using the RichText Editor on email if you cut and paste something like your code


e.g. ${email_script:yourScriptName}



the system copy something like


${email_script:<tag>yourScript}



This means that in the editor seems clean but behind the scenes will not work.


Try to check HTML Source code.



Cheers


Ivano


Hello Ivano--


Thanks for the reply.   We're using Geneva.



I checked the source code in my footer template and it looks correct:



<p></p>


<table id="body" style="width: 100%; height: 100%; border-collapse: collapse; background-color: #f5f5f5;" border="0" width="100%" cellspacing="0" cellpadding="0" bgcolor="#f5f5f5">


<tbody>


<tr>


<td align="center" valign="top" bgcolor="#f5f5f5" width="100%" height="100%">  </td>


</tr>


<tr>


<td style="width: 550px; height: 20px;" width="550" height="20">  </td>


</tr>


<tr>


<td style="width: 550px; font-size: 11px; font-family: Arial, Helvetica, sans-serif; color: #333; text-align: center; line-height: 19px;" align="center" width="550">Liberty University Information Technology<br /> 1971 University Boulevard &bull; Lynchburg, VA 24515 &bull; (866) 447-2869<br /> &copy; Copyright ${mail_script:GetYear} 2016 &bull; Liberty University is a Registered Trademark &bull; <a href="http://www.liberty.edu/index.cfm?PID=6901">Privacy Policy</a></td>


</tr>


<tr>


<td style="width: 550px; height: 30px;" width="550" height="30">  </td>


</tr>


</tbody>


</table>


<p>  </p>


<p><!-- end content block --></p>


<p>  </p>


<p>  </p>


<p>template.print(gdt.getYear()); ;</p>



What do you recommend?



josh


fkhan
Kilo Guru

Hi Josh,



For footer you can write   below given mail script and add this at the end of either notification or your footer template.



Mail Script:



(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,


/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,


/* Optional GlideRecord */ event) {



  var time=gs.now();


  var SplitTime=time.split('-');


  var Year=SplitTime[0];


  template.print(Year);



})(current, template, email, email_action, event);



find_real_file.png


I am not sure it is your requirement or not ,if not please clarify your issue again.....


Thank you for the reply, Farukh.   It looks like you've provided a mail script for getting the current year, but I already have this.   Here's what I'm trying to do:   I've making an email notification and in the message box it looks like this:



${mail_script:Header}



[Body of the email]



${mail_script:Footer}



See below...


find_real_file.png




All this is fine and good. But see the {mail_script:footer} in the screen shot above?   That footer mail script points to a template (the code for which is provided in my 9/23 9:30am reply) that contains another mail script.   This mail script (${mail_script:GetYear}) works fine if I put it directly into the notification, as I've already done with the footer and header.   But it doesn't work when placed in the footer template.   This is what the notification now looks like...



find_real_file.png




Thanks again for any help you can provide.



josh