clone a record object

var frmObj = forms.contact$list;

var record = frmObj.foundset.getRecord(frmObj.controller.getSelectedIndex());

how I can clone a record object.

something like

var recordClone = record.clone();

The record object is cloned.
Then the actual record is modified
Then I want to compare the modifications with cloned object.

Any advice would be highly appreciated.

Thanks

You can do like this:

var clonedRecord = [form]foundset.getRecord(index)

and then compare all changes like this:

if( [form]column1 == clonedRecord.column1)
 { do stuff }
 else
 { do different stuff }

Hope you get the picture.

I tried this method already.

The getRecord() method just gives you a reference to a particular record (as all the normal objects in java do) and not cloning it.

So if I change anything in the record object, it is affected in the underlying database record and the vice versa.

So I tried to write the following method:
It worked for me. But I am not sure this is the correct way of doing it.

var sourceObj = arguments[0];
var clonedObj = new Object();

for (var i in sourceObj) {
   clonedObj[i] = sourceObj[i];
}
return clonedObj;

Any more suggestions please?

ngervasi:

var clonedRecord = [form]foundset.getRecord(index)

That won’t give you a copy of the record but a reference to that record.

I guess you could do it this way (untested code):

var rcSource = foundset.getRecord(index);
foundset.newRecord(true,true);
var rcTarget = foundset.getRecord(foundset.getSelectedIndex());
var bSuccess = databaseManager.copyMatchingColumns( rcSource,  rcTarget);

Hope this helps.

I see now that you wanted to have a copy of the record in memory for tracking changes and not copy a record to a new record. I guess you have to do that yourself like you already posted.
You could make it dynamic by using foundset.alldataproviders so that you don’t have to hardcode the columnnames.

Hope this helps.

faheemhameed:
I tried this method already.

The getRecord() method just gives you a reference to a particular record (as all the normal objects in java do) and not cloning it.

So if I change anything in the record object, it is affected in the underlying database record and the vice versa.

So I tried to write the following method:
It worked for me. But I am not sure this is the correct way of doing it.

var sourceObj = arguments[0];

var clonedObj = new Object();

for (var i in sourceObj) {
clonedObj[i] = sourceObj[i];
}
return clonedObj;




Any more suggestions please?

Sorry, I misunderstood your question, I guess you’re doing the right thing.
A hint: you can save the Object() in a MEDIA field if you want to retrieve it from a different client or after the client is closed.

that’s a very useful hint. Thanks a lot!

There will be instances where I need to use this hint.

You have to be aware of the fact that this record variable wherever you put ir will not be valid forever. If you make changes to the foundset (at least), your variable “is dead”.

Thanks for the advice. Much appreciated.

In my method I am not changing my foundset. So I am pretty safe and it works for me well.