How to create Dynamic Subject of email client Template?

Community Alums
Not applicable

I want to set the subject of my email client template:-

I created an email script and used:-

email.setSubject('');

and called the email script with tag ${mail_script:'name od email script'} in the body of the template.

But the value of the subject is not populating.

Please help!!!!!

19 REPLIES 19

Chaddie
Tera Contributor

As another alternative you can create Before Insert Business Rule on table Email[sys_email] with conditions against the table

Example Business Rule for Catalogue Task Email Client emails:

'Target table' is 'sc_task' and 'Headers' contains 'X-ServiceNow-Source: EmailClient' 

In the Business Rule Advanced tab you can query your record using the 'instance' and dynamically set the subject based on the state/attributes of your target record.

var catTaskGr = new GlideRecord('sc_task');
if(catTaskGr.get(String(current.instance))) {
     current.subject = catTaskGr.short_description + " - Test";   // Here we set the Email Subject
}

 See attachment.

 

Hi,

Thanks for posting and trying to help 🙂

While what you've suggested would be "a" possible solution, the issue there is that it overrides what the user selected for the subject field. Perhaps they typed out something specific, etc.

Business rules are nice, but when it overrides or takes away from what the client/user expects, it doesn't necessarily have the greatest outcome.

With that said, unfortunately, I don't believe we ever heard how "dynamic" they wanted it to be, but if they wanted to accomplish what you were trying to do here (using short description field value for example), then they could simply use this in the email client template:

${current.short_description}

find_real_file.png

And that would always jump start the email client with the short description from the current record (as those templates are associated per table) and the user could change it if needed.

Please mark reply as Helpful/Correct, if applicable. Thanks!

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi,

I was only demonstrating a very simple example and that's why I used 'Short Description'.  I am aware of being able to substitute record data through the use of ${<field name>} within the Email Client Template e.g. ${short_description}.

An Email [sys_email] record is created in the database every time you launch the Email Client from a record. When this email record is created it defaults the "Subject" from the Email Client Template "Subject" value, "Blind copied" from the Email Client Template "Bcc" field, "Copied" from the Email Client Template "Cc" field and "Recipients" based on the Email Client Template "To" field. 

If one did not wish to override what values came from an Email Client Template they could first check in the Business Rule whether a value already existed.

e.g. Checking if the subject on the Email [sys_email] record doesn't already have a value in the Business Rule.

if (!current.subject) { // If Email Client Template did not have a subject specified
  current.subject = catTaskGr.short_description + " - Test";  // Set a new subject
}

The user after launching the Email Client can always change anything they want, Subject, To, Cc, Bcc, email content etc before actually hitting the send button.

If a user launches the Email Client and does not hit the "Send" button and closes the dialogue window an Email [sys_email] record will already have been created (as mentioned previously). However, if you open the created Email [sys_email] record it will have the "Error string" set with the value "User did not press the Send button in Email Client" and "Type" field set as "send-ignored". 

One can be as creative as they want with the conditions in which the Business Rule should run and the associated action to be taken. As always, great care should be taken whenever making the decision to write Business Rules on "sys" tables. Hopefully ServiceNow will provide support for the "email" object for Email Client Templates in future releases.  

Hope this helps 🙂 

 

Hi,

Thanks!

Side note, we don't know...what you know, hopefully that's fair enough to say?

I suppose me saying "overriding" the user's selection regarding what you suggested (a before insert business rule -- I try not to download attachments since they can be pasted into the posts itself and I assist hundreds of people per year and it takes up space, so I didn't see that), wasn't relevant. So that's on me, sorry about that.

Overall, the best practice guidance here is to utilize the email client and feature set available and then, if for some reason you can't or your scenario is more edge case, then review the possible use case for a business rule or other means.

So it definitely has pros and cons, but again, nice suggestion with an alternative approach. In the context of this question though, it could be perceived that since the author didn't mark an answer as correct and this post was over 2 years old...you suggesting what you did was attempting to solve their unsolved problem (which we try to do by providing best practice guidance as the "ultimate approach"). You may not have entered the same thread and posted what you did if it had a correct answer already marked? Maybe? haha...I don't know...

However, the author never stated the context of "dynamic" (if it was as simple as just helping populate the short description or to another extent) and what that meant to them, so we may never know.

I assume your comment about support for the "email object" in email client template was in literal terms, meaning being able to directly just type email.whatever in the email client template and work with it that way, but mail script (thus the email object) can be used in the email client template, same as in notifications, so it is available in the same context.

Either way, thanks again for trying to help!

-Allen

 

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi,

The whole reason for my reply to this 2 year old post was that I was looking for a solution to be able to dynamically set the subject in the Email Client based on a customer's business requirements. I came across this post Googling for a solution.

Jiri Hrdlicka commented earlier to this post that the email object (on a Notification Email script) is currently not supported by Email Client Templates and as such one can't simply call email.setSubject("Test") to be able to set the subject if the Email Client Template uses the mail script.

I confirmed this was still the case in my PDI (San Diego release).

I 100% agree with you in regards to Best Practice being to use an OOTB solution before deciding to go the custom route.

I'm definitely not looking for a thumbs up/correct answer points but in my effort to meet the customer requirements for this POC I found this as a possible way forward and thought to share for those who may also be looking for a solution to the same problem.

 

Cheers,
Chaddie