Skip to content

Add missing var modifier to OnCalculatePreciseCostAmountsOnAfterFilterOpenInboundItemLedgerEntry#9103

Open
Franco111000 wants to merge 1 commit into
microsoft:mainfrom
Franco111000:fix-7611-itemcostmgt-event-var
Open

Add missing var modifier to OnCalculatePreciseCostAmountsOnAfterFilterOpenInboundItemLedgerEntry#9103
Franco111000 wants to merge 1 commit into
microsoft:mainfrom
Franco111000:fix-7611-itemcostmgt-event-var

Conversation

@Franco111000

@Franco111000 Franco111000 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

What & why

The IntegrationEvent OnCalculatePreciseCostAmountsOnAfterFilterOpenInboundItemLedgerEntry in codeunit 5804 ItemCostManagement passes OpenInbndItemLedgerEntry by value. It is raised right after the filters are set on the open inbound item ledger entries and right before FindSet(), so its whole purpose is to let subscribers refine those filters. Because the record is passed by value, any filter a subscriber applies lands on a copy and is discarded, which makes the event unusable for the very scenario it was requested for (the original event request asked for "additional filters" on the open inbound entries).

This change adds the missing var modifier to the record parameter, in the W1 copy and in the byte-identical RU and APAC layer copies of the codeunit. All sibling events in the same procedure, and the directly analogous OnExcludeOpenOutbndCostsOnAfterOpenItemLedgEntrySetFilters filter hook in this codeunit, already pass their Item Ledger Entry records by reference; this event was the only exception.

A regression test is added in SCMAvgCostCalc.Codeunit.al (W1 and its CZ copy): a bound subscriber narrows the open inbound entries to a single entry and the test verifies the calculated average cost reflects only that entry. The test fails against the previous signature (the subscriber filter was ignored) and passes with this change.

Linked work

Fixes #7611

How I validated this

  • I read the full diff and it contains only changes I intended.
  • I built the affected app(s) locally with no new analyzer warnings.
  • I ran the change in Business Central and confirmed it behaves as expected.
  • I added or updated tests for the new behavior, or explained below why none are needed.

What I tested and the outcome

  • The production change is signature-only (one added var per layer copy); no executable statement changes. There are no subscribers to this event anywhere in this repository (verified by search), so no other code needed updating.
  • New test PreciseAvgCostRespectsSubscriberFilterOnOpenInboundEntries in codeunit 137070: mocks two open inbound entries (cost 6.00 over quantity 2 with remaining 1, and cost 10.00 plus rounding precision over quantity 2 with remaining 1) plus a closed outbound entry, so the total cost amount equals exactly the amount rounding precision and the precise cost path in CalculatePreciseCostAmounts is entered. A bound subscriber filters the open inbound entries down to the first entry. Expected average cost: 6.00 / 2 * 1 / 2 = 1.5. Without the fix the subscriber filter is discarded and the calculation yields 4.0025, so the test detects the defect.
  • I traced the full calculation path (CalculateAverageCost, ExcludeOpenOutbndCosts, CalculatePreciseCostAmounts) against the mocked data to confirm the expected value, and mirrored the test into the CZ copy of the test codeunit to keep the two files in lockstep. Building the Base Application locally is not feasible outside the CI baselines, so I rely on the pipeline for compilation and test execution.

Risk & compatibility

  • Why in place rather than a new event: the issue suggests a new event on the assumption that adding var is a breaking change. Adding a var modifier on an event parameter is exactly what AppSourceCop rule AS0077 covers, and this repository deliberately sets AS0077 to None on main (src/rulesets/ruleset.json: "Adding a var modifier in events should be allowed in main, as it only will break the runtime behavior of extensions subscribing to it when used in hotfix scenarios"), while minorrelease.ruleset.json makes it an Error on release branches. PR Fixes #3034 #3186 fixed the same class of defect (OnBeforeGetEmailAttachmentsByEmailScenarios) with the same in-place change. This PR therefore targets main only and should not be backported to releases/*.
  • Existing subscribers: none exist in this repository. External subscribers declared the parameter without var (the only compilable form until now); a subscriber may keep a by-value parameter against a var publisher parameter, so existing code keeps compiling and behaving as before. Subscribers that want the new capability opt in by declaring the parameter with var.
  • Scope of a subscriber filter after this change: a filter set in the event also carries into the open outbound scan in the same procedure, because OpenOutbndItemLedgEntry.CopyFilters(OpenInbndItemLedgEntry) runs after the event. For the intended slice-style filters (location, variant, custom entry attributes) this is the consistent behavior, since the same restriction then applies to both the inbound and outbound legs of the precise cost calculation. Subscribers that need different filters for the outbound leg can use the existing OnCalculatePreciseCostAmountsOnBeforeProcessOpenOutboundItemLedgerEntry event, which is raised between the two scans and already receives both records by reference.

…rOpenInboundItemLedgerEntry

The event passed the open inbound item ledger entry record by value, so
filters applied by subscribers were discarded before the FindSet call in
CalculatePreciseCostAmounts and the event could not fulfill its purpose.
The record is now passed by reference in the W1, RU, and APAC copies of
codeunit ItemCostManagement, matching the sibling events in the same
procedure. Adds a regression test with a bound subscriber that filters
the open inbound entries and verifies the average cost reflects the
filter.

Fixes microsoft#7611
@Franco111000 Franco111000 requested a review from a team July 5, 2026 15:27
@github-actions github-actions Bot added From Fork Pull request is coming from a fork needs-approval Workflow runs require maintainer approval to start labels Jul 5, 2026
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 needs-approval Workflow runs require maintainer approval to start

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Event OnCalculatePreciseCostAmountsOnAfterFilterOpenInboundItemLedgerEntry in codeunit ItemCostManagement

1 participant