Reply
Regular Contributor
nicksquash
Posts: 57

Re: Email-to-Case Auto-response email loop

We've created a trigger that seems to work pretty well.

 

We had a pretty bad experince the other day with a game of auto-response tennis creating well over a thousand cases.

 

We've created the following trigger that in initial testing seems to work pretty well.

This is just a re-worked version of Curry-Man's code

 

 

trigger LoopKiller on Case (before insert) {
  /*
  * Email Loop Killer
  * Will not process new email if there are atleast 4 emails from 
  * the same email with the same subject and the previous email was less than 5 minutes ago
  */
  case[] c = trigger.new;

  case[] check = [select ID, CreatedDate, subject from Case where SuppliedEmail = :c[0].SuppliedEmail and subject = :c[0].subject and isclosed = false order by CreatedDate desc];
  system.debug(c[0].SuppliedEmail);
  system.debug(c[0].subject);
  
  
  if(c[0].Subject != null) {  
    //We have a subject, proceed.
    if(c[0].subject.contains('[ ref:')){
      //No Errors.  Email should be attached to the case.
    }else{
      if(check.size() > 3){
        if((check[0].createddate.addMinutes(5) > System.now()) && check[0].subject.contains(c[0].subject)){
          c[0].addError('Automatic email loop has been terminated');
          //Loop Was Killed.
        }else{
          //New Case should be created now!       
               }
      }
    }
  }
}
 



 

Regular Contributor
ChadMeyer
Posts: 48

Re: Email-to-Case Auto-response email loop

Our approach to email loop protection was to "flag" the case subject and add a filter/exclusion criteria to our auto-response or workflow rules to prevent the auto-response.  By "flag", I mean that we pre-pend "Email Loop Protection" to the subject line and use a "Does Not Contain" filter to prevent the auto-response from triggering.

 

We didn't want to stop processing emails and creating cases just in case there was a valid reason why multiple emails should be received.

 

Something to keep in mind when designing your trigger.  This is also part of Email to Case Premium on the AppExchange