Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Using Tap Forms › Copy data from field to new record
- This topic has 14 replies, 4 voices, and was last updated 4 years ago by Brecht DHeere.
-
AuthorPosts
-
October 21, 2020 at 5:08 AM #42332
Brecht DHeereParticipantHi,
I’m using Tapforms for my students. I create lessons and want to copy data from a field in a record to the new record when I create the new record. Till now I always did that with ‘copy-paste’ but is it also possible to automate this?
Thanks,
BrechtOctober 21, 2020 at 9:57 AM #42338
Daniel LeuParticipantYou could use ‘duplicate record’, but this would copy all fields.
Or you can do this with a script:
function New_Lesson() { // Get current lesson let lesson_id = 'fld-7131629dfa4147d79ed24f05dfc83515'; let lesson = record.getFieldValue(lesson_id); // Create new record let newRecord = form.addNewRecord(); // Copy previous record entry newRecord.setFieldValue(lesson_id, lesson); form.saveAllChanges(); // Return id of new record return newRecord.getId(); } var id = New_Lesson(); // Jump to new record Utils.openUrl("tapformz://record/view/"+document.getId()+"/"+form.getId()+"/"+id);
Just set the lesson_id according to your form. TapForm doesn’t display the new record when it is created from a script. To do this, I use call TapForm from TapForm with the new record Id. This is done with the last statement. You can remove it if you don’t like this behavior.
Have fun!
October 21, 2020 at 6:47 PM #42341
BrendanKeymasterAnother option is to use the Duplicate Record function and then remove the data from copied record you don’t want.
October 21, 2020 at 8:30 PM #42343
Sam MoffattParticipantYou could simplify the last bit by doing
return newRecord.getUrl()
instead ofreturn newRecord.getId()
then it could be simplified toUtils.openUrl(New_Lesson());
Another +1 for duplicate record though.
October 22, 2020 at 8:05 AM #42361
Brecht DHeereParticipantHi Brandon, It is always the same fields that I want to keep. So removing the other data is ok. But how do I do this? With a script?
October 22, 2020 at 8:47 AM #42363
Daniel LeuParticipantYou could remove the unwanted data by hand or using a script.
Thanks, Sam, for the
getUrl
pointer!October 22, 2020 at 9:19 AM #42365
Brecht DHeereParticipantThank You!
How do I learn to create such scripts?October 22, 2020 at 10:06 PM #42369
Sam MoffattParticipantIn the script editor where it has the field list, use the ID field to get the ID’s of each of the fields you want to copy across. It’ll generate a bunch of lines with
fld-something
that you can use with thecopyField
method.Slight refactor to Daniel’s script to create a function for copying the value across as a single line to make it easier to copy multiple fields:
function copyField(fieldId, sourceRecord, destinationRecord) { destinationRecord.setFieldValue(fieldId, sourceRecord.getFieldValue(fieldId)); } function New_Lesson() { let newRecord = form.addNewRecord(); copyField('fld-531d3cf087fc4b63967e29b71d30ba53', record, newRecord); copyField('fld-a53801440fd543669e92f0fdf955fc85', record, newRecord); copyField('fld-4f95fc5da7eb4f6d891e66f2ee620ace', record, newRecord); form.saveAllChanges(); // Return id of new record return newRecord.getUrl(); } Utils.openUrl(New_Lesson());
Attaching a sample archive as well that might help as well.
Attachments:
You must be logged in to view attached files.October 26, 2020 at 9:31 AM #42424
Brecht DHeereParticipantHi Sam, Thanks for the sample but there is still a little problem:
Is it possible to create the new lesson for the same student? For now, I need to choose that name again and refresh before it appears on the student form.
October 26, 2020 at 8:54 PM #42425
Sam MoffattParticipantOf course! So this one is a little more indirect, you need to change the
let newRecord = form.addNewRecord();
line to look at the parent of the current record.First step is to go to our Students form and double click on “Lessons” in the field list of script editor to copy the field ID of the “Lessons” link field, in the database I had, it generates this line
var lessons_id = 'fld-bd0476420e504001a5f20833fc66e7cb';
.Now we jump back to our Lessons form and nuke the
let newRecord
line, paste in thelessons_id
line from above and double click on the “Students” in the field list of the script editor to get thestudents_id
field:var lessons_id = 'fld-bd0476420e504001a5f20833fc66e7cb'; var students_id = 'fld-a9af1b4e918b4cf2b9f7737de6476e40';
Our next step is to get the parent Student record and then use it to add a new Lesson record to it:
var student = record.getFieldValue(students_id); var newRecord = student.addNewRecordToField(lessons_id);
Putting it all together the updated script looks like this:
function copyField(fieldId, sourceRecord, destinationRecord) { destinationRecord.setFieldValue(fieldId, sourceRecord.getFieldValue(fieldId)); } function New_Lesson() { var lessons_id = 'fld-bd0476420e504001a5f20833fc66e7cb'; var students_id = 'fld-a9af1b4e918b4cf2b9f7737de6476e40'; var student = record.getFieldValue(students_id); var newRecord = student.addNewRecordToField(lessons_id); copyField('fld-531d3cf087fc4b63967e29b71d30ba53', record, newRecord); copyField('fld-a53801440fd543669e92f0fdf955fc85', record, newRecord); copyField('fld-4f95fc5da7eb4f6d891e66f2ee620ace', record, newRecord); form.saveAllChanges(); // Return id of new record return newRecord.getUrl(); } Utils.openUrl(New_Lesson());
October 27, 2020 at 4:26 AM #42428
Brecht DHeereParticipantThank you Sam!
I still have one problem: the Email field of the new record in your sample file is not populated with the value from the previous record. Probably because this is a calculation field. But how can I make it enter the value in the new record? Thanks again for your help.
October 27, 2020 at 7:47 AM #42430
Sam MoffattParticipantEvery so often TF doesn’t refresh the calc/script fields after a script execution, at the bottom of the record is a refresh to recalc that record and at the bottom of the list view is another one that will apply to all records in the form. If you see a calc/script field that looks wrong, just hit the refresh button and see if it fixes it up.
October 27, 2020 at 8:14 AM #42431
Brecht DHeereParticipantYes, when I refresh the data is ok. Sometimes I have to go to field view to refresh.
It is not possible with a script to refresh that record? And is that a bug in TF?October 27, 2020 at 3:32 PM #42434
Brecht DHeereParticipantUpdate: It does not always update with the refresh button. Then I have to close the document, re-open, do refresh and then it’s ok. I think that is not very normal??
October 27, 2020 at 4:44 PM #42435
Brecht DHeereParticipantNew Feature
OK, it will come… -
AuthorPosts
You must be logged in to reply to this topic.