Email Client Script Dynamically Changing Subject Line

Rae Khan
Tera Contributor

Hi all, 

 

I am wondering if it is possible to create a dynamically changing subject line for an email, however I am restricted by

the character limit of the subject line. I just wanted to know if it is possible to change the subject line based on values within a form, and I attempted to do so using the ternary operator. 

 

Ex: 

(${u_update_number}) > 0 ? '[Update ${u_update_number}] ${u_headline} (Sev ${u_severity})': '${u_headline} (Sev ${u_severity})';

 

I am however unable to input this entirely due to the character limit on the subject line.

Is there any way around this?

If I could add the entire code, would this even work?

 

Thank you, 

1 ACCEPTED SOLUTION

SunilKumar_P
Giga Sage

Hi @Rae Khan, You can dynamically change the subject line for an email using the Notification Email Scripts.

 

Regards,

Sunil

View solution in original post

2 REPLIES 2

SunilKumar_P
Giga Sage

Hi @Rae Khan, You can dynamically change the subject line for an email using the Notification Email Scripts.

 

Regards,

Sunil

Sumanth16
Kilo Patron

Hi @Rae Khan ,

 

Yes, you can achieve this by using a single notification and customising the email content and subject dynamically based on your conditions using a mail script. Here's an example of how you can do this:

  1. Create a single notification record with a default email body and subject that will be used if none of the conditions are met.

  2. Write a mail script that checks the conditions you want to use to dynamically change the email content and subject. For example, you could use the current date and time to check if the reminder is for 1 week, 2 weeks, or the final reminder.

  3. In the mail script, use the email.setSubject() and email.setBody() methods to dynamically set the email subject and body based on your conditions.

  4. Call the mail script from your reminder script to send the email notification.

Here's an example code snippet:

 

var now = new GlideDateTime();
var reminderDate = new GlideDateTime();
reminderDate.addWeeks(1);
// Check if this is the 1 week reminder
if (now.equals(reminderDate)) {
  // Set the email subject and body for the 1 week reminder
  email.setSubject("1 Week Reminder: Your Task is Due Soon");
  email.setBody("Hello,\n\nThis is a reminder that your task is due in 1 week. Please complete it as soon as possible.\n\nThank you.");
}
// Check if this is the 2 week reminder
else if (now.equals(reminderDate.addWeeks(1))) {
  // Set the email subject and body for the 2 week reminder
  email.setSubject("2 Week Reminder: Your Task is Due Soon");
  email.setBody("Hello,\n\nThis is a reminder that your task is due in 2 weeks. Please complete it as soon as possible.\n\nThank you.");
}
// Check if this is the final reminder
else if (now.equals(reminderDate.addWeeks(1))) {
  // Set the email subject and body for the final reminder
  email.setSubject("Final Reminder: Your Task is Overdue");
  email.setBody("Hello,\n\nThis is a final reminder that your task is overdue. Please complete it as soon as possible.\n\nThank you.");
}
// If none of the conditions are met, use the default email subject and body
else {
  email.setSubject(gs.getMessage("Task Reminder"));
  email.setBody(gs.getMessage("This is a reminder that you have a task that is due soon."));
}

// Send the email notification
email.send();

 

Refer to below threads:

https://www.servicenow.com/community/in-other-news/email-client-amp-client-templates/ba-p/2286626

https://www.servicenow.com/community/developer-forum/dynamic-email-subject/m-p/1387868

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda