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

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

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

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

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.

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

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)
}

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

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

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!

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.

Harjo and jan,

It works like a charm!

Thanks you so much for the help!

Abrahim