I have a custom object named 'comments' with which i made a 'action'.it is having a trigger handler class
public void onAfterInsert(Comments__c[] newComments){
if(newComments.size()>0){
for(Comments__c comRec:newComments){
FeedItem post = new FeedItem();
post.ParentId = comRec.case__c;
post.IsRichText = true;
post.body = comRec.Case_Comments__c;
feedItemInsertList.add(post);
try{
if(feedItemInsertList.size()>0){
insert post;
}
}
catch(Exception e){
system.debug(e.getMessage());
}
}
the 'case comments' field is a rich text but when i add a image to it in case feed it is creating a comment but I was not able to view the image in feed but in detail page the image is available.
It was throwing an error
System.DmlException: 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]
Best Answer
In case if image has been added to Rich Text Area field, its body will be stored as ContentReference record. If you look at rich field, format will be like:
Where src attribute contains link to image.
We can define PageReference and get its content by:
Then insert ContentVersion record:
As you can see there is constant name, reason of that - ContentReference doesn't have name, I think using attachment will be better option
And create Feed:
One more thing I want to mention here is - it's not allowed to insert ContentReference and Feed in single context, queueable can be used here.
Assuming everything above, we can implement following triggerHandler method:
Queueable class: