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

Ultimate Parent Rollup sometimes not aware of currency

Open fabm97 opened this issue 3 years ago • 26 comments

We have multiple SUM Rollups that rollup values of currency fields of Object A into currency fields on a parent object B. Then we also have Ultimate Parent Rollups from the same source field of Object A into the Parent Object B which has multiple Object B beneath it. image

Normally the calculations are correct but with one of the Ultimate Parent Rollups it seems, that Apex Rollup completely ignores currencies. I ran the calculation of the rollup myself in excel and in this one case Apex Rollup just adds up all results from the SOQL and saves it in the Parent Object without bringing it into the currency of the Parent Object either. Here is the log of the rollup that calculates the wrong value: apex-07L5t000000OfCUEA0.log

And here is my calculation if I sum up the values in excel:

_ No. of Records Notional Amount in EUR Notional Amount
Calculated from Table 586 2022691054 1901838874

2022691054 is the correct value we would want and 1901838874 is the one Apex Rollup calculates. 1901838874 is just all numbers summed up without accounting for any currencies. The Rollup Metadata Types from the correctly working Rollup and the one that calculates without currency adaption are nearly the same. The only difference is, that we filter for another Record Type of Object A and write into another field of Object B (Which is also a currency field). Any ideas what this could be about?

fabm97 avatar Jun 09 '22 09:06 fabm97

@fabm97 I will take a look at this as soon as I can. I have a bit less free time this week, but I will keep you updated on my progress. Thank you, immensely, for providing the image and table. The documentation you provide truly goes a long way in helping to diagnose and address these issues, and it does not go unappreciated!

jamessimone avatar Jun 09 '22 15:06 jamessimone

@fabm97 I haven't forgotten about you! I was on vacation last week, and have been trying to finish working on #325 in the meantime. If I'm unable to slot the fixes for that particular issue into a release tomorrow, I'll switch tracks and start working on this issue next. Stay tuned.

jamessimone avatar Jun 16 '22 22:06 jamessimone

@jamessimone Thanks for taking care of it! I hope you had a great vacation :) We currently have implemented a workaround of doing a two-step Rollup instead the Ultimate Parent Rollup, so we are in a good position to wait for the fix for now :)

fabm97 avatar Jun 17 '22 10:06 fabm97

@fabm97 I've finally had a chance to come back to this - took a bit longer since I know you said you had a workaround in place. It' still something I'd like to get fixed. Presently, I don't see exactly where this issue could be occurring. I'm wondering if I were to create a beta package for you, if you would be able to install it in a sandbox with some data in it that we could use to generate the logs necessary to properly pinpoint where this issue is occurring?

The truly bizarre part, in my eyes, is this part of your summary:

The Rollup Metadata Types from the correctly working Rollup and the one that calculates without currency adaption are nearly the same. The only difference is, that we filter for another Record Type of Object A and write into another field of Object B (Which is also a currency field).

At the moment, the only thing that drives out when currencies are converted is consistent between rollups:

a) multi-currency must be enabled in the org b) either the rollup field on the child / rollup field on the parent must be of type currency

Since you seem to meet that criteria, there's nothing obvious in the code that's missing. That's why I'm hopeful to be able to create a beta package with additional logging in it, so that I can properly diagnose what's going on. I will also follow my standard procedure here - trying to reproduce this issue by means of a failing test - so in the event that I'm successful on that front in figuring things out, I'll update you here.

jamessimone avatar Jun 30 '22 14:06 jamessimone

@jamessimone Hey James, yes sure I will create a sandbox and try to recreate this issue there. I hope this is recreatable there, because I think even between our QA and Prod there were some differences in the behaviour of this bug. I will let you know if I can recreate it there and then we could go on with a beta package for logging :)

fabm97 avatar Jul 01 '22 08:07 fabm97

@fabm97 sounds good! I will let you know if I make progress in the meantime

jamessimone avatar Jul 01 '22 13:07 jamessimone

@jamessimone Hey James, I managed to recreate this error on testdata in my dev sandbox and got the permission from my customer to install a beta package. So just let me know when you have the beta package ready and what I need to send to you once it is installed :)

fabm97 avatar Jul 04 '22 15:07 fabm97

@fabm97 excellent. I will be able to create a beta package later today. I started work on it this morning but was catching up from the relatively long holiday weekend here in the States as well so I didn't quite finish it up.

jamessimone avatar Jul 05 '22 15:07 jamessimone

@fabm97 apologies, took a bit longer than expected - here is a link to the beta package with some additional logging in place. Please note that there's a chance that I've already fixed the underlying issue, as I also did some multi-currency work while adding the logging. That being said, the log that I would be looking for would be the QueueableHandler log created during the process of you reproducing the issue. Looking forward to hearing back.

jamessimone avatar Jul 06 '22 14:07 jamessimone

@fabm97 I've gone ahead and released the functionality I was asking you to test in the latest version, as I view this as a positive change even if it doesn't completely address the underlying issue you were having. Please let me know!

jamessimone avatar Jul 08 '22 14:07 jamessimone

Hey @jamessimone, I now had the opportunity to install the beta package and test it. Sadly the wrong calculation is still happening. Here is the log of the Queueable Handler: 2022-07-08_Queueable_Handler_Log.log

The data the rollup should work with is this:

SK_Notional_Amount__c Notional_Amount_EUR__c CurrencyIsoCode
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
173220 10000 TRY
10000 10000 EUR
4580500 500000  

Half of the records are in turkish Lira, the other half in Euro. The Ultimate Parent has Euro defined as currency. As you can see in the log the Rollup seems to just take 173220 TRY as EUR. Hope this log helps, if you need me to try anything else let me know :)

fabm97 avatar Jul 08 '22 15:07 fabm97

I will take a look!

