Copy data from field to new record

Viewing 14 reply threads
  • Author
    Posts
  • October 21, 2020 at 5:08 AM #42332

    Brecht DHeere
    Participant

    Hi,

    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,
    Brecht

    October 21, 2020 at 9:57 AM #42338

    Daniel Leu
    Participant

    You 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

    Brendan
    Keymaster

    Another 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 Moffatt
    Participant

    You could simplify the last bit by doing return newRecord.getUrl() instead of return newRecord.getId() then it could be simplified to Utils.openUrl(New_Lesson());

    Another +1 for duplicate record though.

    October 22, 2020 at 8:05 AM #42361

    Brecht DHeere
    Participant

    Hi 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 Leu
    Participant

    You 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 DHeere
    Participant

    Thank You!
    How do I learn to create such scripts?

    October 22, 2020 at 10:06 PM #42369

    Sam Moffatt
    Participant

    In 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 the copyField 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 DHeere
    Participant

    Hi 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 Moffatt
    Participant

    Of 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 the lessons_id line from above and double click on the “Students” in the field list of the script editor to get the students_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 DHeere
    Participant

    Thank 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 Moffatt
    Participant

    Every 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 DHeere
    Participant

    Yes, 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 DHeere
    Participant

    Update: 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 DHeere
    Participant

    New Feature
    OK, it will come…

Viewing 14 reply threads

You must be logged in to reply to this topic.