[SalesForce] How to delete ContentDocument records associated with Agreements record

As parentId in ContentDocument records is null, i am unable to locate and delete the records associated with Aggreements records. Is there any way to relate records between these two objects without parentId field.

Best Answer

ContentDocument records are linked to their parents or share locations (of which there may be more than one) using ContentDocumentLink records. The Id of the parent record is stored in the LinkedEntityId field on ContentDocumentLink.

I suggest reading through the linked document as you try to design your queries, because there are some unusual restrictions on how you query ContentDocumentLink. In particular,

A SOQL query must filter on one of Id, ContentDocumentId, or LinkedEntityId.

and

You can't filter on the related object fields. For example, you can't filter on the properties of the account to which a file is linked. You can filter on the properties of the file, such as the title field.