[Bug]: Division in CalcCostPerUnit missing guard
@PredragMaricic is already working on this.
Since Aug 24, 2026.
- 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
- Ensure automatic cost adjustment is active.
- Create vendor with a currency code assigned that is equal to your local currency.
- Ensure the currency with exchange rate is 1, 1, 1, 1.
- Create an item and put it on inventory using a purchase order/invoice for that vendor
- Create a transfer order to transfer one or more of those items to another location.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.