Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Updating customize field in Purchase Order Header when line gets updated

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

Categories:
  • jmtugade Profile Picture
    jmtugade 20 on at
    RE: Updating customize field in Purchase Order Header when line gets updated

    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;

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Updating customize field in Purchase Order Header when line gets updated

    Hi,

    Can you please share your code here in txt format?

    Thanks

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,188 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,030 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans