web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Problem with OnAfterValidateEven.

(3) ShareShare
ReportReport
Posted on by 9

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!

 
 
 
I have the same question (0)
  • Suggested answer
    RockwithNav Profile Picture
    8,335 Super User 2025 Season 2 on at
    Problem with OnAfterValidateEven.
    Did you try to enable a debugger and checker if you event is even getting triggered?
    I think issue lies with the event subscriber.
     
  • Verified answer
    Sagar Dangar, MCP Profile Picture
    783 Super User 2025 Season 2 on at
    Problem with OnAfterValidateEven.
    Hi,
     
    You can try another way by extending sales line table instead of event subscriber to fulfill your requirement. below code will trigger when you fill unit cost or unit price on sales line
     
     
    tableextension 50100 MyExtension extends "Sales Line"
    {
        fields
        {
            // Add changes to table fields here
            modify("Unit Cost")
            {
                trigger OnAfterValidate()
                begin
                    UpdateProfitPercentage();
                end;
            }
            modify("Unit Price")
            {
                trigger OnAfterValidate()
                begin
                    UpdateProfitPercentage();
                end;
            }
        }
        local procedure UpdateProfitPercentage()
        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;
    }

    }
  • Suggested answer
    YUN ZHU Profile Picture
    92,656 Super User 2025 Season 2 on at
    Problem with OnAfterValidateEven.
    Hi, I would like to add that the standard has a Profit % (67, Decimal) field, which just needs to be displayed. So this doesn't need to be customized.
     
    Thanks.
    ZHU

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 4,232

#2
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 2,863

#3
Sumit Singh Profile Picture

Sumit Singh 2,819

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans