[Master]- Report 152 "Calculate Low Level Code" terminates with error: "Cannot add instance as another with key %1 has already been added." after upgrade to v28.1#9531
Conversation
… calculation IsChild in codeunit 3684 corrupted the BOM tree dictionary's shared enumerator by exiting early on a match without resetting it, causing a later lookup on the same parent to miss an existing child and wrongly insert a duplicate node. Replaced the enumeration with an O(1) key lookup (ChildNodes.TryGet). Added regression test TestItemAndSKUsSharingSameProductionBOM in codeunit 137053.
|
Could not find linked issues in the pull request description. Please make sure the pull request description contains a line that contains 'Fixes #' followed by the issue number being fixed. Use that pattern for every issue you want to link. |
Copilot PR ReviewIteration 2 · Outcome: completed
Knowledge source: https://github.com/microsoft/BCQuality@186d8a131465475c79244d994acb872cd5c0d4bf Orchestrator pre-filter (2 file(s) excluded)
Findings produced by the AL review agent v1.7.3. Reply 👎 on any inline comment to flag false positives. |
qasimikram
left a comment
There was a problem hiding this comment.
The fix replaces the stateful child enumeration with a non-mutating key lookup, eliminating the shared-enumerator corruption while preserving the dictionary's key semantics. The regression test accurately recreates the failure sequence with an item and two SKUs sharing one production BOM and verifies the resulting low-level codes.
Bug 642311: [ALL-E] Report 152 "Calculate Low Level Code" terminates with error: "Cannot add instance as another with key %1 has already been added." after upgrade to v28.1
AB#642311
Issue: When running Report 152 "Calculate Low-Level Code" (regression after the 28.2 upgrade), the report terminates with "Cannot add instance as another with key {"Type":"Production BOM","No.":"BM00780"} has already been added." This occurs when an item and one or more of its Stockkeeping Units all point to the same Production BOM, so the same Item→Production BOM relation is offered to the BOM tree more than once.
Cause: In the IsChild procedure in BOMTreeNode.Codeunit.al (codeunit 3684 "BOM Tree Node"), the existence check enumerated ChildNodes with MoveNext() and did exit(true) on a match without calling ResetEnumerator(). Because the dictionary uses a single shared enumerator, exiting mid-loop left it stranded; the next IsChild call on the same parent resumed from that position, missed the already-added child, and returned false. The ChildHasKey guard in AddChildToParent then passed and a duplicate node was inserted, raising KeyAlreadyExistsErr.
Solution: Replaced the enumeration in IsChild with a direct O(1) key lookup exit(ChildNodes.TryGet(ChildKey, Child));, which uses the dictionary's KeyIndexMap and never touches the shared enumerator. The duplicate relation is now correctly detected and skipped, so the calculation completes successfully. Added regression test TestItemAndSKUsSharingSameProductionBOM in codeunit 137053 "SCM Low-Level Code" covering an item plus multiple SKUs sharing one Production BOM.