jamessimone avatar Jul 08 '22 16:07 jamessimone

@fabm97 was rollup logging enabled for the log you produced? I see the entry points into the logging that I added to look into this for you, but none of the actual log messages.

jamessimone avatar Jul 08 '22 18:07 jamessimone

@jamessimone Yes, sorry! Totally forgot to tick the checkbox :D Here is the log with Logging actually enabled: 2022_07_08_Rollup_Log_with_actual_Logs.log

fabm97 avatar Jul 08 '22 18:07 fabm97

@fabm97 OK, in looking at the log I'm noticing something interesting - the parent record's currency ISO code is not being captured (it's being reported as null, which hardly seems possible). That's why all of the notional amounts are being translated directly without conversion onto the parent record. I'm going to release a new version which always includes the CurrencyIsoCode (there was some conditional logic in previous version which might have been excluding that field on certain queries). That should address this issue.

jamessimone avatar Jul 11 '22 14:07 jamessimone

@fabm97 I've released a new version of Apex Rollup which might address this issue. Otherwise I'll have to create another beta version with additional logging to test where the failure point is. The crucial thing - which still puzzles me - is why the logging that I'd put in place (as I mentioned in the last comment) was returning null for the parent record's currency ISO code. I'm assuming if you write something like:

SELECT CurrencyIsoCode
FROM Security_Limits__c
WHERE Id = 'a4i0D0000001FjiQAE'

That you end up with EUR ? Just want to make sure of that!

jamessimone avatar Jul 12 '22 22:07 jamessimone

Hey @jamessimone, yeah the CurrencyIsoCode can be queried by me. The table I posted earlier was also just a table of a query I made. So yeah very weird it pops up as null in the logging! The version with the fix is deactivated right now due to some other bug right? As soon as it is available to install I will install on my sandbox and see if the problem persists :)

fabm97 avatar Jul 13 '22 08:07 fabm97

@fabm97 that's correct but the updated version is being released at this moment. you should be able to install v1.5.15 in just a few minutes

jamessimone avatar Jul 13 '22 16:07 jamessimone

@jamessimone I updated the sandbox and retried the calculation. It seems the CurrencyIsoCode is now successfully queried but the result of the rollup is still the same: 2022_07_14_Queueable_Handler_Log.log

fabm97 avatar Jul 14 '22 10:07 fabm97

@fabm97 I will take a look. Might have to install some additional beta logging again but I will let you know! Thanks again for your help and patience with tracking down this issue.

jamessimone avatar Jul 15 '22 20:07 jamessimone

@fabm97 apologies, I've been traveling and otherwise tied up with work. I've created a new beta package for you that I think contains all the logging I need to really pin down this issue.

jamessimone avatar Jul 29 '22 19:07 jamessimone

Hey @jamessimone, no problem! Sorry for the delay on my side too :) I installed the package and let the rollup run again. Here is the Log File: 2022-08-03_Queueable_Handler_Log.log

Seems like the parent ISO Code is not queried somehow?

fabm97 avatar Aug 03 '22 10:08 fabm97

@fabm97 curious, only some of the logging statements I put in seem to be part of that attached log. Perhaps it was cut off. In particular, I was looking to monitor the queries that were being performed when the grandparent records are getting retrieved to see why the ISO code is showing up like that. Were there multiple logs that were generated by your letting the rollup run again?

jamessimone avatar Aug 04 '22 17:08 jamessimone

@jamessimone Hmm weird, the file I uploaded is the version I downloaded directly from the Developer Console. It's the log of the QueueableHandler. The other Logs that are created while the recalculation is running are only aura logs: image The only other log containing any debug statements is the first Aura Log: apex-07L0D00000Ck8Z4UAJ.log

Do I need to run it in a certain way? I the rollup run on Org_Default and the only change I made was enable Logging. I always start the rollup from the "Recalculate Rollup" page.

fabm97 avatar Aug 05 '22 14:08 fabm97

@fabm97 I will have to experiment and get back to you. That Aura log contained only the synchronous portions of the full recalculation rollup starting, without any of the logs I put in to detect multi-currency queries. Of course, the absence of the logs that I was expecting may in and of itself be a clue; I wasn't logging all queries made within Apex Rollup, just the ones that should be going off for grandparent-and-greater rollups. I'll continue to pull on that thread.

I feel bad this issue has remained open for so long, and I won't have the chance to investigate further till the week after next. At that time, I'll be working in my own multi-currency org with the beta package I provided to you, and I'll try to isolate what's happening based on info you've already provided me with so I don't have to keep bothering you. I really appreciate you partnering with me on this issue!

jamessimone avatar Aug 06 '22 20:08 jamessimone

@jamessimone Hey James, no worries! Really glad to help and I really appreciate the work you put into this project! Just let me know if you need any more info from me :)

fabm97 avatar Aug 12 '22 09:08 fabm97

@fabm97 continuing to chip away at how this could be caused - I've created another beta package for you which features some simple logging. I would only use a logging level of DEBUG in your Apex dev console / trace, as I do believe parts of the Queueable log you sent over last time were actually truncated due to having exceeded the max log size.

jamessimone avatar Aug 19 '22 14:08 jamessimone

Hey @jamessimone, sorry for the delay, here is the Log generated with the new version and Logging Level set to debug_only: 2022-08-24_Queueable_Handler_Log.log

fabm97 avatar Aug 24 '22 08:08 fabm97

Not to worry! I will have a look at the log, thank you!

jamessimone avatar Aug 24 '22 12:08 jamessimone

@jamessimone

Can I ask what the current state and considerations are for multicurrency in rollups? Is there anything additional I need to set if one or more of the child records has a different currency than the parent record? Or current version should resolve by default if rollup is on currency field?

RtimmFF avatar Feb 21 '23 11:02 RtimmFF