I would like to know is it possible to send out a notification whenever a record is edited.

I want the trigger to fire on any field change - I have about 10-15 field so I prefer not to create multiple workflow rules and different emails.

I want one email that indicates the updated field, the new value and the editor (last modified by)


1 Answer 1

trigger CaseOnParticularFieldUpdate on Case (after update) {
for (Case c: Trigger.new) {
    Case oldCase = Trigger.oldMap.get(c.ID);
    if (c.Field != oldCase.Field) {
        // field was updated, do some magic here

Here is the sample how you will detect field value has changed .

Now on detect of field change you have to write an outbound email service .

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       // email address of the salesforce email service
       String[] toAddresses = new String[] {'sfa_emailservicehandler@25e8oml5i5zvz5qahgfim4c52w4q9pnwnsb2ufic9cbjerogdg.w-4syhmaq.w.apex.sandbox.salesforce.com'};
       mail.setSubject('Test Batch' );
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Here is a simple example how to write in apex to send email.

You may need to combine both of the snippet and tweak a bit to have a working code .

Also i guess you will have to inspect each of the fields to find out what has changed and put that in a variable of array.

Also remember to comment out the code if huge data load is done .Since it may cross the no of email allowed per day

  • that's great! I thought about storing the update field label and value in two custom fields ("last updated field label" and "last updated field value") and use those in email alert in Workflow rule. What is prefferable?
    – Itay B
    Commented Oct 21, 2012 at 9:19
  • To store in a custom field you will spend two new fields .Dont prefer that since you will spend unnessary data storage limit of org.Just have a variable in your class or trigger itself and then put in your body of the email. Also if you are familiar with custom settings you may store your static data there Commented Oct 21, 2012 at 9:24
  • I'm not familier with Custom settings yet... so is it outbound email that holds a variable of the changed field and value? I'm trying to figure out how's the trigger is built...if field A is changed, send email with field A parameters, if field B is changed, send same email with field B parameteres etc...
    – Itay B
    Commented Oct 21, 2012 at 9:40
  • Declare a variable in the trigger and populate with changed value and field name using if else if logic and then at finally you use outbound email.The link indicated by techrecker is also good for figuring how to get field label Commented Oct 21, 2012 at 11:23
  • If you store the last updated field label and value, what about cases where more than one field was updated? Would there then be multiple notifications? Commented Oct 21, 2012 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .