Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Add info to subject with Email Script in Email Client Template (email action on a Change record)

dorianmorel
Tera Contributor

Hello there,

 

I am trying to run an email script to add the current month to the subject, but it won't work.

 

DorianMOREL_0-1764341225408.png

Any idea why ?
I'm calling my email script in the HTML field of the Email Client Template. It should be running when a user wants to send an Email with the email feature on a Change record.

 

If it is impossible with this method, is there any workaround to achieve what I want to do ? 

 

Thank you in advance.
Dorian

 

1 ACCEPTED SOLUTION

Sheldon  Swift
ServiceNow Employee

Hi @dorianmorel - You would get the current email subject with email.getSubject() With your code, something like this should work:

 var months = ["JANVIER", "FEVRIER", "MARS", "AVRIL", "MAI", "JUIN", "JUILLET", "AOUT", "SEPTEMBRE",
     "OCTOBRE", "NOVEMBRE", "DECEMBRE"
 ];
 var nb_month = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];

 var gdt = new GlideDateTime();
 var month = gdt.getMonth().toString();

email.setSubject(email.getSubject() + " " + months[nb_month.indexOf(month)].toString());

Keep in mind that you can get the current month name from GlideDate if you want to skip the mapping:

var date = new GlideDate();
var month = date.getByFormat("MMMM");

 

UPDATE: Initially, I didn't realize you're using an email client template. I'm not sure this is possible with an email script (see below). You could get creative with a business rule to append the month, but would need to think about how you can make sure you're only appending the month where it makes sense, and not overwriting a user-provided subject.

 


Variables accessible from script:

  • current: Current GlideRecord
  • template: Handles printing to the email message
  • email: EmailOutbound object (null when called from Client Template)
  • email_action: GlideRecord for the email notification (null when called from Client Template)
  • event: GlideRecord for the event that fired the notification (null when called from Client Template)

View solution in original post

3 REPLIES 3

Anand__99
Kilo Sage

Hi @dorianmorel ,

 

Try this:

    var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    var gdt = new GlideDateTime(); //November
    var current_month = gdt.getMonth(); // returns number

    email
.setSubject("Test Subject : " + months[current_month - 1]);


Thanks

Anand

Sheldon  Swift
ServiceNow Employee

Hi @dorianmorel - You would get the current email subject with email.getSubject() With your code, something like this should work:

 var months = ["JANVIER", "FEVRIER", "MARS", "AVRIL", "MAI", "JUIN", "JUILLET", "AOUT", "SEPTEMBRE",
     "OCTOBRE", "NOVEMBRE", "DECEMBRE"
 ];
 var nb_month = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];

 var gdt = new GlideDateTime();
 var month = gdt.getMonth().toString();

email.setSubject(email.getSubject() + " " + months[nb_month.indexOf(month)].toString());

Keep in mind that you can get the current month name from GlideDate if you want to skip the mapping:

var date = new GlideDate();
var month = date.getByFormat("MMMM");

 

UPDATE: Initially, I didn't realize you're using an email client template. I'm not sure this is possible with an email script (see below). You could get creative with a business rule to append the month, but would need to think about how you can make sure you're only appending the month where it makes sense, and not overwriting a user-provided subject.

 


Variables accessible from script:

  • current: Current GlideRecord
  • template: Handles printing to the email message
  • email: EmailOutbound object (null when called from Client Template)
  • email_action: GlideRecord for the email notification (null when called from Client Template)
  • event: GlideRecord for the event that fired the notification (null when called from Client Template)

Hello Sheldon, sorry for the delay

 

That's what I thought... It isn't possible from Client Template... A bit sad and I don't really understand why, but at least I know now ! 

 

Thank you for your time and details, I will accept your answer as a solution and will continue to look into this with your idea of business rule.

 

Best regards,

Dorian