How can i send email on change of fields in single trigger

Posted by Sudhakar on 23-Jul-2014 14:03

Hi all,

i need to send email if any of these fields(phone,mobile,first name,last name,email) change,is it possible to achieve the requirement in single trigger.

And the email template should contain like following :

The following fields have changed in this record:

emal has been changed from xy@gmail.com to abc@yahoo.com
phone number has been changed from 123 to 456

Can anyone pls guide me how can i achieve above. 


All Replies

Posted by Gian Torralba on 23-Jul-2014 14:50

HI Sudhakar,

Since you can't use the [tag:before] values in the email templates. What I'm thinking of is to create fields that will hold the previous values and then comparing it and generating the phrase in a formula field. Please follow the trigger hierarchy:

  1. Send email trigger (Send Email | After Update)
  2. Update previous field values (Object Script | After Update)

These are the steps you must follow:

  1. Create fields that will hold the previous values e.g (previous phone number)
  2. Create a formula field that will be used in the email template to show/hide words that are not needed
  3. Create an email trigger
  4. Create the object script that will have the rbv_api.setFieldValue() API for updating multiple fields in a single trigger (to avoid using different update field value trigger

In your send email trigger compare the previous field and the new field. This will send the email only if there is a change in the conditions specified below e.g

if("my name" != "my new name" || "my number" != "my new number") return true;

In your formula (type string) field that will create the content:

var content = "";

if("my name" != "my new name")

content += "name is changed from abc to efg</br>";

if("my number" != "my new number")

content += "number is changed from 123 to 456</br>";

return content;

Hope this helps,

Thanks,

Gian

Posted by pvorobie on 23-Jul-2014 16:11

You can use Condition formula (available on the bottom of "Edit Trigger" page) to calculate when trigger should run.

Posted by Sudhakar on 23-Jul-2014 16:35

how can i check the values are changed are not in condition formula (i have nearly 20 fields) among the 20 fields if field 2,5,8,10 changed then i need to send mail saying that

"the following fields has been changed :

Here is the list of fields changed(2,5,8,10)

Posted by Gian Torralba on 23-Jul-2014 17:52

You can use a formula field inside the email template and the formula field content should somehow contain a script similar to this:

var content = "";

if("my name" != "my new name")

content += "name is changed from abc to efg</br>";

if("my number" != "my new number")

content += "number is changed from 123 to 456</br>";

return content;

This will now handle the content dynamically based on the if condition.

Let me know if you have more questions on this.

Thanks,

Gian

Posted by Sudhakar on 24-Jul-2014 13:05

i have gone through what you posted [mention:c5dec1ce5a5948c5a25cf4bcaeb7db77:e9ed411860ed4f2ba0265705b8793d05] and i have customized it , now i am able to send mails if any changes in list of fields after update.

Now my question is how can i know list of fields that are changed after update.why because i need to mention the list of fields in Email that are changed after update.

Posted by Sudhakar on 24-Jul-2014 13:28

Ohh got it but still need little more info if i change both fields at at time the formula field(which is storing change values like audit trail) is congaing only one field change data.

Posted by Gian Torralba on 24-Jul-2014 14:02

The formula field should be able to display all field changes since there are multiple if condition that will join the words into a single return value. Please check the script in the previous post. That formula field token is the one that you will add in the email template.

Let me know if you need more clarifications.

Thanks,

Gian

Posted by Sudhakar on 24-Jul-2014 14:15

thanks  [mention:c5dec1ce5a5948c5a25cf4bcaeb7db77:e9ed411860ed4f2ba0265705b8793d05] it is working.But when we are implementing it in live Object due to filed limitation we do not have space to store all fields previous values,is it possible to to store all previous values in single text area fields and complete the same functionality 

Posted by Sudhakar on 24-Jul-2014 18:10

Hi [mention:c5dec1ce5a5948c5a25cf4bcaeb7db77:e9ed411860ed4f2ba0265705b8793d05] ,

We do not have enough fields to store previous values.Can you pls suggest me how can i complete above scenario.

Posted by Gian Torralba on 25-Jul-2014 10:47

Hi Sudhakar,

Actually you can. You  can store them in a textarea separated by comma and then putting it in a javascript array to be compared. Another complex way is to create a new object that will house the previous value fields with a 1-1 relationship to that object. This way, you will use the related token instead.

Hope this helps,

Thanks,

Gian

Posted by Sudhakar on 25-Jul-2014 12:54

thanks for your suggestion,i have found relationship object to current object,im trying object script(timing before update) to store before values but im not able to store previous values,can you pls guide me where im going wrong.

this the script im wring in parent object

var x= rbv_api.getFieldValue("secondary", {!id}, "interest_rate");

rbv_api.setFieldValue("secondary_2", {!R63597782.id}, "Before_Interest_Rate", x);

Posted by Gian Torralba on 25-Jul-2014 13:36

I think you are missing the createRecord trigger. As I said before it will be more complex to create a new record using a create record trigger and a data map that will attach the current record to the secondary object. Please follow this new steps.

  1. Create data map to secondary object (map the look up of the source object to the secondary object)
  2. Create record trigger to create the secondary object ( After create )
  3. Send Email trigger
  4. Object script to update the fields in the secondary object.

You dont need to use getFieldValue because you can access it via token e.g. ({!firstname#value}) in the main object.


Let me know if you need more information.

Thanks,

Gian

Posted by Sudhakar on 25-Jul-2014 15:21

we are having same amount of records in both parent(secondary) and child(Secondary2) object. i am also thinking doing this way is little bit complex.

So i think it is better to move in 2nd way for that i have stored all previous values in one text area separated by comma,can you pls guide me now how can i compare previous value(which is stored in text area) to current value

Posted by Gian Torralba on 25-Jul-2014 16:02

In this case you can just use the update field value trigger and then create a string return containing all field values e.g

var val = "{!firstname#value},{!phone#value}";

return val;

After that when comparing the fields to the value use this:

var arr = "{!textarea#value}";

arr = arr.split(); //this will convert the string int an array;

var username = arr[0];

var phone = arr[1];

//then add the condition as mention before

if(username != " {!firstname#value}") // send email

Please note that the formula field that will be displayed in the email will contain a code similar to this to check if the value is changed or not.

Thanks,

Gian

Posted by Sudhakar on 28-Jul-2014 12:59

Hi [mention:c5dec1ce5a5948c5a25cf4bcaeb7db77:e9ed411860ed4f2ba0265705b8793d05] ,

I have storied the before values in a merge field with the following script(the code which you mentioned is storing current values is not working)  with trigger type:: Validate Record Date and Timing :: Before Update.

rbv_api.setFieldValue("jm_emp", {!id}, "merge_data", "");

var a= rbv_api.getFieldValue("jm_emp", {!id}, "lastName");

var b= rbv_api.getFieldValue("jm_emp", {!id}, "firstName");

var c= rbv_api.getFieldValue("jm_emp", {!id}, "city");

var d= rbv_api.getFieldValue("jm_emp", {!id}, "phone");

var e= rbv_api.getFieldValue("jm_emp", {!id}, "status");

var z=[];

z= a+","+b+","+c+","+d+","+e ;

rbv_api.setFieldValue("jm_emp", {!id}, "merge_data", z);

and it is storing previous values in a text area as expected the sample out put like below

Merge Data:: M,Jigs,Ahmedabad,(702)555-,89826413  

for  comparing the before values with current values im using trigger type:: Object script and timing :: After update with following script

var s = rbv_api.getFieldValue("jm_emp", {!id}, "merge_data");

var fields = s.split(",");

var lname = fields[0];

var fname = fields[1];

var cname= fields[2];

var pname= fields[3];

var sname= fields[4];

if (lname != "{!lastName#value}" || fname != "{!firstName#value}" || cname != "{!city#value}" || pname != '{!phone#value}' || sname != '{!status}' )

{

rbv_api.runTrigger("jm_emp", {!id}, 'tmail', true);

}

else

{

rbv_api.runTrigger("jm_emp", {!id}, 'fmail', true);

}

But here the comparing the values in if conditions is returning always true even through  there is no the no change in before and after field updates.

Kindly let me know where im doing missing.

Posted by Gian Torralba on 28-Jul-2014 13:52

Hi Sudhakar,

Let me correct some of the code you used:

For getting the field values to store in the text area, You don't need to use the rbv_api.getFieldValue() since you can access it via token in the drop down on the top right of the formula text area inside the trigger. e.g

var z = "{!lastName#value}"+"{!lastName#value}";  //using the getFieldValue API will affect performance instead of using the available tokens in the object.

Can you check the trigger hierarchy:

1. Object script to compare trigger //this will compare and send the email

2. Update field value of the merge token //this will set the data on the merge field

You can also use the debug function in the trigger and use rbv_api.println() to display the values and be sure that the data is correct inside the condition.

Thanks,

Gian

Posted by Sudhakar on 28-Jul-2014 14:41

Hi [mention:c5dec1ce5a5948c5a25cf4bcaeb7db77:e9ed411860ed4f2ba0265705b8793d05] ,

Did you got expected output for the storing previous values and comparing values if yes pls share SS and kindly let me know which trigger type and timing u have taken(for both triggers ),why bcz if i used the code which you mentioned above it is storing the current values in text area field.

Posted by Gian Torralba on 28-Jul-2014 15:04

Hi Sudhakar,

I forgot to mention that you need to use "After Update" for both triggers. You are correct, it will store the current values in the textarea that's why the trigger hierarchy is important. You will store the current values in the textarea so next time you compare the values it will go thru trigger #1 first before re updating the values on trigger #2.

Thanks,

Gian

This thread is closed