Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@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:</p> <pre><code>trigger trgBouncedEmails on EmailMessage (after insert) { List&lt;Id&gt; parentCaseIds = new List&lt;Id&gt;(); for ( EmailMessage myEmail : trigger.New ) { // mail box full bounced email for Financial Review emails if ( myEmail.HtmlBody.contains('full') &amp;&amp; myEmail.Subject.contains('Financial Review') ) parentCaseIds.add(myEmail.ParentId); } Case[] parentCases = [select c.Id from Case c where c.Id in :parentCaseIds]; for ( Case c : parentCases ) { c.Resend_Email_Count__c += 1; // this will trigger workflow to send the email again c.Resend_Email_Time__c = System.now(); // keep track of when it was last retried } update parentCases; } </code></pre> <p>Update to space emails out evenly over a 24-hour period:</p> <p>Rework your workflow to make sure it has been 8 hours since <code>Resend_Email_Time__c</code> was 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:</p> <pre><code>global class ResendCaseEmails implements Schedulable { global void execute(SchedulableContext sc) { Contact[] cs = [select id, Resend_Email_Count__c, Resend_Email_Time__c from Contact where Resend_Email_Count__c &lt; 4]; List&lt;Contact&gt; ups = new List&lt;Contact&gt;(); for ( Contact c : cs ) { if ( c.Resend_Email_Time__c != null &amp;&amp; c.Resend_Email_Time__c.addHours(8) &lt; System.now() ) ups.add(c); } update ups; } } </code></pre> <p>**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 <code>ResendCaseEmails</code> class.</p> <p>You can schedule this job to run once an hour by calling this code from the developer console:</p> <pre><code>ResendCaseEmails sched = new ResendCaseEmails(); String cron = '0 0 * * * ?'; System.schedule('Resend Case Email Job', cron, sched); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload