Please help with setSubject in email script

Jessica28
Tera Guru

Hello,

I'm attempting to overwrite the email subject using an email script, but I'm unable to make it work. Please advise.

Here is what I have tried:

 

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

    // Add your code here
    var RITM = current.document_id.getRefRecord();
    var subject = RITM.variables.email_subject;
    email.setSubject(subject);
})(current, template, email, email_action, event);
 
It is printing out "Undefined" on email subject
 
Jessica28_0-1697662787406.png

 

 

8 REPLIES 8

Hi @Prince Arora 

My apologies for not clarifying which table the email script is written for. This is because I was not familiar with the script, and I did not know what I was doing. The email script is invoked from the email notification on the sc_req_item table.

 

I was wondering if the following email script is used on the sysapproval_approver table, would it still work the same like it did for sc_req_item table?

 

Jessica28_0-1697725973263.png

 

Thank you

@Jessica28 

 

The script mentioned by you is working for "sc_req_item" only because variables are available on RITM.

if your email script is on another table say "sysapproval_approver" you need to modify the script.

 

var ritm = new GlideRecord("sc_req_item");

ritm.get(current.sysapproval);

ritm.query();

if(ritm.next()){

subject = ritm.variables.email_subject.getDisplayValue();

email.setSubject(subject);

}

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

 

 

Sandeep Rajput
Tera Patron
Tera Patron

@Jessica28 Simply update your script as follows.

 

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

    // Add your code here
    var RITM = current.document_id.getRefRecord();
    var subject = RITM.variables.email_subject.toString();
    email.setSubject(subject);
})(current, template, email, email_action, event);

Hope this helps.

Hello @Sandeep Rajput 

I have tried you suggestion code and it did not work.