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
Raj R.Raj R. 

How to add line breaks in emails sent from salesforce in apex?

Hi,
I have some debug statements and checks that will send an email out when an Apex class fails on a DML operation (insert, update, delete). How can I create a string variable or array (whatever is most appropriate) that when the email is sent and opened in a Email Client (i.e. Outlook) there are line breaks where I want them to be?

Unsuccessful code I have tried:

try {
         insert list;
} Catch(DMLExceptiion dml) {
         String msg = '';
          for(Object ob : list){
                msg = msg + ob.Name + '\r\n';
          } 
          //at the end of the for loop, the msg variable has a list of all names of the object where there is a new line break after each name

        //structure of sendEmail method is SendEmail(Subject, Body);

        Email.SendEmail ('Test', msg);
}

------------------------------------------------------------------------
try {
         insert list;
} Catch(DMLExceptiion dml) {
         String msg = '';
          for(Object ob : list){
                msg = msg + ob.Name + BR();
          } 
          //at the end of the for loop, the msg variable has a list of all names of the object where there is a new line break after each name

        //structure of sendEmail method is SendEmail(Subject, Body);

        Email.SendEmail ('Test', msg);
}

Best Answer chosen by Raj R.
AshwaniAshwani
With setHTMLBody() it can be acheived just use it like:

try {
         insert list;
} Catch(DMLExceptiion dml) {
         String msg = '';
          for(Object ob : list){
                msg = msg + ob.Name + '<br/>';
          } 
          //at the end of the for loop, the msg variable has a list of all names of the object where there is a new line break after each name

        //structure of sendEmail method is SendEmail(Subject, Body);

        Email.SendEmail ('Test', msg);
}

------------------------------------------------------------------------
try {
         insert list;
} Catch(DMLExceptiion dml) {
         String msg = '';
          for(Object ob : list){
                msg = msg + ob.Name + '<br/>';
          } 
          //at the end of the for loop, the msg variable has a list of all names of the object where there is a new line break after each name

        //structure of sendEmail method is SendEmail(Subject, Body);

        Email.SendEmail ('Test', msg);
}


All Answers

AshlekhAshlekh
Hi, 

You can use htmlBody() method of SingleEmailMessage class. By this mehtod you can pass html content in htmlbody method.
Ravikant kediaRavikant kedia

SingleEmailMessage class contain method htmlBody() this method will solve your problem

Avidev9Avidev9
Can you share the structure of your "SendEmail" method ?
I think "\n" should work for you, I remember using the same earlier with the setPlainTextBody()
AshwaniAshwani
With setHTMLBody() it can be acheived just use it like:

try {
         insert list;
} Catch(DMLExceptiion dml) {
         String msg = '';
          for(Object ob : list){
                msg = msg + ob.Name + '<br/>';
          } 
          //at the end of the for loop, the msg variable has a list of all names of the object where there is a new line break after each name

        //structure of sendEmail method is SendEmail(Subject, Body);

        Email.SendEmail ('Test', msg);
}

------------------------------------------------------------------------
try {
         insert list;
} Catch(DMLExceptiion dml) {
         String msg = '';
          for(Object ob : list){
                msg = msg + ob.Name + '<br/>';
          } 
          //at the end of the for loop, the msg variable has a list of all names of the object where there is a new line break after each name

        //structure of sendEmail method is SendEmail(Subject, Body);

        Email.SendEmail ('Test', msg);
}


This was selected as the best answer
SamHowleSamHowle
Thanks Ashwani! I had trouble with this as well but the '<br/>' tag did the trick and was easy to add right into the my string for my htmlbody() method