record focus

In a quite complex method for duplicating related records in many different tables, in some way the record that handle the main relationship looses the focus (the user interface is not affected by, why?), so the duplication process goes wrong. In fact if I place a variable that gets the pk in some point of the method its value becomes 1, but I don’t know the exact line where it appens.
There is a way to constantly control wich is the pk of the selected record, to discover in wich line the trouble starts?

Hi,

It sounds like you changed records in the process but your question is almost the equivalent of “it doesn’t work, help!” with no extra info, so it’s hard to give you real answers.
Can you give us more information of what this procedure exactly does. Probably showing us the method(s) will shet some light on things.

Like Robert says your information is too little. Anyway, you should always select the record from the foundset if you don’t want to change your selected index via ```
var record = foundset.getRecord(index)


Hope this helps a little but otherwise you should share some code...

This is the code, but anyway there is a way to monitor the pk of the current record in every step of a method?

In this way the method works, without the find in the middle not, and I don’t understand why the record duplicated is not anymore the selected.

if ( status == '2 - Confermata' )
{
return
}

duplicazionePulisci();

var risposta = plugins.dialogs.showQuestionDialog( '',  'Vuoi duplicare questa offerta?',  'Ok', 'Annulla')
if ( risposta == 'Annulla' )
{
return
}

var numeroOrig = numero
var foundsetRighe = offerte_to_offerterighe
var foundsetOrdini = offerte_to_ordini
var foundsetDettaglio = offerte_to_offertedettaglio
var foundsetArticoli = offerte_to_offertearticoli
var numeroRighe = foundsetRighe.getSize()
var numeroOrdini = foundsetOrdini.getSize()
var numeroDettaglio = foundsetDettaglio.getSize()
var numeroArticoli = foundsetArticoli.getSize()
var id = ''

controller.duplicateRecord(true)
status = '1 - Provvisoria'
dataofferta = application.getTimeStamp()
numero = forms.contatori.numeroofferta;
forms.contatori.numeroofferta++;
numero_originale = numeroOrig
var nn = offerteid

//duplicazione offerteRighe
record = foundsetRighe
for ( var i = 1 ; i <= numeroRighe ; i++ )
{
	record.setSelectedIndex(i)
	id = record.offerterigheid
	record.duplicateRecord(i,false,true)
	record.id_offerta = offerteid
	record.id_offerterighe_or = id
}

//duplicazione ordini
record = foundsetOrdini
for ( var i = 1 ; i <= numeroOrdini ; i++ )
{
	record.setSelectedIndex(i)
	id = record.ordiniid
	record.duplicateRecord(i,false,true)
	record.id_offerte = offerteid
	record.id_ordini_or = id
	record.id_offerterighe_or = record.id_offerterighe
	record.dataordine = application.getTimeStamp()
	record.id_offerterighe = null
}

//duplicazione dettaglio
record = foundsetDettaglio
for ( var i = 1 ; i <= numeroDettaglio ; i++ )
{
	record.duplicateRecord(i,false,true)
	record.id_offerte = offerteid	
	record.id_offerterighe_or = record.id_offerterighe
	record.id_ordini = null
	record.id_offerterighe = null
}

//duplicazione articoli
record = foundsetArticoli
for ( var i = 1 ; i <= numeroArticoli ; i++ )
{
	record.duplicateRecord(i,false,true)
	record.id_offerte = offerteid
	record.id_ordini_or = record.id_ordini
	record.id_ordini = null
}

//Without this find the first dupliucated record looses the focus
controller.find()
offerteid = nn
controller.search(true, false)

var foundsetRighe = offerte_to_offerterighe
var foundsetOrdini = offerte_to_ordini
var foundsetDettaglio = offerte_to_offertedettaglio
var foundsetArticoli = offerte_to_offertearticoli
var righeNumero = foundsetRighe.getSize()
var ordiniNumero = foundsetOrdini.getSize()
var dettaglioNumero = foundsetDettaglio.getSize()
var articoliNumero = foundsetArticoli.getSize()
var record = ''

//articoli
record = foundsetArticoli
for ( var i = 1 ; i <= articoliNumero ; i++ )
{
	record.setSelectedIndex(i)
	record.id_ordini = record.offertearticoli_to_ordini_or.ordiniid
	record.id_offerteriga = record.offertearticoli_to_ordini_or.ordini_to_offerterighe_or.offerterigheid
	record.id_ordini_or = null
}

//ordini
record = foundsetOrdini
for ( var i = 1 ; i <= ordiniNumero ; i++ )
{
	record.setSelectedIndex(i)
	record.id_offerterighe = record.ordini_to_offerterighe_or.offerterigheid
	record.id_offerterighe_or = null
	record.id_ordini_or = null
}

//dettaglio
record = foundsetDettaglio
for ( var i = 1 ; i <= dettaglioNumero ; i++ )
{
	record.setSelectedIndex(i)
	record.id_offerterighe = record.offertedettaglio_to_offerterighe_or.offerterigheid
	record.id_ordini = record.offertedettaglio_to_offerterighe_or.offerterighe_to_ordini.ordiniid
	record.id_offerterighe_or = null
}

//offerteRighe
record = foundsetRighe
for ( var i = 1 ; i <= righeNumero ; i++ )
{
	record.setSelectedIndex(i)
	record.id_offerterighe_or = null
}
controller.saveData()

Yes, you can retreive the pk. simply select the dataprovider you need.

But, I don’t understand your code so I will break it up into pieces with remarks:

//I assume foundsetRighe is the selected or current foundset. Then you make a copy of it to the variable record?
//If so 3 things are confusing:
//1 record is normally a referral to a record and not a foundset (=confusing)
//2 you declare a form variable and not a local one, why?
//3 why do you make that copy?
record = foundsetRighe 
for ( var i = 1 ; i <= numeroRighe ; i++ ) 
{ 
//here you (re-)set the index of the foundset, that in itself changes the index of the foundset.
   record.setSelectedIndex(i) 
//here you already retrieve the pk I assume (so you know how to do that ;) )
   id = record.offerterigheid 
//now you duplicate the record but you also select the new record (last true boolean value)
   record.duplicateRecord(i,false,true) 
   record.id_offerta = offerteid 
   record.id_offerterighe_or = id 
}
//I think you should not set the selected index, should not select the new record or reset to the original/wanted index after your code executed

Ok, thanks. As I wrote, this method works fine. I don’t understand why if I don’t perform the search in the middle the select record changes, but never mind. I would like to see when the pk of the selected record changes during debugging, but probably is not possible.

for duplicating records, with all related data, you can use this:

var fs = arguments[0]; 
var relatedFsArray = arguments[1]; 

// Duplicate master record. 
var dup = fs.getRecord(fs.duplicateRecord(false,false)); 
// save is needed if DB_IDENT sequence is used!! 
currentcontroller.saveData(); 

for(var k=0;k<relatedFsArray.length;k++) 
{ 
   var related = fs[relatedFsArray[k]]; 
   for(var i=1;i<=related.getSize();i++) 
   { 
       var relatedOriginal = related.getRecord(i); 
       var relatedDub = dup[relatedFsArray[k]].getRecord(dup[relatedFsArray[k]].newRecord(false,false)); 
       databaseManager.copyMatchingColumns( relatedOriginal,  relatedDub); 
   } 
} 
currentcontroller.saveData();

which can be called from any form method like this:

globals.duplicateRecord(foundset, new Array('parent_to_child','parent_to_child2'));

Very fast!!! :)

:) that is ‘exactly’ what I was about to propose…

I know! :lol:

With this technique is possible to move some data from the original record to the duplicated in differente fields, move data from a field to another and then fill some field with a new relationship created by that?

But what I would like to know is not a duplication method, it works fine, but if during debugging is possible to monitor the pk step by step.

With this technique is possible to move some data from the original record to the duplicated in differente fields, move data from a field to another and then fill some field with a new relationship created by that?

But what I would like to know is not a duplication method, it works fine, but if during debugging is possible to monitor the pk step by step.