Let's say we have a custom class Incident, which represents some sort of a customer-impacting incident (like a service outage). Every incident is related to Account through a standard lookup. Incidents have an ETR (estimated time of resolution).
Whenever one or more incidents have their ETR updated, we want to email the customer and notify them of the fact. The email should contain the list of incidents that have been updated and their new ETRs. Incidents are often updated in batches by an external system through the API.
I have no problem with the trigger that figures out which incidents should be included in an email, but I'm struggling with how to send this information to the VisualForce template. I can access the list of all incidents related to an account if I set the account as the related object, but how do I know which ones just changed without the trigger context?
I've thought about adding a new field to incidents to mark them as "ETR changed", and then clear the flag after the email is sent. This way I can render only those incidents in the VF template that have the flag set. I'm not sure if Messaging.sendEmail(email) is synchronous, however, and am worried about race conditions with sending the email and clearing the flag.