Here is my code on TableExtension of Purchase Order Line
//the computation will trigger when quantity field is modified
modify("Quantity")
{
trigger OnAfterValidate()
var
begin
ComputePurchaseInvoceTotals();
end;
}
//Computation
//last 3 fields are the fields I added on Purchase Header table
//it is working, but I want it to be a calculated fields instead of saving it to the header table because it will affect other scenarios
local procedure ComputePurchaseInvoceTotals()
var
PurchHeader: Record "Purchase Header";
PurchLine: Record "Purchase Line";
VatSetup: Record "VAT Posting Setup";
TotalExcGST: Decimal;
TotalGST: Decimal;
TotalInclGST: Decimal;
VatPercentage: Decimal;
Doctype: Enum "Purchase Document Type";
PurchHeaderStatus: Enum "Purchase Document Status";
begin
PurchHeader.SetFilter("No.", '%1', Rec."Document No.");
if PurchHeader.FindFirst() then begin
if (rec."Document Type" = Doctype::Order) and ((PurchHeader.Status = PurchHeaderStatus::Open) or (PurchHeader.Status = PurchHeaderStatus::Released)) then begin
PurchLine.Reset();
PurchLine.SetFilter("Document No.", '%1', Rec."Document No.");
if PurchLine.FindSet() then begin
repeat
if Rec."Line No." <> PurchLine."Line No." then //Exluding current data
begin
//Computing Total Exc GST AUD
TotalExcGST := TotalExcGST + (PurchLine."Qty. to Invoice" * PurchLine."Direct Unit Cost");
//TotalGST
VatSetup.SetFilter("VAT Prod. Posting Group", '%1', PurchLine."VAT Prod. Posting Group");
if VatSetup.FindFirst() then begin
if VatSetup."VAT %" <> 0 then begin
VatPercentage := VatSetup."VAT %" / 100;
end;
end;
TotalGST := TotalGST + ((PurchLine."Qty. to Invoice" * PurchLine."Direct Unit Cost") * VatPercentage);
end;
until PurchLine.Next() = 0;
end;
//-------Including current
TotalExcGST := TotalExcGST + (Rec."Qty. to Invoice" * Rec."Direct Unit Cost");
VatSetup.SetFilter("VAT Prod. Posting Group", '%1', Rec."VAT Prod. Posting Group");
if VatSetup.FindFirst() then begin
if VatSetup."VAT %" <> 0 then begin
VatPercentage := VatSetup."VAT %" / 100;
end;
end;
TotalGST := TotalGST + ((Rec."Qty. to Invoice" * Rec."Direct Unit Cost") * VatPercentage);
//------------------------
TotalInclGST := TotalExcGST + TotalGST;
//Modify the field INE_PIT_TotalExcGST,INE_PIT_TotalGST,INE_PIT_TotalInclGST in Purchase Header
PurchHeader.PIT_TotalExcGST := TotalExcGST;
PurchHeader.PIT_TotalGST := TotalGST;
PurchHeader.PIT_TotalInclGST := TotalInclGST;
PurchHeader.Modify();
end;
end;
end;