Be careful when you want to update records in triggers, because I’ve noticed that Servoy doesn’t do any update, but Servoy doesn’t report either that update was not done.
This can happen in the following situation:
I have a foundset with salesorder which keeps the maximum sequence_nr used in the salesorderlines.
When adding a new salesorderline, I update the sequencenr in the header and then use this sequencenr in de the salesorderline:
_fs_salesorder.sequence_nr_line++;
_fs_salesorderline.newRecord()
_fs_salesorderline.sequence_nr = _fs_salesorder.sequence_nr_line;
_fs_salesorderline.article_id = _article_id;
_fs_salesorderline.amount = _amount
:
var _ok = databaseManager.saveData()
For the salesorderline table I have defined a table trigger (onInsert)
/**
* @param {JSRecord} record Record that will be inserted
*
* @returns {Boolean}
*/
function salesorderline_INSERT(record)
{
var _fs_header = record.salesorderline_to_salesorder;
_fs_header.total_amount_excl_vat += record.amount;
return true;
}
The above update of the header record (salesorder) is not done.
Even when adding the following lines of code, doesn’t help:
var _ok = databaseManager.saveData(_fs_header.getSelectedRecord())
if (! _ok)
return false;
The above update is not done , because in the main program the salesorder record was changed (update of the max sequence_nr)
And that is the reason that the update in the trigger is ignored.
So the only way I got it work, is the following:
_fs_salesorder.sequence_nr_line++;
var _ok = databaseManager.saveData(_fs_salesorder.getSelectedRecord())
if (! _ok)
return false;
So first write the max sequencenr to the database. Then Servoy accepts updating this record in the trigger.
It would have been nice if Servoy would have triggered an exception, because this was really hard to find.
Is this behavior intended to be like this, or is this an issue and should the salesorder record had been updated already in the first step?