function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
oleg.tuchschereroleg.tuchscherer 

Creating HTML Email by Apex

Hi,

I have trouble with creating HTML formatted EmailMessages by APEX.

Problem:

EmailMessage is created, but Salesforce does not display the label "HTML Body" in layout.

 

Example:
            sSubject = 'Some text';
            sBody = '<html><body><p>here comes the body</p></body></html>';      
           
            // Create an Email Message
            EmailMessage objEmailMessage = new EmailMessage(ParentID = objCase.ID,
                                                            MessageDate = datetime.now(),
                                                            Subject = sSubject,  
                                                            ToAddress = sToMail,
                                                            CcAddress = sToMailCC,
                                                            BCcAddress = sToMailBCC,
                                                            HtmlBody = sBody,
                                                            FromName = Userinfo.getName());
            objEmailMessage.FromAddress = Userinfo.getUserEmail();            
            
            insert objEmailMessage;      

 

Result:

I see the email message in Salesforce. Open this mesaage. Just "Text Body" is visible.

 

Do I miss anything? I need "HTML Body" to be visible.

Do you possibly have a soluton for me?

Thanks a lot!

Rahul_sgRahul_sg

remove HtmlBody .. try somthign like this

 

  Messaging.SingleEmailMessage objEmailMessage= new Messaging.SingleEmailMessage();     

 

use   objEmailMessage.setHtmlBody(sbody);    

oleg.tuchschereroleg.tuchscherer

Not possible. Got error message:
Save error: Invalid field Body for SObject EmailMessage

Rahul_sgRahul_sg

sorry i posted wrong message plz see my updated post

 

oleg.tuchschereroleg.tuchscherer

No, it does not function.

In 1 step I send an email using "SingleEmailMessage" -> no problems. User sees html body.

In 2 step I like to have the sent email in Salesforce, hence i used "EmailMessage" object.

Your solution regards 1.

I need a solution for step 2.

Rahul_sgRahul_sg

I think sfdc is showing the body in TEXT format. just pus some system.debug to verify if the html tags are still there

Also, try this body   sBody = '<html><body><p>here <B>comes</B> the body</p></body></html>';      

oleg.tuchschereroleg.tuchscherer

I think, you are right. Just the Text Content is displayed. There is no possibility to show HTML Body by default.

In my case the Text Body is empty, so the only solution would be to create a new VF page with HTML Body instead of Text Body.