Skip to main content
Ajanth R asked in #Apex
I have to retrieve all the attachments of a custom object (of all records) and list them out with thumbnail and preview option. 

For the preview option, I need the ContentDocument id and for the thumbnail, I need ContentVesrsion Id. 

I need Record Id, Record Title, Attachment Id, ContentDocument Id, ContentVersion Id in the rows of the list.

I tried    

select Id, Title__c, 

    (SELECT Id, Name FROM Attachments)

    from My_Object__c

But how can I get the ContentDocument Id and ContentVersion Id using the query? I am not sure how to proceed. Can I try something like this

select Id, Title__c, 

    (SELECT ContentDocumentId FROM ContentDocumentLink),

    (select id from contentversion where contentdocumentid=ContentDocumentId  and isLatest=true)

    from My_Object__c

I know the above query is wrong and I don't see how to use the ContentDocumentId in the where condition of ContentVersion query. I am confused. Please help. 

 
2 answers
  1. Apr 7, 2017, 12:21 PM

    Hi,

    Select id, ContentDocumentId, ContentDocument.LatestPublishedVersionId from ContentDocumentLink where LinkedEntityId IN (some list of obj Ids) gets you all ContentDocuments and the latest version Id

    Thanks,

    Nagendra.

     
0/9000