Salesforce Content Document Link when Service Report is generated

Salesforce Content Document Link when Service Report is generated

If you want to query Salesforce Content Document Link(ContentDocumentLink) records to which they are attached when the Service Report is generated, we have to use the ParentId on the Service Report Object/Entity. Trigger on ContentDocumentLink object/entity is not fired when the Service Reports are generated and attached. So, we can make use of the following apex trigger for reference.

Sample Apex Trigger:

trigger ServiceReportTrigger on ServiceReport ( after insert ) {
    
    Set < Id > setSRParentIds = new Set < Id >();
    
    for ( ServiceReport objSR : trigger.new ) {
        
        setSRParentIds.add( objSR.ParentId );
        
    }
    
    if ( setSRParentIds.size() > 0 ) {
        
        Map < String, String > mapCLIRecId = new Map < String, String >();
        List < ContentDocumentLink > listCDLs = [
            SELECT Id, LinkedEntityId
            FROM ContentDocumentLink
            WHERE LinkedEntityId IN: setSRParentIds
        ];
        
        for ( ContentDocumentLink objCDL : listCDLs ) {
            
			System.debug( 
                'Service Report is attached to ' + 
                objCDL.LinkedEntityId
            );
            
        }
        
    }
    
}

Leave a Reply