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; } } } }