I’m trying to solve the bouncing email issue based on “Mail box full” by creating a trigger that resends the message if the message contains “mail box full”.
The issue I’m facing is that I need to limit the number of resend to 3 times.
What I have now keeps resending the email as soon as a bounced email is received.
My trigger is
trigger trgBouncedEmails on EmailMessage (after insert) {
for(EmailMessage myEmail: trigger.New) {
//mail box full bounced email
if (myEmail.HtmlBody.contains('full'))
{
Case[] parentCase = [Select c.Id from Case c where c.Id =: myEmail.ParentId];
if (myEmail.Subject.contains('Financial Review'))
parentCase[0].Resend_Email_Send__c = true; // this will trigger a workflow to send the email again.
Update parentCase;
}
}
}
How can I limit the resending, is there a way I can set a wait time before I do the “Update parentCase”
Is there a better way to solve this issue, knowing that I have different types of emails, each one has a different template and a different purpose.
EDIT
The system should automatically re-send the email 3 times in the frequency of 24hours, and then stops resending after 24hrs. My trigger keeps resending indefinitely and I’m trying to find a way to put a wait so it can only send 3 times in 24hrs period, like once every 8hrs.
@grigriforce beat me to the punch – I would also suggest using a field to count the number of retries, rather than a simple boolean value. Here’s a “bulkified” trigger with essentially the same logic as the one you posted:
Update to space emails out evenly over a 24-hour period:
Rework your workflow to make sure it has been 8 hours since
Resend_Email_Time__cwas last set, and then schedule an Apex job to run every hour to pick up eligible Cases that need to have their emails resent, and call update on them to make sure the workflow doesn’t go too long without firing:**note that it’s not a best practice to have this code in the class that implements Schedulable – this code would ideally be in another class that is called by the
ResendCaseEmailsclass.You can schedule this job to run once an hour by calling this code from the developer console: