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

Community site session details

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

Copying record from one table to another.

(0) ShareShare
ReportReport
Posted on by 358

I have a custom table that I want Posted Sales Invoice header records to be in. So, Sales Invoice Header table to myowntable.

Now, on the OnAfterInsert trigger of the Sales Invoice Header table I wrote the following code so that once a new record is created in the Sales Invoice Header table it get copied over to myowntable. All seems ok except that Amount and Amount Including VAT shows 0.00 in myowntable. All other fields i.e. No.,Sell-to Customer no., Due Date.. All gets copied expect Amount and Amount Including VAT.

 trigger OnAfterInsert()
    var
        recProof: Record "Proof of Delivery";
    begin
       
         with recProof do begin
        Evaluate(recProof."No.", Rec."No.");
        recProof."Sell-to Customer No." := Rec."Sell-to Customer No.";
        recProof."Due Date" := Rec."Due Date";
        recProof.Amount := Rec.Amount;
         recProof."Amount Including VAT" := Rec."Amount Including VAT";
        recProof."Currency Code" := Rec."Currency Code";
        recProof."Remaining Amount" := rec."Remaining Amount";
        recProof."Location Code" := rec."Location Code";
      Insert();
         end;
    end;
I have the same question (0)
  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,209 Super User 2025 Season 2 on at
    RE: Copying record from one table to another.

    Because the amount and amount including VAT are flowfield.

    Do calcfields before copying the amounts.

    CALCFIELDS("Amount","Amount Including VAT");

  • Verified answer
    Robert B. Profile Picture
    257 on at
    RE: Copying record from one table to another.

    Hi Mesam,

       Amount and "Amount Including VAT" are both flowfields, which means they don't store a value, but a formula and you need to use Calcfields function to get a value from these fields. 

       There are two options you have:

        1. declare those two fields in your table as decimals, run a Calcfields on them in the original table before copying over to your own.

        2. alternatively you can declare them as flowfields as well and calculate the value based on the original lines, or if you have your own copied lines table.

       Amount is calculated as: "CalcFormula = Sum ("Sales Invoice Line".Amount WHERE("Document No." = FIELD("No.")));"

       "Amount Including VAT" is calculated as: CalcFormula = Sum ("Sales Invoice Line"."Amount Including VAT" WHERE("Document No." = FIELD("No.")));

    All the best,

    Robert  

    Please verify my answer, if you find it useful, that way others can find this solution easier!

  • mysamza Profile Picture
    358 on at
    RE: Copying record from one table to another.

    Did not work.

  • mysamza Profile Picture
    358 on at
    RE: Copying record from one table to another.

    1) declare those two fields in your table as decimals, run a Calcfields on them in the original table before copying over to your own.

    My table has them both as Decimal and here is what I tried but it did not seem to work.

      trigger OnAfterInsert()
        var
            recProof: Record "Proof of Delivery";
        begin
            CALCFIELDS(Rec."Amount", Rec."Amount Including VAT");
            with recProof do begin
    
                Evaluate(recProof."No.", Rec."No.");
                recProof."Sell-to Customer No." := Rec."Sell-to Customer No.";
                recProof."Due Date" := Rec."Due Date";
                recProof.Amount := Rec.Amount;
                recProof."Amount Including VAT" := Rec."Amount Including VAT";
                recProof."Currency Code" := Rec."Currency Code";
                recProof."Remaining Amount" := rec."Remaining Amount";
                recProof."Location Code" := rec."Location Code";
                Insert();
            end;
        end;

  • Robert B. Profile Picture
    257 on at
    RE: Copying record from one table to another.

    That's because at this stage you have no lines created yet.

    So you'll have to use the second option, or if you want the real values do it on codeunit 80 trigger when the invoice gets posted.

  • Suggested answer
    Robert B. Profile Picture
    257 on at
    RE: Copying record from one table to another.

    Or if you want the real values subscribe to something like onafterpost (or similar) on codeunit 80 trigger after the invoice gets posted, then the lines are created as well.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
Sumit Singh Profile Picture

Sumit Singh 2,674

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 2,580

#3
YUN ZHU Profile Picture

YUN ZHU 2,115 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans