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

How to Copy records from one table to another table

(0) ShareShare
ReportReport
Posted on by 67

Hi,

I have two tables, Table1 and DescTable selected with similar field

Now, once the record has been inserted in Table1, then a new record should be added in DescTable with the same primary key and Description of Table1.

procedure MyMethod()
    var
        DescTable: Record DescTable;
        Table1: Record Table1;
    begin
        Table1.reset;
        if Table1.FindSet() then begin
            DescTable.Setrange(DescTable.ID, Table1.ID);
            DescTable.Reset();
            DescTable.Init();
            DescTable.ID := Table1.ID;
            DescTable.Txt := Table1.NotesTxt;
            DescTable.Insert(true);
            Message('Description Added');
        end;
    end;

It only allows me to add one record, for the second record it says the record already exists in DescTable.

Can you tell me what I'm doing wrong?

Thanks.

Solution

procedure changeCode()
    var
        DescTable: Record DescTable;
        Table1: Record Table1;
    begin
        Table1.SETFILTER(Desc, '<> 0');
        IF Table1.FIND(' ') THEN
            repeat
            begin
                DescTable.Init();
                DescTable.ID := Table1.ID;
                DescTable.Desc := Table1.Desc;
                DescTable."new Field" := createguid;
                DescTable.Insert();
            end;
            until Table1.next <= 0;
    end;

 

I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,666 Moderator on at
    RE: How to Copy records from one table to another table

    Good to know that It helps, best of luck.

  • peopleeater Profile Picture
    67 on at
    RE: How to Copy records from one table to another table

    Hi, Nitin Verma,

    It is working now, I greatly appreciate your help.

    Thanks.

  • Suggested answer
    Nitin Verma Profile Picture
    21,666 Moderator on at
    RE: How to Copy records from one table to another table

    Yes,

    Transferfield method always work if we have same field's id and type in both the table in your case please use below

    Note: Please make sure you use Table1.ID once as it is a primary key in other table. otherwise you will face an error, I will suggest you create another one more field in the DesTable so that the new field always have unique value. like below

    procedure MyMethod()
    var
    DescTable: Record DescTable;
    Table1: Record Table1;
    begin
    Table1.reset;
    if Table1.FindSet() then begin
    DescTable.Init();
    DescTable.ID := Table1.ID;
    DescTable.Txt := Table1.NotesTxt;

    DescTable."new Field" := createguid

    DescTable.Insert(true);
    Message('Description Added');
    end;
    end;

  • peopleeater Profile Picture
    67 on at
    RE: How to Copy records from one table to another table

    Hi, Nitin Verma

    Thanks for replying, I'm still getting a error because using transferfields() only working if I have the same no of fields in my both table with same type. But I have a lot of fields in my Table1 and only have two fields in my DescTable, I want to copy both ID and description in my DescTable from Table1, what method should i use then?

    Thanks

  • Suggested answer
    Nitin Verma Profile Picture
    21,666 Moderator on at
    RE: How to Copy records from one table to another table

    Hi,

    Please use the below code.

    procedure MyMethod()
    var
    DescTable: Record DescTable;
    Table1: Record Table1;
    begin
    Table1.reset;
    if Table1.FindSet() then begin
    DescTable.Init();
    DescTable.trarnsferfields(Table1);
    DescTable.Insert(true);
    Message('Description Added');
    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

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
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 4,304

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 4,045

#3
Sumit Singh Profile Picture

Sumit Singh 2,970

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans