need little help how to duplicate a related foundset

Hi all!

I have to duplicate the master record with one or more related record items. The related records should also be duplicated and related to the duplicated master record.

But I’m struggleing to get this working and I would be deeply grateful to get some straight tips.

Thanks in advance,
Thomas

Hi Thomas,

Quick search on the forum gave me these threads:
viewtopic.php?f=3&t=10405
viewtopic.php?f=22&t=13889

Hope this helps.

ROCLASI:
Quick search on the forum gave me these threads:

Hi Robert,

I have read these threads on weekend and my confusion was complete :oops: . Of course I have tried some code of the topics, but without a well working result. Too complicate for me…

Hi Thomas,

The code from Harjo didn’t work for you ?
What does your code look like now ?

Robert,

I will try Harjo’s code again today and reply the result. His code looks like Johan’s from an earlier post… I got not working.

Thank you, Thomas

Ok, I have created now a global function like Harjo’s:

function gm_duplicateRec(arg1,arg2) {
var fs = arg1;
var relatedFsArray = arg2;

// Duplicate master record.
var dup = fs.getRecord(fs.duplicateRecord(false,false));
databaseManager.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);
   }
}
databaseManager.saveData();
}

I would like to duplicate only one master record (order) with one or more related records (order_items). In I have created the function:

function m_duplicate() {
var id = order_id;
if(controller.find()){
order_id = id;
controller.search();
var arg1 = foundset;
var arg2 = new Array(order_to_order_items)
globals.gm_duplicateRec(arg1,arg2);
}
}

The TypeError I get is: Cannot call method “getSize()” of undefined. Because in the debugger the variable in the globale function is undefined!?
Is anything wrong in my function?

Hi Thomas,

It looks like you need to pass an array with relationnames (as string) instead of the relation object.

function m_duplicate() {
   var id = order_id;
   if(controller.find()) {
      order_id = id;
      controller.search();
      var arg1 = foundset;
      var arg2 = new Array("order_to_order_items");
      globals.gm_duplicateRec(arg1,arg2);
   }
}

Robert,

that is it! Thanks a lot.

But now, how can I set different values to the duplicated master and related records like order_no, order_date and so on?

Best regards, Thomas

you can access your duplicate by: dup.myColumn = ‘myvalue’
This must be running in the same method as mine, ofcourse, else there is no variable: dup

Hi Robert + Harjo!

Your help and code was brilliant. All what I would like to do is working perfect.
I made some changes in the form function to get the pk id from the duplicated record:

function m_duplicate() {
   var id = order_id;
   if(controller.find()) {
      order_id = id;
      controller.search();
      var arg1 = foundset;
      var arg2 = new Array("order_to_order_items");
      //********
      var id = globals.gm_duplicateRec(arg1,arg2);
     //*********
   }
}

In the globale function it looks like:

function gm_duplicateRec(arg1,arg2) {
var fs = arg1;
var relatedFsArray = arg2;

// Duplicate master record.
var dup = fs.getRecord(fs.duplicateRecord(false,false));
//*********
var id = dup.order_id
//*********
databaseManager.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);
   }
}
databaseManager.saveData();
//*********
return id;
//*********
}

With the return of the pk id it is easier for me to change and set values in the duplicated record as in the globale function.
Many thanks guys :-)