Skip to main content
Business Central forum
Suggested answer

Inserting the Record in the table from another table.

editSubscribe (1) ShareShare
ReportReport
Posted on by 2,191 Super User

So I have a table and I want to insert and remove the records from another table based on the condition.

    trigger OnOpenPage()
    begin
        insertData();
        // Rec.Reset();
        // if (Rec.FindLast() = true) then
        //     exit
        // else
        //     insertData();
    end;
    
    local procedure insertData()
    var
        uerBatchTable: Record uerDistributionBatchTable;
    begin
        uerBatchTable.Reset();
        uerBatchTable.FindSet();
        repeat begin
            if (uerBatchTable.Stop = false) then begin
                Rec.Init();
                Rec.RowNo := Rec.RowNo   1;
                Rec.uerDistributionPoolBatchID := uerBatchTable.uerDistributionPoolBatchID;
                Rec.uerDistributionPoolID := uerBatchTable.uerDistributionPoolID;
                Rec.uerDistributionDate := uerBatchTable.uerDistributionDate;
                Rec.uerDescription := uerBatchTable.uerDescription;
                Rec.uerAmountToAllocate := 0;
                Rec.uerOrigAmountToAllocate := uerBatchTable.uerDistributionAmt;
                Rec.uerPreviousAmountToAllocate := 0;
                Rec.uerAmountNotDistributed := (Rec.uerOrigAmountToAllocate - 0);
                Rec.Insert(true);
            end;
        end;
        until uerBatchTable.Next() = 0;

The first time when I open the page it inserts the records after re-loading again or modifying the condition it gives an error.

pastedimage1666785677017v1.png

Kindly help.

Regards

Categories:
Attachments
  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,180 Super User on at
    RE: Inserting the Record in the table from another table.

    Hi

    my counter would work if your page was temporary:

    so either set the page property TEMPORARY=TRUE

    o

    check which row line is the last one you inserted in the table:

    rec.findlast;

    counter:=Rec.RowNo;

    if (uerBatchTable.Stop = false) then begin

                  counter += 1;

                  Rec.Init();

                  Rec.RowNo := counter

    DAniele

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 59,978 Super User on at
    RE: Inserting the Record in the table from another table.

    Hi, if you don't use a temporary table, I think you should add a condition. You must first determine whether this record already exists in your table. If it does not exist, insert it, and skip it if it exists.

    Hope this helps as well

    Thanks.

    ZHU

  • Suggested answer
    Amit_Sharma Profile Picture
    Amit_Sharma 2,545 on at
    RE: Inserting the Record in the table from another table.

    Hi,

    Use Variable instead of REC and then find the last entry no and then increment that last entry number and then insert the recod.

    Regards

    Amit Sharma

    www.erpconsultors.com

  • Govinda Kumar Profile Picture
    Govinda Kumar 2,191 Super User on at
    RE: Inserting the Record in the table from another table.

    Hey Daniele Incalza,

    Thank you for replying...

    No, my table is not TmpTable and your separate variable for count is not working. As I have tried both ways..

    OnOpenPage trigger first emptying the table and then inserting the data works fine but I want to solve it with the condition.

    Regards.

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,180 Super User on at
    RE: Inserting the Record in the table from another table.

    Ciao

    l'errore è il seguente;

    quando fai la INIT del Rec, la tua Rec.RowNo viene resettata a 0. e quindi viene inserita sempre 1.

    provate così, assicurandovi che la PAge sia sempre Temporary=TRUE

       trigger OnOpenPage()

       begin

           insertData();

           // Rec.Reset();

           // if (Rec.FindLast() = true) then

           //     exit

           // else

           //     insertData();

       end;

       local procedure insertData()

       var

           uerBatchTable: Record uerDistributionBatchTable;

           counter: integer;

       begin

           uerBatchTable.Reset();

           uerBatchTable.FindSet();

           repeat begin

               if (uerBatchTable.Stop = false) then begin

                   counter += 1;

                   Rec.Init();

                   Rec.RowNo := counter;

                   Rec.uerDistributionPoolBatchID := uerBatchTable.uerDistributionPoolBatchID;

                   Rec.uerDistributionPoolID := uerBatchTable.uerDistributionPoolID;

                   Rec.uerDistributionDate := uerBatchTable.uerDistributionDate;

                   Rec.uerDescription := uerBatchTable.uerDescription;

                   Rec.uerAmountToAllocate := 0;

                   Rec.uerOrigAmountToAllocate := uerBatchTable.uerDistributionAmt;

                   Rec.uerPreviousAmountToAllocate := 0;

                   Rec.uerAmountNotDistributed := (Rec.uerOrigAmountToAllocate - 0);

                   Rec.Insert(true);

               end;

           end;

           until uerBatchTable.Next() = 0;

    BYe

    DAniele

    check my answer if it solves your problem

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,180 Super User on at
    RE: Inserting the Record in the table from another table.

    HI

    the page is Temporary=YES?

    Verify it

    check my answer if it solves your problem

    DAniele

Helpful resources

Quick Links

New Blog Features Released!

Check out the new community blog features for viewers and authors…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,080 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,601 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Featured topics

Product updates

Dynamics 365 release plans