+ Start a Discussion
Abhilash Mishra 13Abhilash Mishra 13 

Does creating an EmailMessage Record using on case using  "APEX code" , also sends email

In standard salesforce layout, if you click on send email and save, it creates a EmailMessage record in the DB and also sends the email.

If we create EmailMessage record using apex code, will the email will be sent to email mentioned in toAddress field?
Raj VakatiRaj Vakati
Yes ...

Case cs = new Case();
            cs.Subject = email.subject;
            cs.contactId = conList[0].Id; 
            cs.Accountid = conList[0].AccountID;
            cs.Origin = 'E-Mail - FMB#1';
            cs.Status = 'New';
            cs.Description = email.plainTextBody;
            insert cs;

            EmailMessage[] newEmail = new EmailMessage[0];
            newEmail.add(new EmailMessage(FromAddress = email.fromAddress,
            FromName = email.fromName,
            ToAddress = email.toAddresses[0],
            Subject = email.subject,
            TextBody = email.plainTextBody,
            HtmlBody = email.htmlBody,
            ParentId = cs.Id)); 
            insert newEmail;