Old values in database and new values on screen

Hello,

I’m facing the following situation. In a script I need to know the old value of a field during the script I’m using when updating my database.
For example:

Order Quantity


1…10
2…10
3…10

The above values are in my database.
Now I have a form that shows these records in table view

The AutoSave() is false.
I change the values in my form:

Order Quantity


1…8
2…10
3…13

Now I’ll press a button and need to update some tables with the differences. This button will launch a script that contains the following:

databaseManager.startTransaction()
forms[_form].myMethod // this method needs to update some tables using the differences
forms[_form].controller.saveData();
databaseManager.commitTransaction()

To keep the old value in some global variable, could be a solution, but I can’t do this. In the OnRecordSelection it is not possible because onRecordSelection is triggered each time I select a record and I only need the first time when a record is loaded.

I also tried to put an Event to the onUpdate of the table, but this method only uses the current record as argument and not the original record.

Anyone has a suggestion?

Martin

I only need the first time when a record is loaded

Create a variable that tells you this was a first record selection I guess…

Or, create the global on startup. Make the default value null and do a check. When the value is null you will add the ‘first value’ when not null you skip it…

Or, when this is something you nee frequently for different tables (and maybe cross-different-users) you could create a temp table with values…

The best solution would be that each field has a property ‘OldValue’.
I’ll need this very often in several tables.
When Servoy loads the records, it can fill this OldValue property and it will be a readonly property
I (and I think many people) will often need the difference between an oldvalue and the new value to make updates

Using the variable doesn’t seem a good solution because I need to have the old values for all the records in the tableview and not only the selected one. So if have to use a ‘firsttime’ variable, it needs to work on firsttime selection per record and not per form.

And temporary tables doesn’t seem to be a good solution. I only use temporary tables in real complex situations when I don’t have any choice. But this case is not complex.

martinh:
The best solution would be that each field has a property ‘OldValue’.

In a sense Servoy already has this. When you attach a method on the onDataChange event of a field then argument[0] will hold the old value.

Hope this helps.

You are right about that, but when the orginal value = 10 and I change it to 9 and then from 9 to 8 and then I press my Save button (to write data to the database), then the second DataChange will give me 9 as original value and not 10

Servoy allready has all this info cached for you:

  • databaseManager.getchangedRecordData(record)
  • databaseManager.getEditedRecords()
  • hasRecordChanges(foundset/record, [foundsetindex])

Paul

Thanks Paul,

This is what I was looking for:

  • databaseManager.getchangedRecordData(record)
var array = databaseManager.getEditedRecords()
for( var i = 0 ; i < array.length ; i++ )
{
var record = array[i];
var dataset = databaseManager.getChangedRecordData(record)
for( var i = 1 ; i <= dataset.getMaxRowIndex() ; i++ )
{
application.output(dataset.getValue(i,1) +' '+
dataset.getValue(i,2) +' '+ dataset.getValue(i,3));
}
}

dataset.getValue(i,1) is the Fieldname
dataset.getValue(i,2) is the Oldvalue
dataset.getValue(i,3) is the Newvalue