The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Apply Email Template in Notification

Steven Young
Tera Guru

Hey Again all you Genius's.   I need your help again.

This is kind of a "idea" rather than it being functional (i think).

So here's the deal.

I love ${mail_script:scriptname}   it's awesome.   Very functional, works like a charm.

We have a request item that we have an approval email for.   Then there is a UI action that was create to fire an Event.   We created a 2nd email notification to accept this event to fire the "Resend email"

here's the deal.

The approval email we have a ${mailscript}   at the very top which contains our approval/reject links.   and Information about the request.
Then we have the BODY of the email.   Contains the variables of the request.

Then we have 2 ${mailscript}'s at the bottom with some "for the administrator"   which tells us the ticket number, what email notification send this email, and so forth.

approval.JPG

Here is where the question comes in.   We cant use an HTML template for all the notifications because we add different ${mailscripts}
approval2.JPG

What i'm wanting to know is:
Can we Create a template, that is dynamic like the mail scripts

So in my email notification, i dont have a template "attached" and used.
BUT.   i used something like ${template:templatename}   Because the body of my email wont change.

Is there a way to do something like so.

${mail_script:Approval-Reminder}
${mail_script:Approval-Links}


${template:templatename}

${mail_script:Approval-Additional}


${mail_script:Approval-Administrator}

Where the ${template} will actually pull the HTML data out of the template.
This way i would only have to update it 1 time and it would update in all 3 of my notifications.
I do not want to use the text box.   i want to use HTML.   i dont want to have to use the <mail_script> template.print() for every single line,   Hence using the HTML (WYSIWYG)

Does that make any sense at all?

it would be great for the templates if you attach it as is to the email and it's used with no modifications.   or if you could "call" the template data into the actual email notification like the mailscripts.

1 ACCEPTED SOLUTION

Steven Young
Tera Guru

So i found my answer.   Dot Walk.


Create an email script, say called "template"


Script will be


template.print(email_action.template.message_html);



That will take the reference field of the "template" from the email notification and then pull the HTML field from the template.



Then in the email notification you can do a ${mail_script:template}   and that is all that is needed.



So now my email Notification html box looks like so:
Capture1.JPG




It will translate into this


Capture2.JPG


View solution in original post

34 REPLIES 34

Steven Young
Tera Guru

You could write a notifcation email script to pull the specific templates you want to use.



i just wanted a way to use the template but able to add a few more things to a certain notification.



So i use the Base notification from a Template, but i have the template applied to 3 or 4 different notifications.   i dont want 4 notifications the Exact same.   So i add another script or 2 to each notification along with the base template.



If you know you want 3 templates,   you could do a gliderecord script.   or you could add 2 more reference fields to the email templates on the notification page and then use the same type of ${mail_script:template}       /   ${mail_script:template1}     /   ${mail_script:template2}



If you do a hard coded mail script looking for specific templates, you'll have to write a lot of scripts.   if you add more fields to the form, then you can easily customize each notification using the base script.


Steven Young
Tera Guru

Another option could be:



Write a business rule that Everytime you create a Template, it automatically created a email script.  



i.e.(   you create   a template named   "Help for the Lost",   you have coded the business rule to create an email script.   ${mail_script:Help for the Lost}   which is a gliderecord script, looking at the email template table, and pulls the text field/s of that template.)


Then in your maintenance aspect.   Create another Business rule that when the Template is "Inactive" you either delete the script or make it inactive.


So Everytime you create a Template, you'll have a new email script created automatically with the same name of the template.




I personally do not prefer this way, because then you   need to keep up with a ton of names.




If you know you're going to use several templates for 1 email, i would prefer to put more "template" fields on the form.


find_real_file.png



Email will contain this:


email template.jpg



So now, you could just change the template fields on the form, and not have to worry about constantly changing the ${mail_script:name}   in the body of the Message HTML


Ok,   so here is what i've done for you.



1.   When you create a Template, it will Create a ${mail_script: } for you.


2.   Will Tie the Mail Script to the Template so it can be easily found.


3.   a way to auto pull the data from templates via ${mail_script:}'s



BEGIN:


  1. On the Email Template form,   Create a new field for a link to the email script
    • Label:   Email Script
    • name: u_email_script
    • Make Read Only if you wish, i did.
    • type: reference
    • reference:   Email Script   (will have to do from dictionary and not form design.   form design doesn't give you the table option)
    • find_real_file.png

  2. On the Email Script form,   Create a New field for a link to the email template.
    • Label: Template
    • name:   u_template
    • Make Read Only if you wish, i did.
    • type: reference
    • reference:   Email Template   (will have to do from dictionary and not form design.   form design doesn't give you the table option)
    • find_real_file.png



  3. Create this Business Rule FROM THE EMAIL TEMPLATE TABLE.
    • Business Rule.jpg

(function executeRule(current, previous /*null when async*/) {



  // Add your code here


  var gr = new GlideRecord("sys_script_email");


  gr.new_lines_to_html = 'true';


  gr.name = current.name;


  gr.u_template = current.sys_id;


  gr.script = "// ${mail_script:" + current.name + "}     Mail script name for use in the Email Notifications\n\n";


  gr.script += "var gr = new GlideRecord('sysevent_email_template');\n";


  gr.script += "gr.addQuery('sys_id', '" + current.sys_id + "');\n";


  gr.script += "gr.query();\n";


  gr.script += "if (gr.next()) {\n";


  gr.script += "       template.print(gr.message_html);\n";


  gr.script += "}\n\n";


  var scriptID = gr.insert();



  current.u_email_script = scriptID;



})(current, previous);





That's it.



NOW.


  1. Create a new template.
    • find_real_file.png
    • The script will fill in the Email Script name.  



  2. The Script will Create a new Email Script
    • find_real_file.png
    • It will fill in the Template Field

  3. Edit the Email Notification with your   new script
    • find_real_file.png
  4. Trigger the email:
    • Here is what was delivered.
    • email.jpg
    • Notice that the Template Script 2 and 3 are blank?   that is becuase the template fields on the form are blank. as seen in #3.
    • So you can do it either way.

That's pretty nifty.



Its wonderful that we can come up with 10 different solutions, but SN hasn't implemented anything easier.


I would just make that business rule an after business rule instead of a before.


I think I will give this a shot when I get some time.



Another solution that is (definitely) less flexible but completely doable is to add reference fields on the Notification form called Header (u_header) and Footer (u_footer) to supplement the out-of-box Template (template) field.



Really I personally wouldn't need more than 3 templates on a single notification, but I can definitely think of many who would use more.



If you wanted one more (besides the 3 mentioned) you could add another call Body Template (u_body) if you needed to.



I wouldn't go any further than that though...


Hey Daniel.  



Yes, i agree with you to a point.   Service Now has to build for a Global user.   We each are building for an individual use or user.


There is no way in the world Service Now would be able to implement everything.



Butthe good thing about servicenow, is that we as a community can come up with 10 different ways to do things the same way.



Not everyone uses the "Header" "Footer" "Body"   like that..     Its a great idea but it's very simple to add 2 fields to the form and create a script or template for that.



Service Now is very robust and adaptable and that means that 10 people can find 10 different ways to accomplish the same thing.   and for that i commend them.   There are things that could be easier, and if you have some ideas,   submit them to service now as enhancements,   that is the only way they will get feedback from us.   I have already submitted several enhancement requests to them.