[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterModifyEvent', '', true, true)]
[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterInsertEvent', '', true, true)] var // Assign SLO variable to codeunit of SalesLineOperarions SLO: Codeunit SalesLineOperations; OrderTotal: decimal; begin // Check if this is a FREIGHT line if rec."No." = 'FCOM' then begin SLO.UpdateFCOMQty(rec); end; end;
procedure UpdateFCOMQty(var Rec: Record "Sales Line") // This is called whenever a sales line is modified or inserted // Checks if there's an FCOM record // If there is one, sum all the non-freight lines before tax and set this as QTY on FCOM var OrderTotal: decimal; FreightSalesLine: record "Sales Line"; begin // See if there are any FCOM lines. if so, calc total before tax and freight FreightSalesLine.SETRANGE("Document Type", Rec."Document Type"); FreightSalesLine.SETRANGE("Document No.", Rec."Document No."); FreightSalesLine.SETRANGE("No.", 'FCOM'); // If we have an FCOM line, calc the total of order minus freight If FreightSalesLine.Find('-') then begin OrderTotal := CalcOrderTotalWithoutTaxAndFreight(rec."Document No.", Rec."Document Type"); FreightSalesLine.Validate(Quantity, Round(OrderTotal, 1)); FreightSalesLine.Modify(TRUE); end; end;
Answers
Change parameters of CalcOrderTotalWithoutTaxAndFreight to a record var for your Rec Sales Line, and pass it. You can still do what you did in there before using "Document No" and "Document Type", but you can add values of your new record to the caculation of OrderTotal too.
I would personally stay away from database triggers, because it is unpredictable what happened when a line was inserted/modified. The user can move the cursor up and down when just selecting the line type, or just selecting the item number without having a quantity/uom/price. Any amount calculation will be false at that time anyway.
RIS Plus, LLC
Thanks again
Mark