How to apply a template to an email in a script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2015 05:02 PM
Short Story:
I want to create an email via a script and apply an email template via the same script. I cannot use the typical event -> notification method (more on that in the long story). I must create the email manually. current.ApplyTemplate does not work since it only queries against the sys_template table not the sysevent_email_template table. Is there another method call that can apply the email template in a script?
Long Story:
We want to send an email with select, not all, attachments. I have looked and cannot find any way to do this out of the box. I have created a UI Action/UI Page combination that prompts the user to select attachments from that record and manually creates the email, attempts to apply an email template, copies the attachment/attachment_doc and saves it for sending. All of this works except applying the template.
I tried using events and notifications, but while passing the attachment sys_id's was simple enough with an event, mail scripts did not have access to the email sys_id, so I couldn't use a mail script to copy the attachment. I tried a business rule, but since business rules don't have access to the event data, it couldn't find out what attachments it wants to send. I thought about the scratchpad, but that only exists for server -> client transactions, not client -> server transactions. I could have stored the list in a field, but then I would be storing ephemeral data in a persistent data structure. (Which you have to worry about clearing, and if you don't clear it, you can have remnants or someone may attempt to report on the field, etc)
Ultimately, and unfortunately, the I go back to the 'do it all in a script' idea but the applyTemplate method does not work for emails. Of course we can create a template in the sys_template table, but then we would have an email template outside of the email templates (I want to avoid storing data in unusual places). I have succumbed to writing my own template interpreter, which quickly got far too big to be a good idea (is it ever a good idea?). Getting fields in the interpreter was easy, but then there was the ${URI} tokens and ${URI+} token, etc, etc.
So what I'm asking for, is if anyone out there knows of a way to call the method for applying a template to an email via a script and abstract out the token handling from me.
Thanks for all the help.
-Justin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2015 06:20 PM
I am not sure if this will help but I saw this one the share. It may help.
https://share.servicenow.com/app.do#/detailV2/4fbe64672ba6e1004a1e976be8da1572/overview
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-08-2015 09:15 AM
Good find. I had heard about that method, but not that someone had implemented it in something we can all see. Thanks for finding that.
I like the idea and had taken that approach first, but in the interest of modularity, sustainability and extensibility, I wanted to avoid putting a rules and fields on the sys_email table for my one situation.
Also that relies on my altering both the sys_email table and the incident form to handle this. My method does not need any modification to any table and only fires when you click the button to send attachments. This is very nice when you want to send this one attachment this one time and then another attachment another time. You don't have to manage a 'u_send' field in a related list. Just check the boxes you want to send this time and problem solved. And by moving the content to a template that can be specified in a system property, it makes it nice and maintainable without altering any tables.
Hence the problem, I want to apply that template in my script...
Thanks for the find I will spend more time pouring over it, but I'm, still trying to find a way to apply an email template via a script.
-Justin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2015 04:27 AM
Hi Justin,
Have you been able to make any progress?
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/