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
Raviraj S 1Raviraj S 1 

How to add image in chatter post

I want to add image in chatter post. I am trying the below example but the ContentData and ContentFileName fields are not present in FeedItem. API version 39.0. I also got similar post but I am facing the same issue.

Blob body=[Select Name, ContentType, Body From StaticResource where name='abc'].body;
FeedItem post = new FeedItem();
post.ParentId = u.id;
post.CreatedById='005E0000003XWRs';
post.Body ='Happy birthday '+u.name;
post.ContentData = body;
post.ContentFileName = 'sample.png';
insert post;

I also tried with below code, but here img tag is not working. it throws error as :- Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Referenced file id is invalid, not accessible, or does not exist: [RawBody]

FeedItem post = new FeedItem();
                post.ParentId = recipientId;
                post.CreatedById = loggedUser;
                post.IsRichText = true; 
                post.Body = message+'<p>&nbsp;&nbsp;</p><img src="/img/rypple/Thanks.png">'+badgeReceived.Description;
                post.RelatedRecordId = thanks.Id;
                insert post;