web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Updating customize field in Purchase Order Header when line gets updated

(0) ShareShare
ReportReport
Posted on by 20

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

  • Suggested answer
    Nitin Verma Profile Picture
    21,812 Moderator on at

    Hi,

    Can you please share your code here in txt format?

    Thanks

  • jmtugade Profile Picture
    20 on at

    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;

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 796 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 481 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 377 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans