0

I have a custom object called "Research" that has a "Notes and Attachments" section where users are to upload documents that represent research for that record. For other objects in our system, this process created an Attachment record whose ParentID pointed to the record to which the document was attached. On my Research object, however, these uploaded documents are getting stored as a ContentDocument. I have two questions:

1) Why is there a difference here? 2) How can I find out how my ContentDocument relates to my Research record? The ParentId of the relevant ContentDocument is null.

1

Salesforce has two kinds of attached files: the classic Attachment and the new Content system.

The Notes and Attachments related list by default creates Content Files. These take the form of a ContentDocument record with one or more ContentVersion related records, which contain the actual document content. (You want the version with IsLatest set, most of the time).

Content Files are shared to record pages and other locations via the ContentDocumentLink junction object. You can query against that object with a filter on the LinkedEntityId field equal to your record Id to find its attached Files. Note that ContentDocumentLink has unusual query restrictions; you cannot query against it without a filter. The SOAP field reference for ContentDocumentLink has very useful details on how to work with this object.

One example of a query (adapted from the above reference) might look like

List<ContentDocument> linkedDocuments;

linkedDocuments = [SELECT Title 
                   FROM ContentDocument 
                   WHERE Id IN (SELECT ContentDocumentId 
                                FROM ContentDocumentLink 
                                WHERE LinkedEntityId = :researchId)]; 
// researchId is the Id of the Research object here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.