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
George Laird 39George Laird 39 

I found a way to report on all files related to an object! BUT I NEED HELP!

Hello All,

As you probably know, there is no way to run a report in Salesforce to show all files related to an object.  My client wants to run a report of all files that were uploaded to a custom object called POC__C.  They also want a link to the file directly in the report.  Ok, so I found a way to do this by making a custom object called FilesCopyCat__c.  This object doesn't hold files, just a link to them with a link to the related POC__record.  It works great!  I got the report I need by running a report type of POC's with Opporutnities and Files.  Since POCs are also realted to Opptys, I can run a report with the Oppty, then the POC, and get a list of all files with a link!  

BUT...how can I make this work with ALL objects, not just POC__C??  Please take a look at my trigger here and let me know if you have a way to make this work on all objects?  

Keep in mind, I have a field on this new FilesCopyCat object called "Related POC__c record."   I would want to replace this with just "related record." Or something.  This is where I need help. 



trigger FilesTrigger3 on ContentDocumentLink (after insert) {
    
    List<Files_CopyCat__c> ccs = new List<Files_CopyCat__c>();
    
    for (POC__c o: [SELECT Name, (SELECT ContentDocumentId, ContentDocument.Title, ContentDocument.ContentSize,ContentDocument.FileType 
                                  FROM ContentDocumentLinks 
                                  WHERE id IN: Trigger.new) 
                    FROM POC__c]) {
                        
                        for (ContentDocumentLink cdl: o.ContentDocumentLinks) {
                            
                            System.debug('Linked Document Id: ' + cdl.ContentDocumentId);
                            System.debug('Linked Document Title: ' + cdl.ContentDocument.Title);
                            System.debug('Linked Document Size: ' + cdl.ContentDocument.ContentSize);
                            
                            
                            Files_CopyCat__c f = new Files_CopyCat__c();
                            
                            f.Name = cdl.ContentDocument.Title;
                            f.File_Id__c = cdl.ContentDocumentId;
                            f.Related_POC__c = o.id;
                            f.File_Type__c = cdl.ContentDocument.FileType;
                            f.Link_To_File__c = URL.getSalesforceBaseUrl().toExternalForm() + '/' + cdl.ContentDocumentId;
                            
                            ccs.add(f);    
                            
                        }
                        
                        try{
                            insert ccs;    
                        } catch(DmlException e) {
                            system.debug('The following exception has occurred: ' + e.getMessage());
                        }
                        
                    }
}