find the document or content file attached to a specific object (say, Object A) in Salesforce





trigger FindAttachedFiles on ObjectA__c (after insert) {

    // Collect Ids of all newly inserted Object A records
    Set objectAIds = new Set();
    for (ObjectA__c objA : Trigger.new) {
        objectAIds.add(objA.Id);
    }

    // Query ContentDocumentLink to get the related ContentDocument for Object A
    List contentLinks = [SELECT ContentDocumentId, LinkedEntityId 
                                              FROM ContentDocumentLink 
                                              WHERE LinkedEntityId IN :objectAIds];

    // Check and process each ContentDocumentLink
    for (ContentDocumentLink link : contentLinks) {
        // Get the ContentDocumentId to retrieve the document details
        ContentDocument contentDoc = [SELECT Id, Title, FileType 
                                      FROM ContentDocument 
                                      WHERE Id = :link.ContentDocumentId 
                                      LIMIT 1];
        // Perform logic with contentDoc, such as logging, processing, etc.
        System.debug('File Title: ' + contentDoc.Title + ', File Type: ' + contentDoc.FileType);
    }
}



find the document or content file attached to a specific object (say, Object A) in Salesforce  find the document or content file attached to a specific object (say, Object A) in Salesforce Reviewed by dasfrogpractice on 05:21 Rating: 5

No comments:

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