apex-rollup icon indicating copy to clipboard operation
apex-rollup copied to clipboard

SUM Rollup Bug when using Where Clause and differing currencies

Open fabm97 opened this issue 6 months ago • 1 comments

Hi James,

I think I may have found another edge-case bug regarding currencies. If you have a rollup using a where clause it sometimes doesn't take currencies into account for recalculation. The cases I found are as follows:

  • You relate Child B (which doesn't fit the Where Clause) with Parent A On the child you have a Status (1,2,3) and with your Where Clause you only want to roll up records in Status 2. Child A (Amount = 10 EUR) with Status 2 is already related to Parent A (Sum of Amounts X PLN, which is equal to 10 EUR).
    You now go ahead and connect Child B (1 EUR) with Status 1 to the Parent A. Now you would think the Sum on Parent A should remain at X PLN (10 EUR) but instead it goes to 10 PLN. So the rollup recalculates, which normally would be fine, but it doesn't take currency into account.
  • I was able to fix this behaviour for relating records by checking "Is Full Record Set" in the Metadata record, but the same behaviour then still happens when you delete the parent from the lookup of the child.

Is there something that I have missed in the config or is this case maybe not covered for multi-currency transactions? If you need any logs or even access to my developer sandbox I can provide it to you :)

Metadata Setup: Image

Child A Image

Child B Image

Parent A Image

fabm97 avatar Jun 20 '25 13:06 fabm97

Let me take a look, I'll let you know if I need more info!

jamessimone avatar Jun 23 '25 13:06 jamessimone

Hi James, did you already have the chance to take a look?

fabm97 avatar Jul 04 '25 07:07 fabm97

I haven't had the chance yet, I will keep you updated

jamessimone avatar Jul 04 '25 10:07 jamessimone

I was able to spend some time looking into this issue today. Here's what I discovered:

  • for rollups with a Default Recalculation (Number/Text) Value configured, the default behavior for updating a child with a non-matching where clause (which is the correct thing - not running the rollup at all) is overridden
  • as you noticed, rollups with Is Full Record Set set to true will re-query all other matching children; rollups without that flag enabled (at least for SUM) are run as a "diff-based" calculation, and when any rollup starts without any children at all (because of a where clause), a recalculation aggregate query is issued. According to the docs:

Aggregate function results on currency fields default to the system currency

So ... where does that leave us?

If it's possible, this issue may self-correct if you can live without the full recalculation default number value (you could even use a flow or Apex to ensure that parents with an amount set to null end up using 0 instead). Otherwise, I will have to look at what sounds like another bug with the Is Full Record set functionality when removing the parent lookup value from any child record because Apex Rollup should always have all other children for a given parent in context when that option is selected.

jamessimone avatar Jul 04 '25 16:07 jamessimone

Hi James, thanks for your answer! I was able to fix the wrong calculations by removing the full recalculation default number on all Rollups. However one case that took me some time to figure out was that I was using a related field in a where-clause which also led to a miscalculation by aggregate query in some instances. I figured I don't really need to filter that field and found another solution, so that's now fixed as well for me.

Thanks again for you help!

fabm97 avatar Jul 10 '25 09:07 fabm97