microsoft / microsoft/BCApps

[Bug]: Division in CalcCostPerUnit missing guard

Open
#10,132 2 comments 0 reactions 1 assignee View on GitHub

@PredragMaricic is already working on this.

Since Aug 24, 2026.

Approved Team: SCM
Dominant language
AL
Stars
683
Forks
459
Avg merge
3d 26m
Merged PRs (30d)
633

Description

Describe the issue

In procedure CalcCostPerUnit in codeunit 5895 "Inventory Adjustment" implements "Inventory Adjustment", "Cost Adjustment With Params", there are two divisions being done.

OutbndValueEntry."Cost per Unit" := OutbndCostElementBuf."Actual Cost" / (ItemLedgEntryQty - OutbndCostElementBuf."Remaining Quantity");
OutbndValueEntry."Cost per Unit (ACY)" := OutbndCostElementBuf."Actual Cost (ACY)" / (ItemLedgEntryQty - OutbndCostElementBuf."Remaining Quantity");

With certain misconfigurations, it is possible to trigger a situation where ItemLedgEntryQty - OutbndCostElementBuf."Remaining Quantity" results in zero, leading to a divide by zero error, which is not very instructive

Expected behavior

I would expect a guard clause to catch this situation and handle it. However, I am not sufficiently versed in this part of the financials to offhand say whether this should throw an error (and which error text it should then give) or simply exit the procedure.

In any case, we should have a guard that along the lines of:

if ItemLedgEntryQty - OutbndCostElementBuf."Remaining Quantity" = 0 then
    /// Handle zero result
Steps to reproduce

Note: this is a configuration that should not happen

  1. Ensure automatic cost adjustment is active.
  2. Create vendor with a currency code assigned that is equal to your local currency.
  3. Ensure the currency with exchange rate is 1, 1, 1, 1.
  4. Create an item and put it on inventory using a purchase order/invoice for that vendor
  5. Create a transfer order to transfer one or more of those items to another location.
  6. Post shipment on the transfer.
Additional context

I am happy to provide a fix, but I will need more feedback on what the preferred direction would be.

I will provide a fix for a bug
  • I will provide a fix for a bug

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.