function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dan Dodd 3Dan Dodd 3 

email to case stopped working with before insert trigger

I have email-to-case working great until I add a trigger to the process and then email-to-case stops working.
I have commnted out most everything and it still fails to run. here is what I need to run:
 
trigger CaseInsertTrigger on Case(before insert, after insert){
if (Trigger.isBefore && Trigger.isInsert) {
 Set<String> conEmailSet = new Set<String> ();

 String userEmail;
 for(case cs: Trigger.new){
  if(cs.contactid == null) {
   if (cs.description !=null && cs.subject.mid(0, 12) == 'Issue Report') {
    userEmail = cs.description.substringBetween('Email: ',' EndEmail');
    if ( userEmail != null) {
      conEmailSet.add(userEmail);
      system.debug(userEmail);
    }
   }
  }
 }

Map<String, Contact> cEmailMap = new Map<String, Contact>();
for(Contact eMap : [select Email, Id from contact where Email IN :conEmailSet]) {
 cEmailMap.put(eMap.Email,eMap );
}
Contact tmpContact;
for(case cs: Trigger.new){
  if(cs.description !=null && cs.subject.contains('Issue Report') ) {
    userEmail = cs.description.substringBetween('Email: ',' EndEmail').trim();
    system.debug('Queue for Case:--->' + cs.OwnerId);
    system.debug('Email for case: ' + userEmail);
    if ( userEmail != null) {
      tmpContact = cEmailMap.get(userEmail);
      if ( tmpContact != null ) {
        cs.ContactId = tmpContact.Id;
        system.debug('Contact for Case:--->' + tmpContact.Id);
      } else {
        //here ToDo crreate a contact
    }
    cs.SuppliedEmail = userEmail;
    // ToDo OwnerId ie Queue app queue , why does ?
    // ToDo cs.SuppliedName
  } else {
    system.debug('Contact for Case:--->None ');
  }
 }
}
}

}

 
Best Answer chosen by Dan Dodd 3
AnudeepAnudeep (Salesforce Developers) 
Hi Dan, 

Added this trigger in my local org. I see the case getting created using Email-to-case (None of the debug statements in the code were displayed though. May be the critieria was not met?)

User-added image
User-added image

Are you seeing any exception at your end? Thanks

Anudeep




 

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Dan, 

Added this trigger in my local org. I see the case getting created using Email-to-case (None of the debug statements in the code were displayed though. May be the critieria was not met?)

User-added image
User-added image

Are you seeing any exception at your end? Thanks

Anudeep




 
This was selected as the best answer
Dan Dodd 3Dan Dodd 3
Hi Anudeep,

No exceptions on my end, I don't see the case..
I'll look in Classic. We don't use it.

Thank you
Dan
Dan Dodd 3Dan Dodd 3
I worked thru and got the code working, I was getting exceptions but I didn't see them. Tweaking the code.
Thanks