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
prakash_sfdcprakash_sfdc 

Unable to post multi line comment on Chatter FeedItem using Chatter REST API using Apex

I am trying to post a simple text comment to FeedItem. The comment has line breaks.

Following is my code:
 
String comment = 'Test1\r\n\r\nTest2';

String access_token = AuthController.getJIRAOAuthToken();

String authVal = 'OAuth '+access_token;
HttpRequest req = new HttpRequest();
String endpoint = system.label.JIRA_OAuth_SFDC_REST_API_Endpoint+'/services/data/v35.0/chatter/feed-elements/';


System.debug(endpoint); 
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setHeader('Content-Type','application/json; charset=UTF-8');

req.setHeader('Authorization', authVal);




req.setBody('{"body":{"messageSegments":[{"type":"Text","text":"'+comment+'"}]},"feedElementType":"FeedItem","subjectId":"a3gm0000000066S"}');


Http http = new Http();
//System.debug(req);
HTTPResponse res = http.send(req);
System.debug('**'+res);

I am getting below response.
System.HttpResponse[Status=Bad Request, StatusCode=400]

If I am trying the same code without "\r\n" then it works perfectly fine. Any suggestions please ?

I tried replacing "\r\n" with " " as per the rich text feed item https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/quickreference_post_feed_element_rich_text.htm but even that didn't help. The comment is posted along with " " in text.
 
Arun KumarArun Kumar
Hi,

Make a Json string like below
{
   "body":{
      "messageSegments":[
         {
             "text" : "First line of text.",
             "type" : "Text"
         },         
         {
            "text" : "Second line of text.",
            "type" : "Text"
         }         
      ]
   },
   "subjectId":"me",
   "feedElementType":"FeedItem"
}
Please let me know if the helps.

As a common practice, if your question is answered, please choose 1 best answer.
Additionaly you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Arun