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 ACCEPTED SOLUTION

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!!

 

View solution in original post

8 REPLIES 8

Got it — right now your script is evaluating the current values for all three fields, so Q1 still prints because it’s still yes.

If you only want to show what changed on this update, use the “changed to yes” check:

var daText = '';

if (current.u_q2.changesTo('yes'))
  daText += "section2? yes<br>";

if (current.u_q3.changesTo('yes'))
  daText += "section3? yes<br>";

template.print(daText);

That will only print Q2/Q3 when they were updated to yes on this update, and it won’t repeat Q1 unless Q1 also changed to yes.

Also make sure the notification is set to send When: Updated (not “Inserted or Updated”).

 

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

Hopefully this work?

 

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

Thanks again @Matthew_13 but changesTo() is not available in mail scripts.

@sparkles Hopefully this help you?