How to apply a template to an email in a script

jdanonuevo
Kilo Expert

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

7 REPLIES 7

edwin_munoz
Mega Guru

Hello Justin,



Sorry if I'm not understanding correctly. If you you events and notifications, couldn't you be able to access the email id by using the email_action object?



If this doesn't help at least I gave a bump to your post.



Thank you.


Good idea, but the email_action is the notification that is being fired, not the instance of the email. There is the 'email' object, but that seems to be a special object without standard glide record properties (such as sys_id) or methods available.



I attempted to find the email object using the event object, since that actually references the email record, but that turned up to have an empty field in it. My guess is when the mail script is processing, the email record hasn't been saved or initialized yet.



Thanks for the idea though.


Gurpreet07
Mega Sage

I don't think its possible through script but you could change data dynamically in the template . Find my Reply On following post.



Multiple Email Template for One email Notification.


Absolutely, Using the tokens in the template, such as ${URI} or ${current.name} is what I want to do. But when you create the email using a script I cannot find a way to apply the template in such as way that it will process those into 'http://thing.service-now.com[...]' or 'Justin'. Right now I actually search for \$\{[\w\.\-_]+}\ manually and dot walk to the requested field. But that doesn't work with special tokens such as URI. I can write that, but I want to avoid fully reimplementing the entirety of template processing.



What I'm looking for is a native method or class that can do that processing for me within the platform and get all the new features from ServiceNow as they come out.



Using <mail_script> tags wouldn't work either since I would have to write an evaluator for that. If you use the mail_script modern functionality, I would have to turn that into a glide record query and then an eval. Also when someone random updates the template in a year, they will inevitably use ${uri}, have it fail, and be confused. Thats why I want to stick to the platform as much as possible.



Thanks for the advice though.


-Justin