Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Unanswered

Sales line margin and markup recalculation

Posted on by 37

Hi folks,

I'm extending the Sales Line table and Sales Quote Subform to add a couple of fields - Markup % and Margin %.

I'd like to have those fields recalculated if the "Unit Price" or "Unit Cost (LCY)" are updated by the Sales staff. They'd also like to be able to manually change the Markup % and have the Unit Price recalculate automatically.

I've pasted in the extension code I have at the moment - does anyone please have any advice to set up the triggers to recalculate properly? Currently, the markup is recalculating whenever I move from the field, based on the unit price/cost, but if I manually change the Markup it doesn't update the Unit Price, it just recalculates the Markup %... I know I'm probably doing something wrong here  I'm fairly new to AL code, so any help is much appreciated.

I'm getting a conflict trying to set up the base application fields to have onvalidate triggers - if there is a better approach to do this, please let me know.

tableextension 50100 "Sales Line Extension" extends "Sales Line"
{
    fields
    {
        field(50101; "Margin %"; Decimal)
        {
            Caption = 'Margin %';
            DecimalPlaces = 0 : 2;
            Editable = false;
        }

        field(50102; "Markup %"; Decimal)
        {
            Caption = 'Markup %';
            DecimalPlaces = 0 : 2;
            Editable = true;
        }
    }
}

pageextension 50111 SalesQuoteLine extends "Sales Quote Subform"
{
    layout
    {
        addafter("Unit Cost (LCY)")
        {
            field("Margin %"; Rec."Margin %")
            {
                ApplicationArea = All;
            }
            field("Markup %"; Rec."Markup %")
            {
                ApplicationArea = All;

                trigger OnValidate()
                var
                    MarkupPercentage: Decimal;
                begin
                    if Rec."Unit Cost (LCY)" <> 0 then
                    begin
                        MarkupPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Cost (LCY)") * 100;
                        Rec."Markup %" := MarkupPercentage;
                    end
                    else
                        Rec."Markup %" := 0;

                    if Rec."Markup %" <> 0 then
                        Rec."Unit Price" := Rec."Unit Cost (LCY)" * (1   Rec."Markup %" / 100)
                    else
                        Rec."Unit Price" := Rec."Unit Cost (LCY)";
                   
                    Rec."Margin %" := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Price") * 100;
                end;
            }

            field("Unit Price"; Rec."Unit Price")
            {
                ApplicationArea = All;

                trigger OnValidate()
                var
                    MarginPercentage: Decimal;
                    MarkupPercentage: Decimal;
                begin
                    if Rec."Unit Price" <> 0 then
                        MarkupPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Cost (LCY)") * 100
                    else
                        MarkupPercentage := 0;
                   
                    Rec."Markup %" := MarkupPercentage;

                    if Rec."Unit Price" <> 0 then
                        MarginPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Price") * 100
                    else
                        MarginPercentage := 0;
                   
                    Rec."Margin %" := MarginPercentage;
                end;
            }
           
            field("Unit Cost (LCY)"; Rec."Unit Cost (LCY)")
            {
                ApplicationArea = All;

                trigger OnValidate()
                var
                    MarginPercentage: Decimal;
                    MarkupPercentage: Decimal;
                begin
                    if Rec."Unit Price" <> 0 then
                        MarkupPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Cost (LCY)") * 100
                    else
                        MarkupPercentage := 0;
                   
                    Rec."Markup %" := MarkupPercentage;

                    if Rec."Unit Price" <> 0 then
                        MarginPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Price") * 100
                    else
                        MarginPercentage := 0;
                   
                    Rec."Margin %" := MarginPercentage;
                end;
            }
        }
    }
}

Categories:
  • Hamish Duff Profile Picture
    Hamish Duff 37 on at
    RE: Sales line margin and markup recalculation

    I might have answered this myself - it is recalculating correctly, however I am still keen to know if this the right approach to take with triggering the recalculation, and if my manual calculation will cause any issues with the logic of the page. Here's the updated code:

    tableextension 50100 "Sales Line Extension" extends "Sales Line"

    {

       fields

       {

           field(50101; "Margin %"; Decimal)

           {

               Caption = 'Margin %';

               DecimalPlaces = 0 : 2;

               Editable = false;

           }

           field(50102; "Markup %"; Decimal)

           {

               Caption = 'Markup %';

               DecimalPlaces = 0 : 2;

               Editable = true;

               trigger OnValidate()

               begin

                   if Rec."Markup %" <> 0 then

                       Rec."Unit Price" := Rec."Unit Cost (LCY)" * (1 + Rec."Markup %" / 100)

                   else

                       Rec."Unit Price" := Rec."Unit Cost (LCY)";

                   if Rec."Unit Price" <> 0 then

                       Rec."Margin %" := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Price") * 100

                   else

                       Rec."Margin %" := 0;

                   if Rec."Line Amount" <>0 then

                       Rec."Line Amount" := Rec."Quantity" * Rec."Unit Price"

               end;

           }

           modify("Unit Cost (LCY)")

           {

               trigger OnAfterValidate()

                   var

                       MarginPercentage: Decimal;

                       MarkupPercentage: Decimal;

                   begin

                       if Rec."Unit Price" <> 0 then

                           MarkupPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Cost (LCY)") * 100

                       else

                           MarkupPercentage := 0;

                       Rec."Markup %" := MarkupPercentage;

                       if Rec."Unit Price" <> 0 then

                           MarginPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Price") * 100

                       else

                           MarginPercentage := 0;

                       Rec."Margin %" := MarginPercentage;

                   end;

           }

           modify("Unit Price")

           {

               trigger OnAfterValidate()

                   var

                       MarginPercentage: Decimal;

                       MarkupPercentage: Decimal;

                   begin

                       if Rec."Unit Price" <> 0 then

                           MarkupPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Cost (LCY)") * 100

                       else

                           MarkupPercentage := 0;

                       Rec."Markup %" := MarkupPercentage;

                       if Rec."Unit Price" <> 0 then

                           MarginPercentage := ((Rec."Unit Price" - Rec."Unit Cost (LCY)") / Rec."Unit Price") * 100

                       else

                           MarginPercentage := 0;

                       Rec."Margin %" := MarginPercentage;

                   end;

               }

       }

    }

    pageextension 50111 SalesQuoteLine extends "Sales Quote Subform"

    {

       layout

       {

           addafter("Unit Cost (LCY)")

           {

               field("Margin %"; Rec."Margin %")

               {

                   ApplicationArea = All;

               }

               field("Markup %"; Rec."Markup %")

               {

                   ApplicationArea = All;

               }

           }

       }

    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans