Query Files related to an object

<!-- Query Files (ContentDocument & ContentDocumentLink) -->
List<ContentDocument> files = [ 
    SELECT Id, Title, FileType, ContentSize, LatestPublishedVersionId 
    FROM ContentDocument 
    WHERE Id IN (
        SELECT ContentDocumentId 
        FROM ContentDocumentLink 
        WHERE LinkedEntityId = :objectAId
    )
];

for (ContentDocument file : files) {
    System.debug('File Name: ' + file.Title);
}

<!-- Query Classic Attachments -->
List<Attachment> attachments = [
    SELECT Id, Name, ContentType, BodyLength 
    FROM Attachment 
    WHERE ParentId = :objectAId
];

for (Attachment att : attachments) {
    System.debug('Attachment Name: ' + att.Name);
}

<!-- Query File Versions (Optional) -->
List<ContentVersion> fileVersions = [
    SELECT Id, Title, VersionData, ContentDocumentId 
    FROM ContentVersion 
    WHERE ContentDocumentId IN (
        SELECT ContentDocumentId 
        FROM ContentDocumentLink 
        WHERE LinkedEntityId = :objectAId
    )
];

for (ContentVersion version : fileVersions) {
    System.debug('File Version Name: ' + version.Title);
}
Query Files related to an object Query Files related to an object Reviewed by dasfrogpractice on 09:29 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe