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 

Replace the "From" of Email-to-Case in the Case so it gets attached to the correct Contact

Hi,
I am using a third party software that collects my Issue reports from the original party (OP) and emails them to Salesforce Email-to-Case. 
It uses the OPs email address as the "FROM" address and Salesforce uses that email to connect it to the Contact. I get good matching when the Contact exists.

The problem comes in is that we should use our email NOT the OP's email as the "From" when sending email.
 
Now when we send the email we need to replace the "From" of Email-to-Case in the Case so it gets attached to the correct Contact.

I can put the "from" email anywhere in the body and was thinking of putting it at the start of the body:
'Email: ' + email + ' EndEmail'
  so I could use the string operator:
fromEmail = str1.substringBetween('Email: ' , ' EndEmail');
and put it in the Case from field in a before insert trigger.
Will this work with the the Email-To Case flow?

 
Dan Dodd 3Dan Dodd 3
This trigger works great with an API call but now the Email-to-Case does not execute to create a case.
Here is the Trigger:
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 ');
  }
 }
}
}

}