function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfadm sfadmsfadm sfadm 

How to extract attachments data in Lightning Experience?

I'm currently using the Document object/table in SOQL to extract attachments in Salesforce classic.
As far as I'm aware in Lightning it is necessary to use File instead of Document object to extract attachments.
In SOQL there is no such File table and I need to know how to extract attachments data in Lightning Experience?
Best Answer chosen by sfadm sfadm
Deepali KulshresthaDeepali Kulshrestha
Hi,

Please follow the given below point and link with the help of these you can solve your problem, it may be helpful to you.

1. To get a list of all files you can query ContentDocument object
[SELECT Id, Title, ContentAssetId FROM ContentDocument]

2. If the ContentDocument is an asset file, this field points to the asset. For most entities, the value of this field is null. You can query extract data using the following query (even assets):
[SELECT VersionData FROM ContentVersion WHERE ContentDocumentId = :myContentDocumentId AND IsLatest = true]

You can read more about that here: 
1. https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/api/sforce_api_objects_contentdocument.htm
2. https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm

If you need to get files that are attached to a specific record, you'd need to first query against ContentDocumentLink and filter upon LinkedEntityId with the recordId to get ContentDocumentIds. Example:

[SELECT Id, ContentDocumentId
FROM ContentDocumentLink WHERE LinkedEntityId = :recordId]

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha 

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi,

Please follow the given below point and link with the help of these you can solve your problem, it may be helpful to you.

1. To get a list of all files you can query ContentDocument object
[SELECT Id, Title, ContentAssetId FROM ContentDocument]

2. If the ContentDocument is an asset file, this field points to the asset. For most entities, the value of this field is null. You can query extract data using the following query (even assets):
[SELECT VersionData FROM ContentVersion WHERE ContentDocumentId = :myContentDocumentId AND IsLatest = true]

You can read more about that here: 
1. https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/api/sforce_api_objects_contentdocument.htm
2. https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm

If you need to get files that are attached to a specific record, you'd need to first query against ContentDocumentLink and filter upon LinkedEntityId with the recordId to get ContentDocumentIds. Example:

[SELECT Id, ContentDocumentId
FROM ContentDocumentLink WHERE LinkedEntityId = :recordId]

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi,

For extract attachments data in Lightning Experience go throug below link it may helpful for you.

https://salesforce.stackexchange.com/questions/270693/how-to-extract-attachments-data-in-lightning-experience

Thanks,
Ajay Dubedi