6

I am having this code:

        plainTextBody += 'Confirmed Late Delivery is changed to unticked \n';
        plainTextBody += 'Delivered Status is changed to a value that is not Delivered Late \n';
        plainTextBody += 'Late Code is changed \n';
        plainTextBody += 'or Late Code Subcategory is changed \n\n';

What I see from my received email is:

Confirmed Late Delivery is changed to unticked Delivered Status is changed to a value that is not Delivered Late Late Code is changed or Late Code Subcategory is changed 

Any reason why it is not changing lines as expected?

I tried \n\n which will change two lines then. A little weird...

3
  • are you using an email template or just sending apex outbound email (without a template)?
    – cropredy
    Aug 7 '14 at 17:10
  • I am doing it without template. Because the data needs to be based on a custom object and I am not sending to a contact so I can't use setWhatId()
    – Lance Shi
    Aug 7 '14 at 20:35
  • None of the answers provided here didn't worked for me, did you found whats wrong here?
    – Hasantha
    Dec 9 '15 at 2:00
3

Assuming you are using apex outboundEmail, I tried to replicate this using your string construction and my Util.sendEmail method (included):

String plainTextBody = '';
plainTextBody += 'Confirmed Late Delivery is changed to unticked \n';
plainTextBody += 'Delivered Status is changed to a value that is not Delivered Late \n';
plainTextBody += 'Late Code is changed \n';
plainTextBody += 'or Late Code Subcategory is changed \n\n';

Util.sendEmail(new List<String> {'foo@bar.com'}, new String[] {}, new String[] {}, 'sfse email line break' , plainTextBody);



  //  ---------------------------------------------------------------------
  //  sendEmail (to, cc, bcc)
  //  ---------------------------------------------------------------------    
public static void sendEmail (String[] toRecipients, String[] ccRecipients, 
                             String[] bccRecipients, String subject, String body) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
try {
    Messaging.reserveSingleEmailCapacity(1);
    email.setToAddresses(toRecipients);
    email.setCcAddresses(ccRecipients);
    email.setbccAddresses(bccRecipients);
    email.setSubject(subject);
    email.setPlainTextBody(body);

    System.debug(LoggingLevel.INFO,'** entered sendEmail, to:' + toRecipients + ' cc:' + ccRecipients + ' bcc:' + bccRecipients + ' subject:' + subject + ' body:' + body);

    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    return;
}
catch (LimitException e) {throw new MyException('[UTIL-05] sendEmail error. No capacity to send email: ' + subject + '\n' + e.getMessage());}
catch (Exception e) {throw new MyException('[UTIL-04] sendEmail error. ' + e.getMessage());}   

}

and in three different email clients - GMail, Yahoo Mail, Outlook 2013, I always get the body of the email, once I open the email (not the preview pane) to show four lines as in:

Me
To Me
Today at 3:12 PM

Confirmed Late Delivery is changed to unticked 
Delivered Status is changed to a value that is not Delivered Late 
Late Code is changed 
or Late Code Subcategory is changed 
7
  • Interesting... I am using gmail. And I tried it on my developer org as well which can still reproduce my issue... I will retry. Thanks a lot.
    – Lance Shi
    Aug 8 '14 at 6:58
  • FWIW, I used a V29 Eclipse execute anonymous
    – cropredy
    Aug 8 '14 at 15:37
  • Thank you for that. I have retried - it is my outlook setting, it removed the extra lines as it 'thinks'. There is no issue with the code though. Thank you for the help.
    – Lance Shi
    Aug 9 '14 at 0:10
  • just curious -- what outlook setting did you change from what value x to value y to resolve this?
    – cropredy
    Aug 9 '14 at 17:45
  • I didn't change anything - I just clicked the banner.
    – Lance Shi
    Aug 10 '14 at 10:24
0

Instead of assigning the content directly to plainTextBody, assign it to a string variable.

    String txt = 'Confirmed Late Delivery is changed to unticked \n';
    txt += 'Delivered Status is changed to a value that is not Delivered Late \n';

Then, assign it to your plainTextBody as email.setPlainTextBody( txt );

0

EDIT

Since what I originally wrote below didn't compile, another option you'd have beyond what Crop wrote in his response would be to define an array of type string then join it at the time you were finished adding lines as below:

 list<string>txtbody = new list<string>();

 txtbody[0] = 'Confirmed Late Delivery is changed to unticked';
 txtbody[1] = 'Delivered Status is changed to a value that is not Delivered Late';
 txtbody[2] = 'Late Code is changed';
 txtbody[3] = 'or Late Code Subcategory is changed \n';

 String plaintxtbody = String.join(txtbody, '/n'); 
 // this joins all of the list txtbody into a single string with each record separated by a newline character. 

Now try using either your or @crop's code to send your email message.

END EDIT

Try coding your lines as below:

plainTextBody += 'Confirmed Late Delivery is changed to unticked' + \n;

Otherwise you'll need to escape the newline character like this: \\n if contained within the single quotes in your original code above.

3
  • That won't compile.
    – Keith C
    Aug 7 '14 at 16:32
  • Didn't have time to look at this again yesterday, but I see you've provided a more complete response where you've assured plainTextBody was defined as a string. The only string method that escapes a newline character is escapeCsv, but it returns it wrapped in double quotes.
    – crmprogdev
    Aug 8 '14 at 14:39
  • Oops, meant to add that you've also included helpful code for sending the email.
    – crmprogdev
    Aug 8 '14 at 14:46
0

You can insert a break into plaintext emails using {!BR()}.

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.