# Create a new record in table A and save it in Table B

**URL:** <https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519>\
**Category:** Classic Servoy\
**Created:** [February 23, 2006, 6:58pm UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519 "2006-02-23T18:58:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![akalehzan](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@akalehzan](https://forum.servoy.com/u/akalehzan)\
**Post date:** [February 23, 2006, 6:58pm UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/1 "2006-02-23T18:58:31Z")

</div>

Hi all,

Lets say we have two tables A and B.  
Table A has the same column as B does.

Once users create a new record in table A and click on save record, I like the same record get created and saved in the table B.

What is the nest way to do that?

Thanks in advance.

Abrahim

---

<div class="post-metadata">

**Author:** ![Westy](https://avatars.discourse-cdn.com/v4/letter/w/9de053/32.png) [@Westy](https://forum.servoy.com/u/Westy)\
**Post date:** [February 28, 2006, 11:11pm UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/2 "2006-02-28T23:11:33Z")

</div>

Create a relation between table A and table B named **a\_to\_b** that has the primary key in table A equal to the foreign key in table B. Create a form based on table A. On the form place a “Save” button with the following method:

controller.saveData()  
**a\_to\_b**.newRecord();  
**a\_to\_b**.column1 = column1

Dean Westover

---

<div class="post-metadata">

**Author:** ![akalehzan](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@akalehzan](https://forum.servoy.com/u/akalehzan)\
**Post date:** [February 28, 2006, 11:41pm UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/3 "2006-02-28T23:41:34Z")

</div>

Dean,

I was hoping that there was a faster way to do this, since most of our tables have around 100 to 150 columns.

Thanks!

Abrahim

---

<div class="post-metadata">

**Author:** ![Jan\_Aleman](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jan\_Aleman](https://forum.servoy.com/u/Jan_Aleman)\
**Post date:** [March 1, 2006, 12:06am UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/4 "2006-03-01T00:06:16Z")

</div>

In the method editor have a look at the Database Manager node and then the function copyMatchingColumns. I believe it does what you are trying to accomplish.

---

<div class="post-metadata">

**Author:** ![akalehzan](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@akalehzan](https://forum.servoy.com/u/akalehzan)\
**Post date:** [March 7, 2006, 3:45am UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/5 "2006-03-07T03:45:32Z")

</div>

Jan,

It is not clear to me that what I need to put for Source and Dest Record in function copyMatchingColumns. Do you have any example?

databaseManager.copyMatchingColumns(srcRecord,destRecord [,overwrite])

Thanks,

Abrahim

---

<div class="post-metadata">

**Author:** ![Jan\_Blok](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jan\_Blok](https://forum.servoy.com/u/Jan_Blok)\
**Post date:** [March 7, 2006, 9:59am UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/6 "2006-03-07T09:59:40Z")

</div>

better sample:

//Copies all matching non empty columns (if overwrite boolean is given all columns except pk/ident, if array then all columns except pk and array names), returns true if no error did happen  
for( var i = 1 ; i \<= foundset.getSize() ; i++ )  
{  
var srcRecord = foundset.getRecord(i);  
var destRecord = otherfoundset.getRecord(i);  
if (srcRecord == null || destRecord == null) break;  
databaseManager.copyMatchingColumns(srcRecord,destRecord,true)  
}

---

<div class="post-metadata">

**Author:** ![akalehzan](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@akalehzan](https://forum.servoy.com/u/akalehzan)\
**Post date:** [March 7, 2006, 10:33pm UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/7 "2006-03-07T22:33:10Z")

</div>

Jan,

Tried the following, but nothing happened:

for( var i = 1 ; i \<= foundset.getSize() ; i++ )  
{  
var srcRecord = forms.ABC\_Compare.foundset.getRecord(i); //src foundset

var destRecord = forms.abc\_1.foundset.getRecord(i); //dest foundset  
if (srcRecord == null || destRecord == null) break;  
databaseManager.copyMatchingColumns(srcRecord,destRecord,true)  
}

//Save any outstanding change to dest foundset  
forms.abc\_1.controller.saveData();

Any advice?

Thanks,

Abrahim

---

<div class="post-metadata">

**Author:** ![Jan\_Blok](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jan\_Blok](https://forum.servoy.com/u/Jan_Blok)\
**Post date:** [March 7, 2006, 11:42pm UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/8 "2006-03-07T23:42:08Z")

</div>

In the “for” you are looping over the current form foundset, but take all records form other foundsets??

---

<div class="post-metadata">

**Author:** ![Harjo](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/harjo/32/4873_2.png) [@Harjo](https://forum.servoy.com/u/Harjo)\
**Post date:** [March 8, 2006, 8:39am UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/9 "2006-03-08T08:39:51Z")

</div>

The method Jan,suggest, does copy matching columns if you have a record on both sides.

It seems to me that the destination, has not any records at all?  
You have to create them first!

```auto
for( var i = 1 ; i <= foundset.getSize() ; i++ )
{
var srcRecord = forms.ABC_Compare.foundset.getRecord(i); //src foundset

var destRecord = forms.abc_1.foundset.getRecord(forms.abc_1.foundset.newRecord()); //dest foundset
if (srcRecord == null || destRecord == null) break;
databaseManager.copyMatchingColumns(srcRecord,destRecord,true)
}

//Save ANY outstanding change to dest foundset
controller.saveData();

```

This will CREATE and COPY the records.

---

<div class="post-metadata">

**Author:** ![akalehzan](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@akalehzan](https://forum.servoy.com/u/akalehzan)\
**Post date:** [March 8, 2006, 8:51pm UTC](https://forum.servoy.com/t/create-a-new-record-in-table-a-and-save-it-in-table-b/5519/10 "2006-03-08T20:51:34Z")

</div>

Harjo and jan,

It works like a charm!

Thanks you so much for the help!

Abrahim
