How to dynamically change content & subject of email notifications(reminder mails)

Raviteja Kunal1
Tera Expert

I trying to dynamically change the content & subject of the reminder email notifications based on conditions like 1 week, 2weeks, final reminder etc rather than using multiple notifciations?

Can we achieve this using a single notification and by using a mail script?

 

TIA

1 ACCEPTED SOLUTION

Ratnakar7
Mega Sage
Mega Sage

Hi @Raviteja Kunal1 ,

 

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();

 

 

you can also refer - Scripting examples for email notifications 

 

If my response helps you to resolve the issue close the question by Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.

 

Thanks,

Ratnakar

View solution in original post

5 REPLIES 5

Sai Kumar B
Mega Sage
Mega Sage

@Raviteja Kunal1 

Add Notification Subject somewhere in the OOB look-up table by creating records and Use the email script to set notification subject and content on basis of the week by querying the lookup table

OR


You can use system properties as well to store email subjects and content

If I could help you with my response you can mark it as helpful and correct as it benefits future viewers
Thanks,
Sai Kumar B
Community Rising Star 2023 & 2022

 

Thank You, How to check the date in the notification script to set values like complete one week, 2 weeks etc? Can you share a sample script?

Ratnakar7
Mega Sage
Mega Sage

Hi @Raviteja Kunal1 ,

 

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();

 

 

you can also refer - Scripting examples for email notifications 

 

If my response helps you to resolve the issue close the question by Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.

 

Thanks,

Ratnakar

Raviteja Kunal1
Tera Expert

Thank you for your quick response. One question if we want to customize days like 3 days, every Thursday rather than 1 week we can do it in the script?