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
Nelson DavenapalliNelson Davenapalli 

Could not access attchment referred in cases using SOQL

First and foremost, i am a newbie with SF so pls bear with me and beg your pardon, if I dont use any adequate terms
Problem statement : Want to access files attached to Case objects using the SFDC rest api or by executing SOQL query. 

The following is what i have tried so far but no sucess.
I am using a SF dev account. A workflow has been set to read email with invoices and convert into case  with attachments. 
However, when I ran a SOQL query from the dev console,
Select Id from Attachment

I dont see any attachments info. Moreover, I dont see any object called Attachment in ObjectManager. I also see that the attachments are visible under Files  but dont see a way to use SOQL Query api to fetch those. 
So, can anybody provide any info as to how to fetch attchments referred in cases programatically in Java.

 
Best Answer chosen by Nelson Davenapalli
Ramesh DRamesh D
Hi @Nelson,
Lightning:
In lightning when you upload attachments, it save as salesforce files. for querying salesforce files you need to understand three object:
  1. ContentVersion
  2. ContentDocumentLink
  3. ContentDocument
Refer this https://salesforce.stackexchange.com/questions/138860/how-to-query-a-file-uploaded-as-an-attachment-in-lightning-experience

Classic Attachments:
SELECT Id FROM Attachment WHERE ParentId = 'record id'

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D

All Answers

Ramesh DRamesh D
Hi @Nelson,
Lightning:
In lightning when you upload attachments, it save as salesforce files. for querying salesforce files you need to understand three object:
  1. ContentVersion
  2. ContentDocumentLink
  3. ContentDocument
Refer this https://salesforce.stackexchange.com/questions/138860/how-to-query-a-file-uploaded-as-an-attachment-in-lightning-experience

Classic Attachments:
SELECT Id FROM Attachment WHERE ParentId = 'record id'

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
This was selected as the best answer
Nelson DavenapalliNelson Davenapalli
Yes, indeed, I was referring to the old way of reading attachments. I stumbled upon the same that i need to refer 3 objects to get the rest url to read the file content. Interestingly the  LinkedEntityId with the  pdf file is of the email message which had the original file and not the case. Came as a surprise but not now,  as I was looking for 1-1 correlation between a case and a file, whereas in this case the linkedEntityId  was an email message as the email message was used to create the case. Thanks for your answer
Deepali KulshresthaDeepali Kulshrestha
Hi Nelson,

You can try this out
SELECT ContentDocumentId, LinkedEntityId  FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM Case ) and LinkedEntity.Type='Case

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