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
Sean Williams 21Sean Williams 21 

trigger not firing for cascade delete (feedcomment)

We have a legal requirement to backup copies of some users chatter posts when those posts are deleted.  I have created a new trigger to write out feeditem and feedcomment objects to a custom object when they are deleted, and its working well.  It runs after delete on FEEDITEM (a chatter post basically) and FEEDCOMMENT (chatter comment on a chatter post).

Chatter post = FEEDITEM record (parent)
  > Chatter comment = FEEDCOMMENT record (child)

All works as expected, but when a chatter post is deleted in the UI (and thus the comments are deleted via cascade delete) the trigger on FEEDCOMMENT does not fire.  This appears to be documented, but is a major limitation for me.  

Can anyone suggest a workaround?
 
Best Answer chosen by James Loghry
James LoghryJames Loghry
As you've found out, triggers don't fire on children records when they are cascade deleted when the parent is deleted.  In order to support this, you will need to write another, before delete trigger on the parent record (in this case FeedItem) that "manually" deletes the FeedComments first, which will in turn fire the trigger on the FeedComment record(s)

All Answers

James LoghryJames Loghry
As you've found out, triggers don't fire on children records when they are cascade deleted when the parent is deleted.  In order to support this, you will need to write another, before delete trigger on the parent record (in this case FeedItem) that "manually" deletes the FeedComments first, which will in turn fire the trigger on the FeedComment record(s)
This was selected as the best answer
Sean Williams 21Sean Williams 21
Thanks James.  I arrived at a similar answer, just finished coding and it seems to work.  What i did was to write the comment objects to my backup object, rather than deleting them, which has the same effect.  I did this as u suggest in the before delete on the parent.  

Thanks for the suggestion though - hadnt considered doing the delete myself!
Animesh DattaAnimesh Datta

Hi,

We have same requirement to take backup of deleted feedpost and its comments. As casedelete does not work, we are deleting feedcomments using feeditem trigger. It works only for system admin, but does not work for other users as they do not have "View All Data" permission (Only Admin or users with View All Data permission can query on feedcomment). So is there any way to avoid querying feedcomment while feeditem is fired.

Regards,
Animesh

Animesh DattaAnimesh Datta
reason of this my code is running in With Sharing. Please let me know if anyone has solution