Creating newrecord

So this one is costing me to much hours.

Please explain to me why this code is crashing (freezing)

controller.newRecord();
controller.saveData();
var ThisTemplateID = idtemplates
for ( var i = 1 ; i <= 5 ; i++ )// dagen zijn genummerd van 1 tot 5
	{
	forms.tab_templ_dag.controller.newRecord();
	forms.tab_templ_dag.templ_dagnummer = i;
	forms.tab_templ_dag.templates_idtemplates = ThisTemplateID;
	forms.tab_templ_dag.controller.saveData();
	forms.tab_templ_dag.controller.loadAllRecords()
	forms.tab_templ_dag.controller.setSelectedIndex(forms.tab_templ_dag.controller.getMaxRecordIndex());
	var ThisDayID = forms.tab_templ_dag.idtempl_dagen;
	application.output(ThisDayID)
	for ( var j = 0 ; j <= 7 ; j++ )// dagdelen gaan van 0 tot 7
		{
		forms.tab_templ_dagdelen.controller.newRecord();
		forms.tab_templ_dagdelen.type_dagdeel = j;	
		forms.tab_templ_dagdelen.templates_dagdelen_idtempl_dagen = ThisDayID;
		forms.tab_templ_dagdelen.controller.saveData();
		}
			
	}

it freezes on the last line.(forms.tab_templ_dagdelen.controller.saveData():wink:

Situation: Templates are weeks ; on creating a new week I need to create the days of the week (no problem here). Each weekday consists of several (8) dayparts.

Help VERY much appreciated

a more complete solution is needed.
Maybe some other methods are triggered the moment you call savedata ect. It is very hard to say by just looking at that method (that one seems ok)
Why aren;t you doing these things throug related foundsets?
(so not controller.newRecord but foundset.newRecord ect)
and why are you calling for the days saveData then loadAllRecords and then setting the selected to the max?

Send you the solution; Hope you can make something out of it

Why aren;t you doing these things throug related foundsets?
(so not controller.newRecord but foundset.newRecord ect)

Probably Ignorance & Confusion.

Reasons:

  1. I know about controller.newRecord is selfexplanotory.and is being matched with controller.saverecord

  2. Only when the trouble started I found out about foundset.newRecord but found it confusing. Whats the difference with controller.newRecord and why is there no matching foundset.saveRecord.

  3. Looking at the forum I found somewhere that controller.saverecord saves any outstanding changes. I interpret this as any call to saverecord will save outstanding changes for all controllers. But then I thought why isn’t it called application.saveRecord. Confusion rises. Maybe it saves all outstanding changes for the backend table that the controller is based upon? Seems logical . Still share your wisdom please :?

  4. I did make a version of the script that worked over relations (started a version to be more precise). Worked fine for the first level.(from Template level to Days, didn’t work for second level from days to dayparts). Same problem as I’m having now

why are you calling for the days saveData then loadAllRecords and then setting the selected to the max?

Probably Confusion and Ignorance :?

I didnt at first. This is what I had:

controller.newRecord();
controller.saveData();
var ThisTemplateID = idtemplates
for ( var i = 1 ; i <= 5 ; i++ )// dagen zijn genummerd van 1 tot 5
{
forms.tab_templ_dag.controller.newRecord();
forms.tab_templ_dag.templ_dagnummer = i;
forms.tab_templ_dag.templates_idtemplates = ThisTemplateID;
forms.tab_templ_dag.controller.saveData();
var ThisDayID = forms.tab_templ_dag.idtempl_dagen;
application.output(ThisDayID)

for ( var j = 0 ; j <= 7 ; j++ )// dagdelen gaan van 0 tot 7
{
forms.tab_templ_dagdelen.controller.newRecord();
forms.tab_templ_dagdelen.type_dagdeel = j;
forms.tab_templ_dagdelen.templates_dagdelen_idtempl_dagen = ThisDayID;
forms.tab_templ_dagdelen.controller.saveData();
}

}

But then ThisDayID gets the value for the first record created and doesn’t update in the next iteration so it KEEPS the value of the first record (even when in iteration 2,3,4,5)

Please tell me what I’m doing wrong

1> controller.newRecord does exactly the same thing as foundset.newRecord() only foundset you have more choices like not changing the selection (that will make things much faster). Also there is no matching controller.saveRecord()!! there is a controller.saveData but as you said this does save ALL pending changes over EVERYTHING. We can’t just do save that record. Because related data should also be saved. But in the end you don’t need to worry so much for saving youre record. This will be done for you.. controller.saveData() can be used at the end of method where you make newRecords or change data. The need to do it in between session is not that great. Only ONE exception i can think of.. Creating related records with a parent that has a database identity (auto increment column or db sequence) Because then before you make a new relatedRecord we should have the parents id. And with that kind of sequence we only have the id when you save it. We are trying to handle this as good as we can so a saveData is maybe not needed but we can guarentee it 100%

2> the difference is that foundset.newRecord has more options and therefore can be much faster.

3> yes we are thinking about that to move controller.saveData to application… Maybe it is more logical. Because it does save everything.

4> Through relations it also should work fine. And the related data doesn’t have to be set. if you do relation.newRecord() because that do we for you.

If you don’t use database sequences but just servoy sequences can you remove all saveData calls from youre code and only set one at the end of the script?
If you do use db sequences then you can only remove this line:

forms.tab_templ_dagdelen.controller.saveData();

and do controller.saveData() at then end.

So thanks to Johan we were able to resolve the problem. :D

It had to do with the backend being screwed-up. Reinitialising the db cured the problem. However while investigating Johan rewrote the method in a much more elegant way. So I post it here for everybody to admire and make good use of it.

controller.newRecord();
for ( var i = 1 ; i <= 5 ; i++ )// dagen zijn genummerd van 1 tot 5
{
var record = templates_to_templ_dagen.getRecord(templates_to_templ_dagen.newRecord(false,false));
record.templ_dagnummer = i;
for ( var j = 0 ; j <= 7 ; j++ )// dagdelen gaan van 0 tot 7
{
var childRecord = record.templ_dagen_to_templ_dagdelen.getRecord(record.templ_dagen_to_templ_dagdelen.newRecord(false,false));
childRecord.type_dagdeel = j;
}
}
controller.saveData();

A Special Thanks to Mister J. (let me know your favorite beer (I know what country you live in))

:wink:

Hmm,

to follow up without having to reexplain the problem which is related to the above:

How do I duplicate a template that has been created with the method above ?

So Situation in 1 line

Templates → Days—>Dayparts

I want to duplicate a template and the related Days AND their related dayparts.

Thanks for helping me out.

if you want to duplicate these things you need to work with foundsets and records only (so no controller nothing)

i don’t have the time currently to completely write that method but it is something like this:

// get the current selected record you want to duplicate:
var templateRecord = foundset.getRecord(controller.getSelectedIndex());
var duplicatedRecord = foundset.duplicateRecord();

// Walk through the current templateRecords relation:
for ( var i = 1 ; i <= 5 ; i++ )// dagen zijn genummerd van 1 tot 5
{
var currentDagRecord = templateRecord.templates_to_templ_dagen.getRecord(i);
var newDagRecord = duplicateRecord.templates_to_templ_dagen.getRecord(duplicateRecord.templates_to_templ_dagen.newRecord());
databasemanager.copyMatchingColumns(currentDagRecord,newDagRecord);
for ( var j = 0 ; j <= 7 ; j++ )// dagdelen gaan van 0 tot 7
{
// do exactly the same as the above loop for all dagdelen
}

}