Email trigger to send an email on update of particular Field

Posted by Paulh0763 on 25-Mar-2015 10:29

My client would like to know when a field(s) is updated and wants the email to display the before and after values for the particular field(s) that might have changed. I created prior fields on the object and a trigger to update the fields with the prior value...if that makes sense? Here is a snippet of code:

"{!first_check_date}"!="{!prior_1st_check_date}" ? rbv_api.setFieldValue("account", {!id}, "prior_1st_check_date", '{!prior_1st_check_date}') : "";

"{!aca#id}"=="" ? rbv_api.setFieldValue("account", {!id}, "prior_aca", '') : "";
"{!aca#id}"=="22129437" ? rbv_api.setFieldValue("account", {!id}, "prior_aca", 'Yes') : "";
"{!aca#id}"=="22129438" ? rbv_api.setFieldValue("account", {!id}, "prior_aca", 'No') : "";

"{!acaia#id}"=="" ? rbv_api.setFieldValue("account", {!id}, "prior_acaia", '') : "";
"{!acaia#id}"=="28689052" ? rbv_api.setFieldValue("account", {!id}, "prior_acaia", 'Yes') : "";
"{!acaia#id}"=="28689053" ? rbv_api.setFieldValue("account", {!id}, "prior_acaia", 'No') : "";

It seems to work but it also gives out false information. Example, if the field is NULL the prior field will display "No" or "Yes" depending on the change of the current field instead of indicating that prior value was NULL.

I also have a formula field that I am using in the email template to show bring in what values have changed but it is not functioning properly. I hope someone can direct me to what I need to fix. Snippet of code on formula field below.

var count = 0;
var outputText = '';

if ('{!first_check_date}' != '{!first_check_date#before}')

count ++;

outputText += "First Check Date has changed from '{!prior_1st_check_date}' to '{!first_check_date}' <br></br>";

if ('{!aca#id}' != '{!aca#id#before}')

count ++;

outputText += "ACA value has changed from '{!prior_aca#value}' to '{!aca#value}' <br></br>";

if ('{!acaia#id}' != '{!acaia#id#before}')

outputText += "ACAIA value has changed from '{!prior_acaia#value}' to '{!acaia#value}' <br></br>";

count ++;

if ('{!acaia#id}' != '{!acaia#id#before}')

outputText += "ACAIA value has changed from '{!prior_acaia#value}' to '{!acaia#value}' <br></br>";

count ++;

if (count > 0){

return outputText ;

} else {

return '';

}

All Replies

Posted by Godfrey Sorita on 25-Mar-2015 12:25

You can use #before tokens to get the previous value of your fields in triggers. Then use the prior_field tokens for your email template.

Posted by pvorobie on 25-Mar-2015 13:04

This will work for "after update" triggers. Example of email template:

Counter has changed from {!Counter#before} to {!Counter}

Posted by Paulh0763 on 26-Mar-2015 10:04

How do I get the template to display only the records that have changed? I tried using a  count++  function but it is not working. What I want is the fields that are set to "No" or null (these are picklist fields) on the record, and when they change to "Yes" I want only that field to display. If there is more than one field changed to "Yes" then I want only those fields. I do not want fields that have been changed to "Yes" prior to the update. Only current change...make sense? I am using a formula field and the token from the formula field on my template to display changes.

Posted by pvorobie on 26-Mar-2015 11:29

If you wish to use JavaScript in your Email template - use EVAL[] block. Template by itself is just a text (HTML).

Posted by Godfrey Sorita on 26-Mar-2015 13:02

You if statement should be enclosed with curly brackets {} when you have multiple statements to execute. Otherwise, it will only execute the first statement.

Example: The below code will append outputText whether the condition is true or false.

if ('{!first_check_date}' != '{!first_check_date#before}')
count ++;
outputText += "First Check Date has changed from '{!prior_1st_check_date}' to '{!first_check_date}' <br></br>";

Change it to:

if ('{!first_check_date}' != '{!first_check_date#before}') {
  count ++;
  outputText += "First Check Date has changed from '{!prior_1st_check_date}' to '{!first_check_date}' <br></br>";
}

This thread is closed