Email script

sparkles
Tera Contributor

Hi,

 

I have an email script for notification, I use it when record's insert to display the answer, and for another notification on update to display the updated answer. 

The script display the question if the answer is yes 

Example:

user answer for:

Q1 = yes

Q2 = n/a

Q3= no

email notification will contain the Q1 only (this part is working)

after 2 days user needs to update their (n/a or no) answer

Q1 = yes

Q2 = yes

Q3= yes

email notification on update display Q1 = yes only and ignore the updated answer. I checked the back-end and I can see the value updated. I am using the same code/script for both notifications, just changed "when to send"

=====================================================

(function runMailScript(current, template, email, email_action, event) {
    var daText = '';

 

    if (current.variables.Q1 == 'yes') {

 

        daText += "section1? " + current.variables.Q1 + "<br>";
    }
if (current.variables.Q2 == 'yes') {

 

        daText += "section2? " + current.variables.Q2 + "<br>";
    }
if (current.variables.Q3 == 'yes') {

 

        daText += "section3? " + current.variables.Q3 + "<br>";
    }
    template.print(daText);


})(current, template, email, email_action, event);
 

Not sure what's wrong with it, variable names are correct and it display all if the answer is yes to every question.

I hope someone can help with this

Thanks

S

1 REPLY 1

Matthew_13
Kilo Sage

Your script itself is fine from what i see.

The issue is maybe what record the update notification is firing on.

On insert the notification might be running on the RITM so current.variables is correct. On update; it’s probably running on a Catalog Task or parent record, where current.variables doesn’t reflect the updated answers — which is why only Q1 shows.

Fixx it by explicitly pulling variables from the RITM when needed:

var v = current.variables;
if (current.request_item) {
  v = current.request_item.variables;
}

if ((v.Q1 + '') == 'yes') template.print("section1? " + v.Q1 + "<br>");
if ((v.Q2 + '') == 'yes') template.print("section2? " + v.Q2 + "<br>");
if ((v.Q3 + '') == 'yes') template.print("section3? " + v.Q3 + "<br>");

Also double check that the update notification condition actually fires when Q2/Q3 change; big one.....

 

@sparkles - Please mark as Accepted Solution and Thumbs Up if you find Helpful!!

Thanks @Matthew_13  I am not using RITM. Its running on a custom table.

 

Hi @sparkles,

Since this is a custom table as you say, the current.variables isn’t positive unless the record has a variable pool there.

If those answers come out of record producer, load the variables in the mail script kinda like:

var vp = new GlideappVariablePool(current);
vp.load();
var q2 = vp.getQuestion('Q2');

Then check q2.getValue() instead of current.variables.Q2.

If they’re regular fields on the custom table, reference them directly, for like current.u_q2), not current.variables.

This maybe why the insert works but update doesn’t... it’s a variable loading issue, less your logic.

 

@sparkles - Please mark as Accepted Solution and Thumbs Up if you find Helpful!!