You need to sign in to do that
Don't have an account?
Ivo 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
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
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
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.
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.
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
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.
Thank you for your response.
If possible could you please share your trigger written on ContentDocument plz