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
carlvcarlv 

linking comments to posts

Hi All,

 

Having created a post via apex, am trying to add a comment to that post - unsuccessfully.

 

Code is pulled straight from the cookbook examples -

 

FeedPost fpost = new FeedPost();
            fpost.ParentId = post.Post_Parent_record_Id__c; // target record to attach post to

            fpost.Body = post.Name;
            insert fpost;

 

The step above works fine. However if I try to use fpost.id as a link for the comment, I get an error stating fpost.id is not correct id type. See below -

 

FeedComment fcomment = new FeedComment();
                fcomment.FeedItemId = fpost.id; // should link to Post record from step 1 right?
                fcomment.CommentBody = comment.Name;
                insert fcomment;      

 

What am I doing wrong with the comment? I must be misunderstanding how comments relate to posts. Any assistance most appreciated.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
cloudcodercloudcoder

argh.... that code didnt format well. Let me try that again:

 

 




FeedPost fpost = new FeedPost();
fpost.ParentId = UserInfo.getUserId();
fpost.Body = 'hello there';
insert fpost;

List myfeed = [SELECT Id, Type,
CreatedById, CreatedBy.FirstName, CreatedBy.LastName, CreatedDate,
ParentId, Parent.Name,
FeedPostId, FeedPost.Body, FeedPost.Title, FeedPost.CreatedById,
(SELECT Id, FieldName, OldValue, NewValue
FROM FeedTrackedChanges ORDER BY Id DESC),
(SELECT Id, CommentBody, CreatedDate,
CreatedById, CreatedBy.FirstName, CreatedBy.LastName
FROM FeedComments ORDER BY CreatedDate, ID DESC LIMIT 10)
FROM NewsFeed
WHERE feedpostid = :fpost.id
ORDER BY CreatedDate DESC, ID DESC
LIMIT 1];

FeedComment fcomment = new FeedComment();
fcomment.FeedItemId = myfeed.get(0).id;
fcomment.CommentBody = 'my comment body';
insert fcomment;

All Answers

cloudcodercloudcoder

It's a little confusing, but the FeedComment requires a FeedItemId. The error you are receiving is because you are trying to pass in the id (not a feeditemid) of the FeedPost.

 

But, just passing in the feeditemid of your inserted FeedPost wont work. (You will get a required field missing error) You need to query the EntityFeed tables (such as NewsFeed) to get your FeedPost. Only then will you have all the data, and in the right format to add a comment to a post. So something like the following will work:

 

 

FeedPost fpost = new FeedPost(); fpost.ParentId = UserInfo.getUserId(); fpost.Body = 'hello there'; insert fpost; List<NewsFeed> myfeed = [SELECT Id, Type, CreatedById, CreatedBy.FirstName, CreatedBy.LastName, CreatedDate, ParentId, Parent.Name, FeedPostId, FeedPost.Body, FeedPost.Title, FeedPost.CreatedById, (SELECT Id, FieldName, OldValue, NewValue FROM FeedTrackedChanges ORDER BY Id DESC), (SELECT Id, CommentBody, CreatedDate, CreatedById, CreatedBy.FirstName, CreatedBy.LastName FROM FeedComments ORDER BY CreatedDate, ID DESC LIMIT 10) FROM NewsFeed WHERE feedpostid = :fpost.id ORDER BY CreatedDate DESC, ID DESC LIMIT 1]; FeedComment fcomment = new FeedComment(); fcomment.FeedItemId = myfeed.get(0).id; fcomment.CommentBody = 'my comment body'; insert fcomment;

 

 

 

cloudcodercloudcoder

argh.... that code didnt format well. Let me try that again:

 

 




FeedPost fpost = new FeedPost();
fpost.ParentId = UserInfo.getUserId();
fpost.Body = 'hello there';
insert fpost;

List myfeed = [SELECT Id, Type,
CreatedById, CreatedBy.FirstName, CreatedBy.LastName, CreatedDate,
ParentId, Parent.Name,
FeedPostId, FeedPost.Body, FeedPost.Title, FeedPost.CreatedById,
(SELECT Id, FieldName, OldValue, NewValue
FROM FeedTrackedChanges ORDER BY Id DESC),
(SELECT Id, CommentBody, CreatedDate,
CreatedById, CreatedBy.FirstName, CreatedBy.LastName
FROM FeedComments ORDER BY CreatedDate, ID DESC LIMIT 10)
FROM NewsFeed
WHERE feedpostid = :fpost.id
ORDER BY CreatedDate DESC, ID DESC
LIMIT 1];

FeedComment fcomment = new FeedComment();
fcomment.FeedItemId = myfeed.get(0).id;
fcomment.CommentBody = 'my comment body';
insert fcomment;

This was selected as the best answer
carlvcarlv

Thanks so much for your help! That got me where I needed to be.

 

For anyone else who uses the code in solution,  dont forget to use 'List<Newsfeed> myfeed' to make it work.