TapForms 5 doesn’t provide a way to copy data from one document to another one. But since a few releases, there is support to copy data to and from the clipboard. This can be used for data exchange between two documents.
Following functions copies the currentRecord in JSON format to the clipboard:
function copyRecordToClipboard(currentRecord){
let currentForm = currentRecord.form;
let rec = {};
let fields = currentForm.getFields();
for (f of fields){
rec[f.name] = currentRecord.getFieldValue(f.getId());
}
rec['recUrl'] = currentRecord.getUrl(); // add record url so we can backlink to original record
Utils.copyTextToClipboard(JSON.stringify(rec));
console.log("Copied record: " + JSON.stringify(rec));
}
Then in the target document, I create a new record based on the just copied data:
function copyRecordFromClipboard(currentForm){
let newRecord = currentForm.addNewRecord();
let fields = currentForm.getFields();
let rec = JSON.parse(Utils.copyTextFromClipboard());
for (f of fields){
newRecord.setFieldValue(f.getId(), rec[f.name]);
}
console.log("Created new record from : " + JSON.stringify(rec));
return newRecord;
}
There is one additional trick that comes in very handy: Instead of switching from the first document to the second document using the GUI, I use
Utils.openUrl(tfUrl)
in my script to automate this step. The value for tfUrl is created using ‘File > Copy Record Link’ in the target document.
The given example code to exchange record data between two documents works well, when all field names of the two documents are identical. Otherwise, the same principle still can be used, but at least one ‘for … loop’ needs to be changed into separate field assignments.
Did you like this post? Did you use the given code? Please consider supporting me by buying me a coffee!
Thanks!