How to query a file uploaded as an attachment in lightning experience?
Files are stored in the ContentDocument Object .You will have to run a SOQL against contentDocument link first and look for all Content Version DocumentLink with EntityId as the ParentId of the record .
SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = '[RECORD ID]'
Then SOQL on ContentDocument
Select Id ,Title from ContentDocument Where ID In :[CONTENTDOCUMENTLIST]
In lightning when you upload attachments, it save as salesforce files. for querying salesforce files you need to understand three object:
- ContentVersion
- ContentDocumentLink
- ContentDocument
for each files uploaded in lightning, there will be only one contentDocument will get created. Contentversion depends upon you, default it will get create as version 1, later you can create n number of contentVersion. If you are uploading the document on a record, there will be two contentDocumentLink will get created. One will be related to you and other will be related to that particular record. later if you share the file with any other record or user, for each one, a contentDocumentlink will get created.
You can query all the latest contentversion as:
[select id,FileType, Title,FileExtension, Versiondata from ContentVersion where islatest=true ];
for querying the no. of files related to a particular record:
[SELECT Id, LinkedEntityId, ContentDocumentId,Visibility, IsDeleted, ShareType,ContentDocument.Title,ContentDocument.createdDate, ContentDocument.FileType FROM ContentDocumentLink WHERE LinkedEntityId =:parentIds and ContentDocument.FileType!= 'SNOTE'];
ContentDocument.FileType!= 'SNOTE'
this will help you to eliminate the contentNote .
for querying ContentDocument directly:
[Select Id,FileExtension,SharingOption,Title from ContentDocument ];