BCApps icon indicating copy to clipboard operation
BCApps copied to clipboard

[Bug]: Calculate Inventory applies default dimensions from an unrelated Location whose Code equals the Item No.

Open Franco111000 opened this issue 4 months ago • 5 comments

Describe the issue

Report 790 "Calculate Inventory", procedure CreateDimFromDefault(), filters Default Dimension with two independent OR-filters:

DefaultDimension.SetFilter("No.", '%1|%2', TempQuantityOnHandBuffer."Item No.", TempQuantityOnHandBuffer."Location Code");
DefaultDimension.SetFilter("Table ID", '%1|%2', DATABASE::Item, DATABASE::Location);

Because "No." and "Table ID" are filtered independently, the combinations are never paired per table. If an Item's No. happens to equal the Code of some unrelated Location (for example both are 10), that unrelated Location's default dimensions pass both filters and leak onto the counting journal line, even when the inventory is calculated for a completely different location.

Verified still present in current main: src/Layers/W1/BaseApp/Inventory/Counting/Journal/CalculateInventory.Report.al, CreateDimFromDefault().

The fix is to replace the cross-product filter with two scoped reads: one for ("Table ID" = Database::Item, "No." = Item No.) and one for ("Table ID" = Database::Location, "No." = Location Code).

Expected behavior

Only the default dimensions of the counted Item and of the Location actually being counted are applied to the physical inventory journal line. A Location that merely shares its Code with the Item's No. must not contribute dimensions.

Steps to reproduce

  1. Create Item with No. = 10.
  2. Create Location with Code = 10 and add default dimensions to it.
  3. Post some inventory for Item 10 on a different location (e.g. BLUE).
  4. Run Calculate Inventory (report 790) for location BLUE.
  5. The journal line receives the default dimensions of Location 10, which has nothing to do with the count.

Additional context

This is a copy of microsoft/BusinessCentralApps#1542 (reported by Karel Koncel, labeled Approved/SCM in the contribution pilot). It was being fixed in microsoft/BusinessCentralApps#1908, which was reviewed (a regression test was requested and added) but was closed unmerged when the repository was retired. Re-filing here per the retirement notice to re-submit the fix against BCApps.

I will provide a fix for a bug

  • [x] I will provide a fix for a bug

Franco111000 avatar Jul 05 '26 12:07 Franco111000

:robot: AI triage completed. Product group, please validate the triage report (Microsoft internal access only, use Ctrl+Click to open in new tab).


Was this triage helpful? React with :thumbsup: or :thumbsdown: on this comment to provide feedback.

github-actions[bot] avatar Jul 05 '26 12:07 github-actions[bot]

Gentle follow-up on triage here. This was filed on 5 July as a re-submission of a fix that had already been reviewed in the retired pilot repository, and it still carries no Approved label.

The fix is ready on my side, covering the W1 report and the RU copy, with a regression test. Since a pull request needs an approved linked issue, I am holding it until this one is triaged. Is there anything else needed from me to help it along?

Franco111000 avatar Jul 26 '26 08:07 Franco111000

Hi @AndersLarsenMicrosoft! A gentle follow-up on this one, since you routed it to SCM back in July and it has been quiet since.

This was filed on 2026-07-05 as a re-submission of a fix that had already been reviewed in the BusinessCentralApps repository (issue #1542, PR #1908) before that repository was retired and its notice asked contributors to re-file here. Five weeks on it still carries only the SCM label, so it is waiting on a triage decision rather than on any work.

For context, the other three re-submissions from the same batch took different routes: #9099 and #9101 were approved on 2026-07-07 and are now open pull requests (#9180, which has since been reviewed and approved, and #9181), while #9102 is sitting in exactly the same state as this one.

The fix itself is already written and covers the RU copy of the report as well as the W1 one. I am not trying to rush anyone, I would just like to know whether this is still in the queue or whether it was set aside for a reason. If it needs re-scoping or more detail from my side to be decided on, I am glad to provide it.

Franco111000 avatar Aug 09 '26 10:08 Franco111000

Approved for contribution.

I confirmed that the defect is still present in both the W1 and RU copies of CalculateInventory.Report.al. The independent OR filters on "No." and "Table ID" form a cross-product, so an Item No. matching an unrelated Location Code can pull that Location's default dimensions into the physical inventory journal line.

The proposed fix from microsoft/BusinessCentralApps#1908 is appropriate: query the two intended (Table ID, No.) pairs separately - (Item, Item No.) and (Location, Location Code) - while preserving the existing dimension-buffer flow.

Please include:

  • The production fix in both W1 and RU report copies.
  • The regression test from the retired PR, verifying that the actual Location's default dimension is applied and the unrelated same-code Location's dimension is not applied.

Risk is low and localized, while the current behavior can silently produce incorrect dimensions on inventory adjustments.

PredragMaricic avatar Aug 24 '26 14:08 PredragMaricic

Thanks for the review and the approval @PredragMaricic, and for confirming the defect in both copies.

Pull request is up: #10575. It covers what you asked for:

  • The scoped (Table ID, No.) reads in both the W1 and the RU copy of CalculateInventory.Report.al, with the existing InsertDim and TempDimBufIn flow left as it is.
  • The regression test from the retired pull request, in codeunit 137275 "SCM Inventory Journals", asserting both directions: the counted Location's Default Dimension is applied, and the unrelated same-code Location's Default Dimension is not.

One note on the test: the setup now renames the Item and the unrelated Location to the same generated code that fits Location.Code by construction, rather than relying on the length the Item No. series happens to produce. The assertions are unchanged from the version reviewed in the pilot.

Franco111000 avatar Aug 24 '26 19:08 Franco111000