I'm creating an email to custom object class which creates a record on the "Trade Support" object along with the emails attachment(s) if any and wanting to attach the email message to the record as well via the EmailMessage object.

I have everything working minus the EmailMessage portion of this. When I try to list the toAddress, I'm getting an error of

"Illegal assignment: from list to string"

I'm having a hard time figuring out how to create a list and attach that list to the "toAddress" field of the emailMessage that attaches to the case.

Here is my 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;

            if([SELECT count() from Contact where email =:email.fromAddress] == 0){
                c = new Contact();
                c.LastName = email.fromName;
                c.Email = email.fromAddress;
                insert c; 
                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 have the issue
            //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;
                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;
                if(attachments.size() > 0)
                    insert attachments;

        catch (QueryException e) {
            System.debug('Query Issue:  ' + e);

        result.success = true;

        return result;
  • Next time just post the code that's relevant to your question. Scrolling through the whole file takes time :-) May 1 '19 at 14:34
  • My apologies. Didn't think that one through before posting.
    – tDactyl
    May 1 '19 at 14:39
  • Never, ever catch an exception if you are going to take no subsequent action (and no, debugging doesn't count as an action).
    – Adrian Larson
    May 1 '19 at 15:20

toAddresses is a List<String>, so you need to join the values together:

mail.toAddress = String.join(email.toAddresses, ',');

You'll want to check the CC Address list to make sure it's not null before attempting to call join on it.

  • Did you intend String.join(email.toAddresses, ',') instead?
    – Jayant Das
    May 1 '19 at 14:39
  • @JayantDas Yeah... Let's say I've been writing too much JS lately.
    – sfdcfox
    May 1 '19 at 15:03

The error on the line below is because EmailMessage.toAddress is of type String and that Messaging.InboundEmail.toAddresses returns a String[]

mail.toAddress = email.toAddresses; // results in illegal assignment that you are facing

To fix this you will need to extract the String value from the email.toAddresses and assign accordingly. Something as:

mail.toAddress = String.join(email.toAddresses, ',');

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.