Hi,
I extended a customized decimal field in Purchase Order Header named Total Inclusive GST. It's a running field and gets updated whenever a field in sales order line gets modified. I have achieved it by creating a procedure on OnAfterValidate trigger and saving the value. However, we encountered some error when posting the document saying that we cannot change the Purchase Header because it is already opened. I found out that PurhcaseHeader.Modify is being called many times including my procedure that's why the error is showing.
I want Total Inclusive GST to be updated without saving the value or make it like a Flowfield. but the value came from a computation. Is there a better way to achieve it without saving the Purchase Header? I can do an action button but its not a client option.
TIA,
JM
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;
Hi,
Can you please share your code here in txt format?
Thanks
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,188 Super User 2024 Season 2
Martin Dráb 230,030 Most Valuable Professional
nmaenpaa 101,156