Update on any record update

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)

Answer

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.setToAddresses(toAddresses);
       mail.setSubject('Test Batch' );
       mail.setPlainTextBody(body);
       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

Attribution
Source : Link , Question Author : Itay B , Answer Author : Mohith Shrivastava

Leave a Comment