SFDO-Community/declarative-lookup-rollup-summaries

Can't aggregate Salesforce Files (ContentDocumentLink) as child object

matthewsouther opened this issue · 26 comments

Due to an apparent SOQL limitation with the ContentDocumentLink standard object, attempts to use DLRS to capture the ID of the most recent file on a parent record fail.

The validation error reads:

"Apex trigger dlrs_ContentDocumentLinkTrigger caused an unexpected exception, contact your administrator: dlrs_ContentDocumentLinkTrigger: execution of AfterInsert caused by: System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.: Class.dlrs.LREngine.QueryExecutor.query: line 631, column 1"

See Success Community thread: https://success.salesforce.com/0D53A00002v7xXO
11-16-2016 10-50-18 am

Thanks for sharing @matthewsouther, i'll flag this as a future enhancement, maybe there is a workaround the platform limitation.

Are there any updates to this thread? I'm trying to count the number of files for a custom object and run into this if I try to run via Process Builder or Real-Time. When I just run the lookup to calculate from the Manage screen, it runs through and updates.

I think this is fixed per pull request #585

Yes @douglascayers i would agree.

Fixed in v2.10

Hi,
We tried to implement this for attachments it is working fine when we are adding the attachment but when we delete the attachment it is not working as expected. We are trying to put the counts of attachments related to contract object. Count gets increased when we are adding the attachment but it is not decreased when we deleted the attachment.

Can you please help over it.

Hi @afawcett, I have tested this with v2.10 and I'm not getting reliable results:

  1. When I upload a file to a Contact once, the DLRS doesn't work, but when I immediately upload a second file it gets calculated but doesn't count the one of the files, so we are off the correct value by one.
  2. As @rishikumar2018 stated, I'm not able to get this to work when deleting a file from Contact. The Aggregate Result Field in the parent object (Contact) doesn't get updated.

Any clues? Thanks!

Ok thanks for confirming, i'll re-open since our hope this was fixed via #585 does not seem to have transpired sadly.

I'm unable to deploy the trigger for the child object ContentDocument, receive the following error: Error:
dlrs_ContentDocumentTest.testTrigger System.TypeException: DML operation INSERT not allowed on ContentDocument Class.dlrs.RollupService.testHandler: line 282, column 1 Class.dlrs_ContentDocumentTest.testTrigger: line 11, column 1. This is for a rollup counting ContentDocument on Account. Is this related to these reported issues with ContentDocumentLink?

Hi @Ltowns,

You should use ContentDocumentLink object instead of ContentDocument or ContentVersion objects.

One reason is that sharing security is based on ContentDocumentLink object. Users, even admins, only can query and see ContentDocument or ContentVersion records explicitly shared to them. Any whose access is inferred by the user having access to the record the file has been shared to (e.g. account or contact) are ignored. So you would not get accurate counts and the counts would change based on the context user each time.

The other reason is that ContentDocument records can’t be inserted or updated directly which is why the auto-generated test fails. The document is created by Salesforce when a ContentVersion record is inserted and its ContentDocumentId field is null.

You might try cross object filter criteria like “ContentDocument.Title = ‘Account Form’”

Hello @afawcett,

Please, could you let us know if there is an estimated time of resolution for the issue reported by Sebas on the 31st of January?

Many thanks!
Jesus

@Jesus-GA sadly i cannot give an ETA, this is an open source project and thus dependent on peoples personal time. It is flagged as a priorty issue though, which means when some time from a dev does come up this will be one of those looked at first.

I'm also finding that the count in Realtime does not go down for linked Files (rollup based on ContentDocumentLink), though it goes up just fine. A Scheduled rollup does result in number going down for deleted files. But for some use cases a scheduled rollup may not meet the need.

Hi, I was wondering if this has been fixed. I'm working with another client and they could really use DLRS as long as it supports ContentDocumentLink object. Thanks for the hard work!

Hi, I'm trying to count the number of files attached to a record. I'm getting this error, and I'm stuck tracking down the issue. Any help or guidance is appreciated.

dlrs_fistula2
dlrs_fistula1

This issue where the count of files will correctly count for added files but not for deleted files is still occurring in the latest version (2.13). I noticed that if I select the option for "Remove from Record" then the decrementing on the count does work, but not for a Delete. We need this to be accurate in real time so, unfortunately, a scheduled run to correct any incorrect counts won't work.

I would love to understand why the behavior on this object is different from other Salesforce objects.

RelatedListForFilesOptions

Hi, I have setup the roll-up based on ContentDocumentLink and created the trigger successfully. However, when I tested by upload a new file to the record, it throw me an error - "Can't add 1 file to Opportunity"

I read through the Salesforce Help & Training and it ask to inactive the ContentDocumentLink trigger (https://help.salesforce.com/s/articleView?id=000313353&type=1) . Please advice how or any althernative way I could create the roll-up to calculate the number of attachment / files on the object?

image

@JobbyC, are you able to collect any logging information for what is happening? Maybe use Salesforce Debug Logs and send us what you find there? Unfortunately that error message doesn't include any details.

Can you also include the details of your rollup so we can understand how you have it configured?

Completely random guess but I'd suggest one of two likely scenarios. Something in the rollup is configured incorrectly which is throwing an error or something about your opportunities is rejecting this record edit to save the rollup value, maybe a validation rule or something similar.

For future reference I'll do what I can here but I recommend you check into the Trailhead community group here, https://trailhead.salesforce.com/trailblazer-community/groups/0F9300000009O5pCAE. You're much more likely to get help and support there.

Thanks!

SF-Q commented

Hi, Has the issue been fixed to reduce file count when a file is deleted? Are there any workarounds to address this?

@SF-Q Yes managed to resolve it. I attached the roll-up that I did here, hope this help :)
image

SF-Q commented

@JobbyC - I have the same configuration and the file count does not decrease when I delete files. Did you have to write additional code?

@SF-Q If you use the delete option, it will not decrease the number. So, need to click the last option "Removed from Record" and the recalculation will work.
image

I am trying to do this:

SELECT Id, ContentDocumentId,  ContentDocument.LatestPublishedVersion.Vervaldatum__c , ContentDocument.LatestPublishedVersion.Soort_Document__c   FROM ContentDocumentLink
WHERE LinkedEntityId  = '0010600002ET8ZbAAL'  AND ContentDocument.LatestPublishedVersion.Soort_Document__c = 'KvK document'
afbeelding

The query works, but how do i implement that in DLRS?

@riekusr I don't think this issue ticket would be the correct place to discuss your issue. Please reach out on the Trailblazer Community Group (https://trailhead.salesforce.com/trailblazer-community/groups/0F9300000009O5pCAE) and we can discuss it there.

Thanks!