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!
replyTo
emailaddress to be something other than E2C and use an apex inbound email service to process? – cropredy Dec 5 '19 at 23:54