+ Start a Discussion
GMASJGMASJ 

Trigger is not firing on case when case is created via email

Hi, 

 Below trigger on case is not firing when case is created via email. Please suggest me how to fix thi issue.
Trigger
======
trigger CaseTrigger on Case (Before Insert){

   CaseTriggerUtils.processInsert(Trigger.new);

}


Class
=====
public class CaseTriggerUtils {
    
    public static void processInsert(list<Case> cseLst) {        
        processCase(cseLst, new Set<String>{'FW:', 'RE:', 'RW:'});  //Keywords are passed     
    }   
   
 public static void processCase(list<Case> cseLst, Set<String> keyWords) {
        String CaseSubject;
        String CaseId;
        String CaseOwnr;
       
        Map<String, List<String>> keyWordSubjectListMap = new Map<String,List<String>>();
        list<Case> caseLst = new list<Case>();
       
        for (Case c : cseLst) {    
            CaseId = c.id;   
            if(c.subject <> null){
                for(String keyWord : keyWords) {
                    if(c.Subject.contains(keyWord)) {
                        CaseSubject = c.subject.replaceAll(keyWord,'');
                        List<String> subjectList = keyWordSubjectListMap.containsKey(keyWord) ?
                        keyWordSubjectListMap.get(keyWord) : new List<String>();
                        subjectList.add(CaseSubject);
                        keyWordSubjectListMap.put(keyWord, subjectList);
                        break;
                    }
                     if(CaseSubject == null)  
                       CaseSubject = c.subject;
                }
            }
        }
        System.debug('Case Subject' +  CaseSubject);
        System.debug('MAP' + keyWordSubjectListMap);
        
       list<case> cseQry = [select id, casenumber,subject,createddate,ownerid,owner.name from case where CreatedDate = this_year and id <> :CaseId and subject like :('%' + CaseSubject + '%') order by createddate asc limit 10];
        //system.debug('Existing Cases :'  + cs.casenumber + ' ' + cs.subject + ' ' + cs.createddate + ' ' + cs.owner.name); 
        
       for(Case cs: cseLst){     
         if(!cseQry.isEmpty()){  
           if(cs.ownerid <> cseQry[0].ownerid)   // if owner is different from the current owner, then update the owner and set duplicate flag.
             cs.ownerid = cseQry[0].ownerid;   
             cs.Duplicate_With__c = cseQry[0].casenumber;   
           if ( cs.subject.length() > 18 ){    //keyword check is more than 18 character remove keyword and save.
              cs.subject = CaseSubject;
            } 
          }     
       }
        
 }
    
}
Thanks
GMASJ
 
Michael BrumleyMichael Brumley
This seems like the same issue I've just dealt with in an Enhanced Email trigger - if so, you need a trigger on the EmailMessage object instead. In my case I was after Tasks created when emails were sent from Opps so I needed the ActivityId to get to them, you probably want the RelatedToId.