Skip to content

[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

Open
DhavalMore88 wants to merge 2 commits into
microsoft:mainfrom
DhavalMore88:bugs/Bug-642311-Main-ReportCalculateLowLevelCodeterminateswitherror-3
Open

[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
DhavalMore88 wants to merge 2 commits into
microsoft:mainfrom
DhavalMore88:bugs/Bug-642311-Main-ReportCalculateLowLevelCodeterminateswitherror-3

Conversation

@DhavalMore88

@DhavalMore88 DhavalMore88 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

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.

… 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.
@github-actions github-actions Bot added the From Fork Pull request is coming from a fork label Jul 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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.

@DhavalMore88
DhavalMore88 marked this pull request as ready for review July 16, 2026 10:37
@DhavalMore88
DhavalMore88 requested a review from a team July 16, 2026 10:37
@DhavalMore88 DhavalMore88 added the SCM GitHub request for SCM area label Jul 16, 2026
@github-actions github-actions Bot added the Linked Issue is linked to a Azure Boards work item label Jul 16, 2026
@github-actions github-actions Bot added this to the Version 29.0 milestone Jul 16, 2026
Comment thread src/Layers/W1/BaseApp/Inventory/BOM/Tree/BOMTreeNode.Codeunit.al
@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Copilot PR Review

Iteration 2 · Outcome: completed

All 16 sub-skills completed; the diff is a small, well-contained bug fix (replacing an enumerator-based lookup with a direct key-existence check) plus a matching regression test. No sub-skill found a knowledge-backed defect, and the self-review pass found no cross-cutting concern that clears the agent-finding precision bar.

Knowledge source: https://github.com/microsoft/BCQuality@186d8a131465475c79244d994acb872cd5c0d4bf

Orchestrator pre-filter (2 file(s) excluded)

  • layer-disabled (knowledge) : 2 file(s)

Findings produced by the AL review agent v1.7.3. Reply 👎 on any inline comment to flag false positives.

v-ankitgoyal
v-ankitgoyal previously approved these changes Jul 16, 2026

@qasimikram qasimikram left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

From Fork Pull request is coming from a fork Linked Issue is linked to a Azure Boards work item SCM GitHub request for SCM area

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants