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
Phuc Nguyen 18Phuc Nguyen 18 

Get number of Files attached to a record

Need help counting the number of files atatched to a custom object.
Should I be looking at ContentDocumentLinks
 
if ( Trigger.isInsert || Trigger.isDelete ){
            Set<Id> FileCount = new Set<Id>();

            for(SObject cdl : Trigger.new){
                ContentDocumentLink cdls = ( ContentDocumentLink ) cdl;

                FileCount.add(cdls.LinkedEntityId);
            }

            List<File_c> fileList = [SELECT Id, (SELECT Id FROM ContentDocumentLinks) 
            FROM File_c WHERE Id IN :FileCount];

            for(File_c fl : fileList){

                fl.Number_of_Files__c = fileList.size();

            }
        }

 
David Zhu 🔥David Zhu 🔥
You may do the followings:
1. get the list or IDs, put in a list, lets call it listIds;
2. use the following query:
select count(id),FirstPublishLocationId from contentversion where  IsLatest = true and FirstPublishLocationId in :listIds group by FirstPublishLocationId
3.Loop  through the trigger.new and query result of step 2, set  number_of_files__c.
VinayVinay (Salesforce Developers) 
Hi Phuc,

Check below examples to number of Files attached to a record

https://salesforce.stackexchange.com/questions/312483/count-of-files-attached-to-custom-object-record
https://developer.salesforce.com/forums/?id=906F0000000g0oZIAQ

Thanks,
mukesh guptamukesh gupta
Hi Phuc,

Please use below query :-
 
select Id, 
(SELECT Id, Name FROM Attachments), 
(SELECT Id, Title FROM Notes) 
from "your Object" 
where id='your object ID'

If this solution is usefull for you, Please mark as a Best Answer to help others.


Regards
Mukesh

 
Phuc Nguyen 18Phuc Nguyen 18
Wow this is even more confusing.  I have read 3 different suggesttions.  Are the fiels kept in contentversion ,ContentDocumentLink , or Attachment?
@David Zhu ,
1. get the list or IDs, put in a list, lets call it listIds;   --- is this the custom object id?---
2. use the following query:
select count(id),FirstPublishLocationId from contentversion where  IsLatest = true and FirstPublishLocationId in :listIds group by FirstPublishLocationId  --  is contentversion where thh files are stored?

 
David Zhu 🔥David Zhu 🔥
1. get the list or IDs, put in a list, lets call it listIds;  --- is this the custom object id?--- Yes.
List<Id> ids = new List<Id>();
if (trigger.isInsert) 
{
   for (SObject cdl : Trigger.new){ 
    ids.add(cdl.id);
    }
}
else if (trigger.isDelete)
{
   for (SObject cdl : Trigger.old){ 
    ids.add(cdl.id);
    }
}

2 is contentversion where thh files are stored?  -Yes
Phuc Nguyen 18Phuc Nguyen 18
Hey @David Zhu
I if I am querying the contentversion  object how am going to assign the count/size to the custom object?
for (SObject cdl : Trigger.new){ 
                ids.add(cdl.id);
                }
    
            List<contentversion> FileList= [select count(id),FirstPublishLocationId from contentversion where IsLatest = true 
                                 and FirstPublishLocationId in :ids group by FirstPublishLocationId ];
    
            for(File_Holder__c qd : FileList){
    
                qd.Number_of_Files__c = FileList.size();
    
            }

 
David Zhu 🔥David Zhu 🔥
It would be like:

AggregateResult[] groupedResults = [select count(id),FirstPublishLocationId from contentversion where IsLatest = true and FirstPublishLocationId in :ids group by FirstPublishLocationId ];

for (AggregateResult ar : groupedResults) {

   // you get the record id and count and assign accorindingly

    System.debug('Customer Object ID ' + ar.get('FirstPublishLocationId '));
    System.debug('File count' + ar.get('expr0'));
   
}
TheDeckbladTheDeckblad
Using FirstPublishLocationId won't help if a user first uploaded a file to their user's file directory and then later attached it to your custom object. The FirstPublishLocationId will still be still be set to the user.