\n is not showing newline in chatter feed

This has to do with the Rich Text feature that was introduced in v35.0 of the API. As you saw, if there are no markup segments, the post is treated as plain-text and the newlines are preserved. If there are markup segments, you need to do the following to achieve a newline:

  • Create a ConnectApi.MarkupBeginSegmentInput of type Paragraph
  • Create a ConnectApi.TextSegmentInput with text = '& nbsp;' (with no space between the & and n -- I had to add a space in order to get it to display properly here)
  • Create a ConnectApi.MarkupEndSegmentInput of type Paragraph

In your example, you'd want to put the three segments inputs before and after textInput3 in the list of input.body.messageSegments to see some newlines.

It's the Apex equivalent of this REST example.

Other options are:

  • Compile the triggers using API v34.0 (I haven't tried this, but I think it will work since the rich text feature was introduced in v35.0). You'll have to make sure you use the appropriate version of ConnectApiHelper.
  • Disable the rich text feature in your org.

Tags:

Chatter