Table Events: Limitations

Is it OK to create related records in a table event?

Because I’ve tried it and I get strange behavior.
Essentially, the child record is created, but the save never happens.
So with auto save, every time i “click out”, the table event is run again, but the changes are never comitted. (The little ‘e’ in the status bar never goes away)

If I programmatically add controller.saveData() to my new record method, i get a concurrent modification exception.

What am i doing wrong, or is it just not going to work to have related records created in the table event? Here’s some example code.

Table Event Example Code:

//	The record argument 
var record = arguments[0];

// 	Rule: All parent records MUST have one CHILD record to start with
//	--------------------------------------------------
	
	
	//	create child record
	record.parents_to_children.newRecord();

//	--------------------------------------------------

//    passed all rules
return true;

Servoy Developer
Version 3.5-build 513
Java version 1.5.0_10-b03 (Windows XP)

What event exactly are you using?
I want to reproduce the behavior.

It is happening on INSERT events

i wanted to add a related record while inserting

i.e. record.relation.newRecord();

Hi Sean

I can confirm this, have the same behaviour.

I would like to check for mandatory fields in the table event onInsert, but I am unable to get a sensible result:

This is my code:

Code:
var record = arguments[0];

if (record.code == null || record.name == null)
{
plugins.dialogs.showErrorDialog( “Error”, “Code und Fach verlangen eine Eingabe”, ‘OK’);
}
else
{
plugins.dialogs.showErrorDialog( “Error”, “Prima”, ‘OK’);
forms.ScoSubjectsL.controller.readOnly = true;
}

If I enter only a value for code, I get the first time an error when saving (clicking out of the rec, i. e. autosave becomes active).
Then I click into the record.name field without entering anything and I the record is stored into the db, i. e. no error occurs, which is wrong (as far as I can see).
So I am able to store invalid records into the db. Can you confirm this as well?

Thanks and best regards, Robert

sean:
It is happening on INSERT events

i wanted to add a related record while inserting

i.e. record.relation.newRecord();

Robert, you’re not returning true or false, like in the sample code I provided you with in the other thread. If you do not return false, the insert is not canceled!!!

Paul

The data is not saved due to an internal Servoy problem. The behavior is not natural, so we have to treat this scenario properly, either by not supporting such actions inside the table events (and specifying it - treating it nicely) or by making it work.

Please file this issue at Servoy - Error.

Thanks.

Posted to Support System

The issue is corrected in Servoy 3.5.1 and 4.0.
The data is now saved in both the first table and the related table.

Enjoy :) .

Hi Paul, Andrei

Thanks, I added the return values.
The behaviour of this event is still very strange, we don’t get usable results. The problems so far found (autosave = on):

  • The onRecordInsert event is executed 5 times - why??? With this behaviour, it is not possible to put plugins.dialogs.xxx in this event as it is executed 5 times

  • If one inserts a record and uses a databaseManager.rollbackEditedRecords(), the selected record is always 1 off, as the onRecordSelection event is executed BEFORE the onRecordInsert event.

  • If there is an error and onRecordInsert is fired, the onRecordSelection event is always executed twice and shows once the index of the (newly) added record (which is still not saved as it has an error) and once the index of the clicked record (which triggers the onRecordInsert event, i. e. tries to autosave the record)

  • We think the solution could be if the onRecordInsert is fired after the onRecordSelection event, as we then would know the correct record index (we are though not exactly aware of all other possible implications)

  • The cmd forms[currentFormName].elements.fldCode.requestFocus() does not work

Conclusion: At the moment we can’t get the onRecordInsert to work as (we think) it is intended (without a lot of workaround programming).
And as it is called every time 5 times, we can’t put a dialog to inform the user about an error in it as it is executed 5 times anyway.

At the moment, we are stuck and don’t know exactly how to continue, good it’s weekend .-)

Best regards, Robert

Code of onActionAddRecord, i. e. button add record:
/*
Title: Create a new subject

Author: Robert Huber
Created: 16.08.2007
Modified: -

Arguments: -
Returns: -
Notes: -
*/

controller.newRecord();
setReadOnly(false);

