I need to send an email to one user that the following records are modified by user. For ex: If one user modified same field in 5 records at a time, then the modified data and previous data needs to club in one mail and that mail needs to be send one user. Can we achieve this through trigger or any other way. I've tried with workflow but i couldn't make it.
2 Answers
Yes you can handle this bulk in trigger (after update) by using Trigger.oldMap and Trigger.new in a loop and checking oldfield!=newfield values. Then, get the old and new fields values using same map and store it in a string.
I believe we can get the current user Id (who modified the data) using Userinfo.getUserId() then query this Id in User object, to get User's email (if you want to send mail to the user who modified) or if its specific User then set the email inside trigger.
Then, you can send the string using Messaging.SingleEmailMessage (mail) methods ( setToAddresses, setPlainTextBody ) and Messaging.sendEmail (new Messaging.SingleEmailMessage[] {mail}
@chris, on your above commented question: If you want to try in workflow rule, you can create new field for the old value. To get old value, create a workflow rule with Criteria: ischanged(field) [ I believe field is Answer__c ] and Add workflow action: Field update to update the new field with: Priorvalue(field) [ i.e Priorvalue(Answer__c) ]
Now you can add existing field + new field which is updated with the old value in your email alert template. But you can't do this for your bulk update as mentioned above. 5 emails gets fired for 5 updates.
-
or instead getting the current user email address. just use targetObjectId with current userId in singleEmailMessage. Commented Jun 6, 2016 at 5:38
-
Hi @Valli S, My major requirement is to bulk updated data needs to be send in one email. Can we make that by using any visual workflow or workflow or any other way?– user24737Commented Jun 7, 2016 at 18:57
-
@chris, Process builder also works like workflow rule. i.e. update of 5 records triggers the process builder and execute each operation for 5 records individually. so 5 email gets fired. I feel trigger is easy way to do this.– Valli SCommented Jun 8, 2016 at 7:48
You can achieve this using trigger after update, and take a list to store the records that are updated. Then send emails using sendemail() method.