0

I am using the following SOQL query to get the documents attached to an account:

select LinkedEntityId    
, ContentDocument.Title
, ContentDocument.ContentSize
,ContentDocument.Description
, ContentDocument.FileExtension
, ContentDocument.FileType
, ContentDocument.Id 
, ContentDocument.ContentAssetId
, ContentDocument.LatestPublishedVersion.VersionData
, ContentDocument.LatestPublishedVersion.PathOnClient
,ContentDocument.CreatedBy.Name
  FROM ContentDocumentLink
WHERE LinkedEntityId in (SELECT Id from Account where id='0014W00002nVX2KQAW')

I need to include the account Name in the result. How can I join to the account table? Thanks in advance.

1 Answer 1

1

You should make use of the TYPEOF clause. Given that the LinkedEntityId is a polymorphic field you need to specify the fields you want to retrieve depending on the LinkedEntity sObjectType.

For your particular scenario, in which you want to retrieve the Name of the Account, your query should be updated to:

[
SELECT 
TYPEOF LinkedEntity WHEN Account THEN Name END,
LinkedEntityId, 
ContentDocument.Title,
ContentDocument.ContentSize,
ContentDocument.Description,
ContentDocument.FileExtension,
ContentDocument.FileType,
ContentDocument.Id,
ContentDocument.ContentAssetId,
ContentDocument.LatestPublishedVersion.VersionData,
ContentDocument.LatestPublishedVersion.PathOnClient,
ContentDocument.CreatedBy.Name
FROM 
ContentDocumentLink
WHERE 
LinkedEntityId IN 
(SELECT Id from Account where id='0014W00002nVX2KQAW')
]

Best regards!

You must log in to answer this question.

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