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
Gabriel Costa 13Gabriel Costa 13 

Get Images and Media related to an object SOQL

Hi,

I have a 'Property' object that has some images and other media attached to it. How can I get the images/files associated with this object in SOQL?

This is the section where the files are stored

User-added image
Best Answer chosen by Gabriel Costa 13
SwethaSwetha (Salesforce Developers) 
HI Gabriel,

A similar ask was posted here https://salesforce.stackexchange.com/questions/22978/soql-query-for-retrieving-images-from-notes-and-attachments
 
SELECT Id,Name FROM Attachments where ParentId ='id of the record for you want to query related attachments for' AND ContentType ='type of the attachment you want to retrive'

Example:
SELECT Id,Name,ContentType FROM Attachment where ParentId ='0019000000CHVUl' AND contenttype in ('image/png','image/gif')



If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Gabriel,

A similar ask was posted here https://salesforce.stackexchange.com/questions/22978/soql-query-for-retrieving-images-from-notes-and-attachments
 
SELECT Id,Name FROM Attachments where ParentId ='id of the record for you want to query related attachments for' AND ContentType ='type of the attachment you want to retrive'

Example:
SELECT Id,Name,ContentType FROM Attachment where ParentId ='0019000000CHVUl' AND contenttype in ('image/png','image/gif')



If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Gabriel Costa 13Gabriel Costa 13
Hi Swenta,

I've used SOQL as you mentioned in the endpoint below but I'm getting 0 results even having files attacheds. Is there any other object that stores the media data?

https://MYORG.my.salesforce.com/services/data/v56.0/query?q=SELECT FIELDS(ALL) FROM Attachment WHERE Name!='log.txt' LIMIT 200

 
Gabriel Costa 13Gabriel Costa 13
Hi, I found the issue. As I'm using PropertyBase I need to use a custom object, that is PropertyMedia, this query below fix the issue.

https://MYORG.my.salesforce.com/services/data/v56.0/query?q=SELECT FIELDS(ALL) FROM pba__PropertyMedia__c WHERE pba__Property__c='ID' LIMIT 200

Thanks for your answer Swenta, it guided me to the right answer.
usman Mustafviusman Mustafvi
In Salesforce, you can use the ContentDocumentLink object to retrieve the images and other files associated with a specific object such as 'Property' in SOQL.
Here is an example of a SOQL query that retrieves all the images associated with a 'Property' object:
SELECT ContentDocument.Title, ContentDocument.FileType, ContentDocument.LatestPublishedVersion.Id
FROM ContentDocumentLink
WHERE LinkedEntityId = 'Property_Id'
In this query, you need to replace the Property_Id with the actual Id of the 'Property' object that you want to retrieve the images for.
You can also use different fields of ContentDocument and ContentVersion to filter and retrieve the specific media files that you need.
Please note that it is not possible to directly query the ContentDocument object. You have to use the ContentDocumentLink to get the related documents.
Additionally, you can use the ContentVersion object to retrieve the version information of the media files and use it to sort or filter the results.
It is important to note that in a query, you can only get the files that are shared with the user who runs the query. you can use the sharing settings to share the files with users or groups that need to access them.