Update records using table triggers

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?

calling saveData() in a table event method is of no use. Because that would be a circular call (it is busy doing a saveData())

But as far as i can see we do try to save it all, even records that are again touched inside table event methods.
So can you create a sample and attach it to a case? Also in 6 this code is changed quite a bit, so maybe you can also test it against 6 to see if it works better there for you?

jcompagner:
Also in 6 this code is changed quite a bit, so maybe you can also test it against 6 to see if it works better there for you?

Hi Johan what do you mean with “code is changed”?

the code in servoy that handles editing records and table triggers.
That code is changed quite a bit from 5 to 6.

So it could be that this is already fixed in 6 for you, that is handy to know.