I need to duplicate a line with its associated related data (although not all related data needs to be copied). I found an excellent forum post by Greg Pierce which pointed me in this direction, but I can’t get it to work:
var rec = foundset.getRecord(foundset.getSelectedIndex()); // current
var dup = foundset.getRecord(foundset.duplicateRecord()); // dupe
for(var ix=1; ix<=databaseManager.getFoundSetCount(rec.relation1); ix++)
{
var relRec = rec.relation1.getRecord(ix);
var relDupe = dup.relation1.getRecord(dup.relation1.newRecord());
// copy cols, whatever here
}
This is how I’ve applied it to my own version. It copies the main line, but not the children.
var fs = arguments[0];
var relatedFsArray = arguments[1];
// 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();
use this as a global method, and pass the right foundset as first argument and an array of servoy relation(s) as a second argument
and this method will do the rest for you
after this method, you can change what ever you want in the duplicated record, by using dup.mycolumn = newValue, etc…
in Servoy 6 I get warnings in Harjo’s code for the duplicating records method.
How can I fix the issue of an undefined function getSize() and getRecord() as completion of the variable ‘related’?
Servoy 6 is much more advanced in terms of code completion.
Therefore, you need to specify the types of variables when Servoy can not find out what it is.
first of all you have to specify the type of the arguments.
but you also have to specify the type of ‘related’, something like this:
/**
* @type {JSFoundset<db:/udm/contacts>}
*/
var related = fs[relatedFsArray[k]];
/**
* fs == master record(s) foundset
* relArr == array of servoy relation(s) records
* uid == uid_<table> (e.g. uid_order)
*
* @properties={typeid:24,uuid:"E964EAF4-1018-485F-9DAC-37896CD170A4"}
*/
function gm_duplicateRec(fs,relArr,uid) {
//duplicate master record
var dup = fs.getRecord(fs.duplicateRecord(false,false));
var pk = dup[uid];//uid duplicated record
databaseManager.saveData();
//duplicate related record(s)
for(var k = 0;k < relArr.length;k++){
var related = fs[relArr[k]];
for(var i = 1;i <= related.getSize();i++){
var relatedOriginal = related.getRecord(i);
var relatedDup = dup[relArr[k]].getRecord(dup[relArr[k]].newRecord(false,false));
databaseManager.copyMatchingColumns(relatedOriginal,relatedDup);
}
}
databaseManager.saveData();
return pk;//returns uid of duplicated record
}
How should I specify the JSDoc syntax correct?
In my (wrong) way the warnings was gone!? Do I try like your explanation Johan, I still have the warnings for ‘related.getSize()’ and ‘related.getRecord()’?
Sure you have the warnings you have not specify JSDoc for related
Try this code:
/**
* @param {JSFoundset} fs == master record(s) foundset IF YOU NEED THIS FOUNDSET FIELDS YOU MUST DECLARE IT PROPERLY
* @param {Array} relArr == array of servoy relation(s) records
* @param {String} uid == uid_<table> (e.g. uid_order)
*
* @properties={typeid:24,uuid:"E964EAF4-1018-485F-9DAC-37896CD170A4"}
*/
function gm_duplicateRec(fs,relArr,uid) {
//duplicate master record
var dup = fs.getRecord(fs.duplicateRecord(false,false));
var pk = dup[uid];//uid duplicated record
databaseManager.saveData();
//duplicate related record(s)
for(var k = 0;k < relArr.length;k++){
/** @type {JSFoundset} */
var related = fs[relArr[k]];
for(var i = 1;i <= related.getSize();i++){
var relatedOriginal = related.getRecord(i);
var relatedDup = dup[relArr[k]].getRecord(dup[relArr[k]].newRecord(false,false));
databaseManager.copyMatchingColumns(relatedOriginal,relatedDup);
}
}
databaseManager.saveData();
return pk;//returns uid of duplicated record
}