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
Ivo Rocha 19Ivo Rocha 19 

How to get the FeedItem Id when a ContentDocument is deleted

Hi, I'm facing a problem when deleting documents uploaded via Chatter.

So I have a numeric field called "TotalNumberOfDocuments__c" on Opportunity level that will increase/decrease everytime files are uploaded or deleted.

These are the cases:
- Attachments can be uploaded via Notes & Attachments related list (so far so good)
- Attachments can be deleted via Notes & Attachments related list (so far so good)
- Files can be posted on the opportunity feed (so far so good)
- Files can be deleted when feed post is deleted (so far so good)
- Files can be deleted via Notes & Attachments (outch)

The problem is that when a file is uploaded via Chatter and later is deleted on the Notes & Attachments related list (in this case a ContentDocument is deleted, but the FeedItem is kept). I can't find a way to get the opportunity which the file is related to.
Is there a way to get the FeedItem Id from the ContentDocument object that was delete? Via trigger after delete on the SObject ContentDocument?

Thanks
Best Answer chosen by Ivo Rocha 19
Neil HayekNeil Hayek
To solve one issue you're facing, if the ContentDocument is deleted, you should be able to have a trigger before delete of ContentDocument, and in the trigger, you query ContentDocumentLink.  This will tell you all records the file is shared with (it can be multiple).  For each of those records, you can decrement the count.

If, however, the user clicks the delete link in the notes and attachments related list for a ContentDocument, it does not delete the file, it unshares the file with the record (the file can become private to the owner if it isn't shared elsewhere).  In this case, since the document isn't deleted, the trigger won't fire.

I currently can't think of a way you can handle that case.  One possibility is in your other triggers, such as feed item delete, you could do a query like [SELECT count() FROM ContentDocumentLink where LinkedEntityId = '<recordId>'].  This will give you how many ContentDocuments are shared with that record, and you can see if it matches your count (or update the count accordingly).  This would at least clean up the count after certain conditions.

All Answers

Neil HayekNeil Hayek
To solve one issue you're facing, if the ContentDocument is deleted, you should be able to have a trigger before delete of ContentDocument, and in the trigger, you query ContentDocumentLink.  This will tell you all records the file is shared with (it can be multiple).  For each of those records, you can decrement the count.

If, however, the user clicks the delete link in the notes and attachments related list for a ContentDocument, it does not delete the file, it unshares the file with the record (the file can become private to the owner if it isn't shared elsewhere).  In this case, since the document isn't deleted, the trigger won't fire.

I currently can't think of a way you can handle that case.  One possibility is in your other triggers, such as feed item delete, you could do a query like [SELECT count() FROM ContentDocumentLink where LinkedEntityId = '<recordId>'].  This will give you how many ContentDocuments are shared with that record, and you can see if it matches your count (or update the count accordingly).  This would at least clean up the count after certain conditions.
This was selected as the best answer
Ivo Rocha 19Ivo Rocha 19
Thanks Neil Hayek. I was able to count down the field using a trigger before delete on ContentDocument by quering the ContentDocumentLink object. Actually when the document is deleted via related list "Notes & Attachments" this trigger is triggered.

I also implemented a trigger after undelete on ContentDocument to count it up again if for some reason the user recovers the record via the Recicle bin.

Many thanks again for your input.
Neil HayekNeil Hayek
Ah, the content document is deleted when you click delete from the related list.  I was thinking it's just an unlink.  I'm glad the approach works for you.
Kavitha J SubramanyaKavitha J Subramanya
Hello Neil Hayek,

I'm facing similar issue but when i delete file from communities.
Files is a related list to my custom object and the requirement is to display files count on one of the field in record.   
I have writtten a trigger on ContentDocumnetLink for after insert,after update and aftetr delete events. 
The trigger is firing when file is added and count is updated. this is working fine from both classic(org) and communities
The trigger is not fired when the file is deleted from record from the communities. However, if I delete the file from record from the classic as a system admin it is working fine. 

I tried the approach suggested by you,  writing another trigger on FeedItem to update the count when file is deleted from the related list(FILES). but my trigger is not getting fired when i delete file.

If you have a trigger written for it could you share here
Ivo Rocha 19Ivo Rocha 19
Hi Kavitha J Subramanya,

Did you try to create a trigger on ContentDocument before delete? And then to get the related record id, you'll need to query ContentDocumentLink object like this "Select Id, LinkedEntityId From ContentDocumentLink Where ContentDocumentId = :contentDocumentRecordDeleted.Id". 
(Note: it must be done on before delete trigger, otherwise you'll lose the ContentDocumentLink record)

"LinkedEntityId" is the record id of your custom object (or any other object), so to make sure it is related to your custom object, you need to check to which object this Id belongs to. You can make do something like:
if(contentDocumentLinkRecord.linkedEntityId.getSObjectType().getDescribe().getName() == "YOUR_CUSTOM_OBJECT_NAME")
   then decrement the rollUpField on the linkedEntityId record by one


The trigger objects I've used when I implemented this were:
AttachmentAfterDelete
AttachmentAfterInsert
AttachmentAfterUndelete
ContentDocumentAfterUndelete
ContentDocumentBeforeDelete
FeedItemAfterDelete
FeedItemAfterInsert

Hope it helps you.
Kavitha J SubramanyaKavitha J Subramanya
Hi Ivo Rocha,

Thank you for your response.
If possible could you please share your trigger written on ContentDocument plz 
Kavitha J SubramanyaKavitha J Subramanya
Hi Ivo Rocha, I was able to follow the approach and  It is working :). Thank you.