1

I am attempting to allow users from my company's sales department to email potential customers from a lead in service cloud, then if the customer replies to their email i need to display the email response on the lead, however, the email they are replying to is also setup for email-to-case so it creates a case (which i need it to not create) and links the email message to the case record. I have attempted to do things like clone the email message and create emailmessagerelation records to link the message to the lead but i am not having success having them display on the lead record. My only 'workaround' currently is that i created a Lead__c lookup from the emailmessage object and i populate that based on the reference:{xxxxxxxx(lead id)} that the sales user will add to the message before sending the email so the emailMessage trigger will know that the message should be linked to a lead record, then i have a related messages component (related list) where those messages are displayed. I need the email message records to display in the standard chatter publisher component like it normally would when a customer replies to an email sent from a case. Keep in mind, i am not looking for code to create a lead, just link an inbound email message to its existing lead.

   public static void parseEmailSubjectForLeadRelation(List<EmailMessage> emailList){
    system.debug('Entered parseEmailSubjectForLeadRelation');
    //List<EmailMessage> emList = new List<EmailMessage>();
    List<EmailMessageRelation> emrList = new List<EmailMessageRelation>();
    for(EmailMessage email : emailList) {
        string leadId;
        if(email.Incoming == true && email.ParentId != null && email.TextBody != null) {
            if(email.TextBody.contains('reference:') /*&& email.ToAddress == 'salessupport.eu@msts.com'*/){
                leadId = email.TextBody.substringBetween('reference:{', '}');
                //if(leadId != null) {
                    email.Status = null;
                    email.ParentId = null;
                    //email.RelatedToId = leadId;
                //}
            }
        }
    }
}

public static void parseEmailSubjectForLeadRelation0(List<EmailMessage> emailList){
    system.debug('Entered parseEmailSubjectForLeadRelation0');
    //List<EmailMessage> emList = new List<EmailMessage>();
    List<EmailMessageRelation> emrList = new List<EmailMessageRelation>();
    list<EmailMessage> emsToInsert = new list<EmailMessage>();
    list<EmailMessage> emsToUpdate0 = new list<EmailMessage>();
    list<Case> casesToDelete = new list<Case>();
    string leadId;
    for(EmailMessage email : emailList) {
        if(email.Incoming == true && email.ParentId != null && email.TextBody != null && email.CreatedByTrigger__c == false 
           && email.TextBody.contains('reference:') /*&& email.ToAddress == 'salessupport.eu@msts.com'*/) {
                case case0 = new case(id = email.ParentId);
                casesToDelete.add(case0);
                leadId = email.TextBody.substringBetween('reference:{', '}');
                EmailMessage emsg = new EmailMessage();

               emsg.ToAddress =  email.toAddress;
               emsg.FromAddress =  email.FromAddress;
               emsg.FromName = email.FromName;
               emsg.Subject = email.subject;
               emsg.status = '2';
               emsg.Incoming= True;
               emsg.TextBody = email.TextBody;
               System.debug('TextBody:>>>>> ' + email.TextBody);
               emsg.Lead__c = leadId;

                emsg.parentId = null;
                //emsg.Lead__c = leadId;
                emsg.CreatedByTrigger__c = true;
                emsToInsert.add(emsg);
        } else if(email.CreatedByTrigger__c == true) {
            leadId = email.TextBody.substringBetween('reference:{', '}');
            EmailMessageRelation emr = new EmailMessageRelation(
                EmailMessageId = email.id,
                RelationId = leadId,
                RelationType = 'FromAddress',
                RelationAddress = email.FromAddress
            );
            emrList.add(emr);
        }
    }
    if(emsToInsert != null && emsToInsert.size() > 0) {
        insert emsToInsert;
    }


    if(emrList != null && emrList.size() > 0) {
        system.debug('emrList == ' + emrList);
        insert emrList;
    }

    if(casesToDelete != null) {
        system.debug('casesToDelete == ' + casesToDelete);
        delete casesToDelete;
    }
}

I have the two methods above being called from the EmailMessage trigger, parseEmailSubjectForLeadRelation on beforeInsert and parseEmailSubjectForLeadRelation0 on afterInsert. Any help is greatly appreciated!

  • 1
    why not change the replyTo emailaddress to be something other than E2C and use an apex inbound email service to process? – cropredy Dec 5 '19 at 23:54
  • Cropredy could you go into a little more detail, or if you happen to have any sample code or an example i could see would be super helpful as well. I will continue to learn more about email services in the meantime and see if it sparks any ideas for me! – Logie C Dec 12 '19 at 21:15
  • Ok - i ended up creating a separate email service (not as bad as i thought) and disabled email-to-case for that address. – Logie C Dec 18 '19 at 5:10
0

The simplest solution is as follows:

  • Decouple the replyTo email address (e.g. foo@mycompany.com) from email-to-case
  • Create an Apex email service that you bind to foo@mycompany.com
  • Within the Apex email service, parse/use regex/inspect the email body for the filter key you need to look up the Lead. Then attach the email to that Lead and do whatever other processing you need.
|improve this answer|||||
  • Hi Cropredy, so you commented on my original question, i have made it through the portion of creating the email service, and have disabled email-to-case for this email address but i am getting stuck attempting to attach the email to the lead. Can you explain that or post a link to some documentation describing how to link an email message to the lead object? I have opened a new question with some more specific information – Logie C Dec 18 '19 at 21:41
  • see EmailMessage and EmailMessageRelation – cropredy Dec 18 '19 at 22:04
  • the lines i was missing are: emObj.ToIds = leadIdList; String[] toIds = new String[]{leadId}; emObj.toIds = toIds; – Logie C Dec 18 '19 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.