jamessimone / apex-rollup

Fast, configurable, elastically scaling custom rollup solution. Apex Invocable action, one-liner Apex trigger/CMDT-driven logic, and scheduled Apex-ready.
MIT License
220 stars 30 forks source link

Roll-up after Master-Detail Cascade Delete not working #463

Closed cdevelin closed 1 year ago

cdevelin commented 1 year ago

Hi James,

I have a parent object which has 5 summary fields, which are updated by 5 separate roll-ups from the same child table, with each roll-up selecting different record types in the child transaction table.

It's working perfectly during normal transaction creation, with the 'SUM' fields on the parent object updating according to the transaction record type created.

However, it is not working when I delete the child transactions. I have checked the trigger on the child transaction object, and it includes the required 'before delete' as part of the trigger operation:

trigger AssessmentTransactionRollupTrigger on AssessmentTransaction__c(after insert, after update, before delete, after undelete) { Rollup.runFromTrigger(); }

I'm testing in a dev scratch org, and I have activated the 'Is Dev Ed or Trial Org' parameter on the Org Default CMDT.

When I do a full-recalc, it does resolve the issue, but if I try to do a full-recalc on all 5 roll-ups at the same time, I often (but not always) get the error 'Too many queueable jobs added to the queue: 2' when it runs the 'RollupDeferredFullRecalcProcessor'.

When I create new transactions, the Queueable job created 'RollupAsyncProcessor' works very quickly, and updates everything well.

But when I delete transactions, no Queueable job is created at all.

I have tried switching from the default 'Should Run As' = 'Queueable' to 'Batchable', but it made no difference (to either the rollup after delete, nor the queueable failures in the scratch org - it still tried to create too many queued jobs).

What I suspect is happening is that I am not actually deleting the transactions themselves during this testing, they are the detail records in a master-detail, and I am deleting the master batch which contains the transaction children.

So deleting a master causes a cascade delete, but this is NOT triggering the 'before delete' RollUp trigger on the children.

If you're wondering why I'm using Apex Rollup when I could just use out-of-the-box rollup on a master-detail, it is because the object with the roll-ups is not the master (which is the transaction batch), but a related lookup.

cdevelin commented 1 year ago

Just googled it, and realised it's a long standing gripe that Salesforce doesn't fire triggers on cascade deletes! So it's nothing buggy in your code James, it was my ignorance and assumption that a before delete trigger should fire before every delete. Foolish assumption :)

jamessimone commented 1 year ago

Not to worry, and yes I've been bitten by this particular platform idiosyncrasy before! One thing you might be able to do (with some tweaking), would be to have the delete version of the rollup set up to run from the parent record. If you're comfortable with Apex, you'd be able to do the customization for this part there; otherwise, the easiest way is actually through a record triggered flow using the base rollup action (where you fill in the values yourself instead of using custom metadata). Because you'd be using the "Is Rollup Started From Parent" flag for the delete case, it would (by default) perform a full recalc.

Since you said you have a more complicated object model, I may be misrepresenting how this would work within your org and I do apologize if that's the case. Either way, I appreciate you reaching out and if there's something I can do to help, just let me know

jamessimone commented 1 year ago

Closing for now - again, let me know if what I outlined above works!