Hi everyone,
I’m trying to add a custom field called "Profit" to the Sales Line table in Business Central 2023 Wave 2 On-Prem (BC 2023 W2). The field should calculate the profit percentage using the formula:
(("Unit Price" - "Unit Cost") / "Unit Price") * 100
********************************************************************
1* Create "Profit " field in the Sales Line table:
tableextension 50110 SalesLineExt extends "Sales Line"
{
fields
{
field(50100; "profit percentage"; Decimal)
{
Caption = 'Profit Percentage';
Editable = false;
}
}
}
2* Extend the Sales Line Subform page to show the field in the line.
pageextension 50110 SalesLinePageExt extends "Sales Order Subform"
{
layout
{
addlast(ControlGroup)
{
field("profit percentage"; "Profit")
{
ApplicationArea = All;
ToolTip = 'Displays the profit percentage for the sales line.';
}
}
}
}
3*add an Event Subscriber to update the field based on the Unit Price and Unit Cost.
codeunit 50110 SalesLineEvents
{
[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterValidateEvent', 'Unit Cost', true, true)]
local procedure UpdateProfitPercentage(var Rec: Record "Sales Line")
begin
if Rec."Unit Price" <> 0 then
Rec."profit percentage" := ((Rec."Unit Price" - Rec."Unit Cost") / Rec."Unit Price") * 100
else
Rec."profit percentage" := 0;
end;
}
*************************************************************
The "profit percentage" field is not being updated at all — no error messages, no change in value.
Has anyone experienced a similar issue in the on-prem environment? Is there something specific to BC On-Prem (2023 W2) that could prevent the event subscriber from firing or updating the field?
Any ideas or suggestions would be greatly appreciated.
Thanks in advance!