|

Count files on a Salesforce record

I got a question earlier today about how can we count files on a Salesforce record.

Files in Salesforce are stored as Content Document records and there are several objects that allow them to be stored, related to records and have version control. The object that we are interested in is the Content Document Link so we can count related files.

New Field

We will create a new field on the Account record that sill store a count of related files. We will then use a Record Triggered flow to update this field. If you want to count files on another object, just create this field on your desired object and change the object in the Choose Object screen below.

Once we have this counter on the record, we can use a validation rule to check if it is greater than zero – to indicate if a file has been uploaded or not.

Count files on a Salesforce record - custom field


Next, we will create a record triggered flow that will update that field.

New Flow

It’s a pretty simple flow with only a few elements. When completed, it wil look like this:

Count files on a Salesforce record - flow overview

On with the flow !

First, select a Record-Triggered Flow:

Count files on a Salesforce record

Perform this action when a record is created or updated and after the record is saved.

Correction: Change to Trigger the Flow When: A record is updated (in the screen below).


Select the Account object and always update it.


Get all Content Document Links that have a link to our record.

Count files on a Salesforce record - get files


Set our new counter field to equal the count of records that we found from our get records.


Finally, update the record.

Count files on a Salesforce record - update counter on account


Test and see if it works!


Here is the counter value:


Here are the 3 files, success!

Remember you need to update the Account record for this trigger to run and update the counter.

Want to learn more about Flow?

Check out our Learning Salesforce Flow course at CertifyCRM.com.

Similar Posts

13 Comments

  1. For cases it does not work properly. It counts files if they are added manually, but if a file is added for example by outlook integration (log email through outlook to a case) then it does not find that Files.
    Any clue?

    1. In the 6th screenshot above, check that you are checking for files that are related to your $Record -> CaseID. If this is correct, it suggests that when Outlook integration is adding files they are not related directly to the case. If you have the Salesforce inspector tool (Chrome plugin), open one of these cases, look at all data and see if you can find what the file is related to.

  2. Great solution – Currently, the record needs to be updated to reflect the total count of attachments on an object. We have contract object records and want to count the total number of attachments on these contract records to verify whether users have provided the red line documents or not.
    1) Is there any way such that the counter can automatically be updated when the FILE/Attachment is uploaded or deleted without updating the actual contract record?
    2) Is there any way that can set the counter only when the attached File Type is PDF?

    1. For q1 – you can’t trigger the flow on content document link or files so I had to take the record updated approach.
      For q2 – you may need to get the content document version (via content document link) to determine the file type.

  3. Just got to see note message below –
    Remember you need to update the Account record for this trigger to run and update the counter.

    So need to write trigger to update the Account whenever file is inserted/updated/deleted. Any thoughts if this can be done easily for all 3 cases. My concern is most on delete file.

    1. This solution solves the problem of an individual record being updated by a user.
      For the existing records (that have not been updated) but you want a count of the files, I suggest you create a scheduled flow to trigger their update. This will be bulkified so all you need is a checkbox field on Account that you can test to see if the account needs to be updated, if it is not set then update the Account by setting the checkbox to true. This will then trigger the flow to run and update the file count. If you have a large number of accounts I suggest you only update a small selection (say a few thousand at once, maybe all accounts for a specific country) and see if it works ok. You will need to process all accounts eventually but do it step by step with a scheduled flow and just keep editing the country until the bulk are done then you can remove the country filter and do the remainder. Setting all these existing records is a one-off task, once they have been updated, the flow will take care of all the updates.

      One other thing you may want to consider is an “automation switch” that turns off all automation and allows you to run your dataloader jobs without having to manually turn them off and back on again. Basically you create a custom setting and turn this on/off as needed. In every automation you set this as one of the start criteria – if automation is on then run the flow, if not don’t run.

  4. sorry for too many questions – as i am working on urgent requirement for the client.

    It appears to be this flow is not bulkified? if 1000 files are loaded through data loader is this flow is going to be invoked 1000 times? is it not going to hit governor limit. Please clarify. Thank you.

  5. Thank you for this nice solution. How can I get this flow to fire when a user just upload’s a document without editing the account? The counter only updates itself if I manually edit the account after uploading.

    1. This is tricky because we can’t trigger (via flow or apex) on the file upload. If you create your own flow to do the file uploads then you could add a checkbox on account that you could set to make the flow that counts the number of files trigger to calculate the number of re4lated files. You will also have to reset it after the flow triggers. Maybe call another flow to do this with a short pause. Sounds a bit messy but I think it is possible.

    1. Yes the count will be zero when all files are deleted – but note the update only triggers when the account record itself is updated. So deleting a file attached to the account will not cause it to recalculate but any update on the account record will do the update. Another solution would be to use DLRS.

Leave a Reply