15

I am uploading a file via the Files related list on the Contact detail page. When I query (SOQL) the database I see the relevant ContentDocument and ContentVersion records. I would like to get the Contact record Id, with the SOQL query but I can't fine a ParentId field.

How to I get the Id of the record the file was uploaded under?

2
  • you should query attachment object: select parentId from Attachment; parentId represents your contact or opp-ty etc. Commented Jul 16, 2016 at 16:52
  • I tried query Attachment but I receive no results.
    – mascot
    Commented Jun 20, 2018 at 15:29

3 Answers 3

16

You need the LinkedEntityId on the ContentDocumentLink

1
11

Yes, the documents seem to be out of date?

There can be multiple entries for each ContentDocument. In the case of attachments (Lightning uses ContentDocument for attachments), there will be a record in the ContentDocumentLink for both the UserId and the record that is the Attachments Parent.

You can use a query similar to the below to find details of the attachments relating to an sObject. In this example a Case Id. *Note that the ContentDocumentLink object must be filtered with a where clause.

SELECT Id, LinkedEntityId, ContentDocumentId, Visibility, IsDeleted, ShareType,
ContentDocument.Title, ContentDocument.createdDate, ContentDocument.FileType
FROM ContentDocumentLink 
WHERE LinkedEntityId = '5000Y000999MQKTQA4'

enter image description here

3

I am having the same issue. LinkedEntityId seems to default to the userId of the person who uploaded the document.

The ParentId field on ContentDocument would be ideal, but even though it is in the API guide, it is not available in SOQL or even visible. The docs say regarding ParentId on ContentDocument:

ID of the library that owns the document. Created automatically when inserting a ContentVersion via the API for the first time.
This field is available in API version 24.0 and later when Salesforce CRM Content is enabled.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .