You need to sign in to do that
Don't have an account?

InboundEmailHandler toAddress list
I'm creating email to custom object records via the InboundEmailHandler. (sorry if my verbage is wrong on that).
What I have working is the email to custom object, as well as the attachments to the record, but having issues creating a new EmailMessage that is associated with the custom object record.
I have all the fields figured out minus the toAddress since it's inserting via a list to string. I'm not sure how to convert that to a list. Any help would be appreciated. Here is the code:
global class CreateTradeSupport implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env){ Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); String myPlainText= ''; Contact c; myPlainText = email.plainTextBody; try{ if([SELECT count() from Contact where email =:email.fromAddress] == 0){ c = new Contact(); c.LastName = email.fromName; c.Email = email.fromAddress; insert c; } else{ c = [SELECT Id from Contact Where email =:email.fromAddress LIMIT 1]; } Trade_Support__c ts = new Trade_Support__c(); ts.Description__c = myPlainText; ts.Status__c = 'New'; ts.Subject__c = email.subject; ts.web_email__c = email.fromAddress; ts.contact__c = c.Id; insert ts; EmailMessage mail = new EmailMessage(); mail.Subject = email.subject; mail.FromAddress = email.fromAddress; mail.HtmlBody = email.htmlBody; mail.TextBody = myPlainText; mail.FromName = email.fromName; mail.RelatedToId = ts.Id; //This is where I'm having issues mail.toAddress = email.toAddresses; mail.CcAddress = email.ccAddresses; mail.Status = 'Read'; insert mail; // Save attachments, if any List<Attachment> attachments = new List<Attachment>(); if(email.textAttachments != null) { for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) { Attachment attachment = new Attachment(); attachment.Name = tAttachment.fileName; attachment.Body = Blob.valueOf(tAttachment.body); attachment.ParentId = ts.Id; attachments.add(attachment); } } if(email.binaryAttachments != null) { for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) { Attachment attachment = new Attachment(); attachment.Name = bAttachment.fileName; attachment.Body = bAttachment.body; attachment.ParentId = ts.Id; attachments.add(attachment); } } if(attachments.size() > 0) { insert attachments; } } catch (QueryException e) { System.debug('Query Issue: ' + e); } result.success = true; return result; } }
Please try below code . I have added below lines in the code:
//This is where I'm having issues
List<String> toAddressesList = New List<String>();
toAddressesList.AddAll(email.toAddresses);
mail.toAddress = toAddressesList;
Please Mark as Best answer if helped.
Regards,
Ramakant
If my understanding is correct. You are struggling in the below lines:
mail.toAddress = email.toAddresses;
mail.CcAddress = email.ccAddresses;
In Email Message Object toAddress , CcAddress are String.
https://developer.salesforce.com/docs/atlas.en-us.sfFieldRef.meta/sfFieldRef/salesforce_field_reference_EmailMessage.htm
But In InboundEmail Class toAddresses, ccAddresses are array.
So to convert this properly you can use the below function :
String toAdd = String.join(email.toAddresses , ';');
String ccAdd = String.join(email.ccAddresses , ';');
mail.toAddress = toAdd;
mail.CcAddress = ccAdd;
Thanks,
Maharajan.C