Including "Email Scripts" in a template or notification

Makosko
Tera Expert

Guys,

 

has anybody managed to figure out how to include an email script in a template ?

 

Cheers

Maros

17 REPLIES 17

marcguy
ServiceNow Employee
ServiceNow Employee

you mean an email template yes?


I normally do this in the Message field (call a script include to return the message).



<mail_script>


var dem = new NotificationEmail();


template.print(dem.send(current.sys_id));



template.print('<a href="' + gs.getProperty('glide.email.override.url')   + '?uri=unsubscribe.do?sysparm_notification=' + email_action.sys_id   + ">Click here to Unsubscribe</a> from the "' + email_action.name + " Notification<br />\n');


</mail_script>


</font>




The script include contains the code to build the message and return it to the template.



The bottom piece you can ignore, this is to include a 'unsubscribe' link for this notification type.


Hey,



can you go to "sys_script_email". How do i include those ??



Cheers


Maros


mguy,



got any examples of script includes in which you build up your message content ? it would be interesting to see what techniques you are using...



Cheers


Maros


marcguy
ServiceNow Employee
ServiceNow Employee

Sure, I can give an example of a weekly project manager summary I send out rather than spamming them all with individual notifications.



so my email template would call the script via the line:



template.print(my script include).



within that script include (code below) basically build a HTML body, table, etc and return it so an example would be find a list of projects and their children tasks and send the user a summary of those that they project manage (excuse some of the lazy html, I need to set some time to clean up and modernize ). It looks ok in emails honestly!!




var WeeklyProjectManagerEmail = Class.create();



WeeklyProjectManagerEmail.prototype = {


  initialize : function() {


  },



  send : function(msi) {



var prj = new GlideRecord('pm_project');


  prj.get(msi);



  var uri = gs.getProperty('glide.instance.url');



  outputString = '<body style="color: #000000; font-family: arial, helvetica, sans-serif; font-size:12px;">'


  + '<span style="color: #000000; font-family: arial, helvetica, sans-serif; font-size:12px;" >';


  th = "<th bgcolor=#C0C0C0>";


  tableHeader = "<table style='width: 90%; color: #000000; font-family: arial, helvetica, sans-serif;   font-size:12px;'>";


  tableFooter = "</table></br></br>\r\n";




  outputString = outputString +"<b><a href=" + uri + "pm_project.do?sys_id="+prj.sys_id+">"+prj.number + "</a>"+" "+"</b> - " + prj.short_description.getDisplayValue() + " "+"</br>State: " + prj.state.getDisplayValue() + "</br></br>";




  var ptch = new GlideRecord("pm_project_task");


  ptch.addEncodedQuery('parent=' + prj.sys_id);


  ptch.orderBy('ptch.start_date');


  ptch.query();


  if (ptch.hasNext()) {


  gs.log('found some records that need patch scheduler to look at ','debug.u_patching');


  outputString = outputString + "<b>Project Tasks for " + prj.number + "</b><br><table style='width: 90%; color: #000000; font-family: arial, helvetica, sans-serif;   font-size:12px;'>";



  outputString = outputString + "<tr>"+th+"Number</th>"+th+"Planned Start</th>"+th+"Planned End</th>"+th+"Title</th>"+th+"State</th>"+th+"Assignee</th></tr>";


  while (ptch.next()) {


  outputString = outputString +"<tr><td width='150px'><a href=" + uri + "pm_project_task.do?sys_id="+ptch.sys_id+">"+ptch.number + "</a>"+" "+"</td><td width='150px'>" + ptch.start_date + " "+"</td><td width='150px'>" + ptch.end_date + " "+"</td><td>" + ptch.short_description.getDisplayValue() + " "+"</td><td width='100px'>" + ptch.state.getDisplayValue() + " "+"</td><td width='200px'>"+ ptch.assigned_to.getDisplayValue() + "</td></tr>";



  }


  outputString = outputString + "</table><table style='width: 90%; color: #000000; font-family: arial, helvetica, sans-serif;   font-size:12px;'>";


  }



  outputString = outputString + tableFooter + '</body></span>';



  return outputString;




  },



  type : WeeklyProjectManagerEmail


  };