Code of onRecordInsert:
/*
Title: Validate mandatory columns

Author: Robert Huber, Birgit Rieder
Created: 17.08.2007
Modified: -

Arguments: arguments[0] is record being processed
Returns: -
Notes: -
*/

var record = arguments[0];
var currentFormName = application.getMethodTriggerFormName();

if (record.code == null || record.code == “” || record.name == null || record.name == “”)
{
var dialogResult = plugins.dialogs.showErrorDialog( “Error”, “Code und Fach verlangen eine Eingabe”, ‘Abbrechen’, ‘Bearbeiten’);
if (dialogResult == “Abbrechen”)
{
forms[currentFormName].controller.readOnly = true;
databaseManager.rollbackEditedRecords();

// workaround to decrease the record index by one after the onRecordSelection event
// add a job that runs at the given date (20 seconds in the future)
// and repeats that every 20 seconds for 40 times or the enddate is reached (0 for no repeats = just one call)
var startDate = new Date();
startDate.setTime(startDate.getTime()+100);
var endDate = new Date(startDate.getTime()+100000);
plugins.scheduler.addJob(‘in2seconds’,startDate,setIndexMinusOne,20000,0,endDate)

}

// needs work (does not yet work):
// if (currentFormName != null)
// {
// forms[currentFormName].elements.fldCode.requestFocus();
// }
return false;
}
else
{
forms[currentFormName].controller.readOnly = true;
return true;
}

Code of onRecordSelection:
/*
Title: A new record is selected. If the form is in edit mode and the selected
record is not the edited record, reselect the edited record and do not
allow to select another record.

Author: Robert Huber, Birgit Rieder
Created: 17.08.2007
Modified: -

Arguments: -
Returns: -
Notes: -
*/

//application.output("selected index: " + forms.ScoSubjectsL.controller.getSelectedIndex());
//application.output("selected code: " + code);

if (isReadOnly() == false)
{
var selectedIndex = controller.getSelectedIndex();
if (selectedIndex != editIndex)
{
controller.setSelectedIndex(editIndex);
}
}

pbakker:
Robert, you’re not returning true or false, like in the sample code I provided you with in the other thread. If you do not return false, the insert is not canceled!!!

Paul

In the first problem on this thread (the solved one) - the method’s execution was interrupted in the moment a new record was created (and added to the edited record list) because the edited record list changed while Servoy was iterating on it to save records…

I think that in your case the problem might be the same (and thus it might be solved also)… “rollbackEditedRecords” probably changes the edited record list too and thus activates the same bug => the rest of the method is not executed any more and no save takes place. So anyway if it is the same problem, strange behavior can happen, as normal execution is interrupted by force…

I am not certain of this, so you can add what you reported in the previous post as another case in the support system + a sample solution to see exactly what is going on.

Hi
I have a solution where a dialog saves a new record when running locally in developer, however the same solution does not save a record from the last tabs create dialog.

However i have imported this same solution onto a hosted system running Gentoo linux and have a saving problem with a particaular dialog.

The problem:
I have modified the sample CRM application, and in this particular problem the last tab, 1 of 2 i added, on the company forms tabs , is not saving the record being created in the dialog.

Any suggestions?
Thanks
Marcus

Andrei Costescu:
In the first problem on this thread (the solved one) - the method’s execution was interrupted in the moment a new record was created (and added to the edited record list) because the edited record list changed while Servoy was iterating on it to save records…

I think that in your case the problem might be the same (and thus it might be solved also)… “rollbackEditedRecords” probably changes the edited record list too and thus activates the same bug => the rest of the method is not executed any more and no save takes place. So anyway if it is the same problem, strange behavior can happen, as normal execution is interrupted by force…

I am not certain of this, so you can add what you reported in the previous post as another case in the support system + a sample solution to see exactly what is going on.

Hi,

Your problem apparently has nothing to do with table events.
Please create another topic for this problem.

Thanks.

The reason i posted it here is that it seems to have a common thread the problem expressed in this thread. The problem being that records are not being save from a dialog, and a message pops up saying the the “data cannot be saved”.
Thought maybe the context would be considered.
I will repost to Forms thread.

Andrei Costescu:
Hi,

Your problem apparently has nothing to do with table events.
Please create another topic for this problem.

Thanks.