The CreatorCon Call for Content is officially open! Get started here.

Set dynamic subject for email client template

Thi Dinh Nguyen
Tera Expert

Hello all,

I'm trying to create an email client template on the Incident Communication Task table. However, the subject of the template requires me to use field values from the related incident to populate. Here's what I have attempted:

- Use the ${field name} syntax in the Subject field. This worked, but the subject field only allows 100 characters, so I couldn't put all variables required in the subject.

- Create an email script. It didn't show up when I call it in the body, and when I put it in the subject instead, it just displayed the ${mail_script:...} line.

- Create a script include function and call it via javascript. This also didn't work.

So how can I achieve this? I appreciate your help.

Thanks

6 REPLIES 6

Basheer
Mega Sage

Hi @Thi Dinh Nguyen,

Can you paste your line of code in message body from where you've called the mail script and the code in mail script, it seems there may be issue in the script which you are trying to execute.

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

In the Body HTML field I put this line before the body

${mail_script:get_ict_email_client_subject}

Here's is my email script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

          // Add your code here
	var subject = "New P" + current.incident_alert.source_incident.priority + " " + current.incident_alert.source_incident.number + " - " + current.incident_alert.source_incident.short_description;
	email.setSubject(subject);
})(current, template, email, email_action, event);

 

Thanks @Thi Dinh Nguyen ,

I was not able to find any mistake in the script which you've written, but I would suggest to debug as below to find out the exact issue

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

gs.log("Into the mail script : 1");
var priority = current.incident_alert.source_incident.priority;
gs.log("Into the mail script : 2 " +  priority);
var number = current.incident_alert.source_incident.number;
gs.log("Into the mail script : 3 "+number);
var shDesc = current.incident_alert.source_incident.short_description;
gs.log("Into the mail script : 4 "+shDesc);
          // Add your code here
	var subject = "New P" + current.incident_alert.source_incident.priority + " " + current.incident_alert.source_incident.number + " - " + current.incident_alert.source_incident.short_description;
gs.log("Into the mail script : 5 "+subject);
	email.setSubject(subject);
})(current, template, email, email_action, event);

 

After changing this code, execute it via preview template option on notifications and navigate to system logs and check what logs have been printed

Filter the logs as message starts with Into the mail script : 

From the logs we would be able to know whether the email script triggered or not and if triggered till where it went.

From there we can find what the actual issue is.

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

Hello Bashir, I tried adding log statements and could confirm that there's nothing wrong with the email script. I also created a test notification and it also worked correctly.

However, I'm trying to apply this to an email client template instead and in the log it displayed the error "Cannot convert null to an object." for the email.setSubject(subject); statement. I suspect that they don't use object 'email' in the email client template.