September 20, 2018 at 3:20 PM #30687
I use TapForms for a logbook documenting my boat trips. In this logbook, I keep the records sorted by ‘trip start date’ (one of the fields). When adding a new record, I would like to copy several fields from the previous (=most recent) record to the newly created record. This would allow me to automatically copy the ‘mileage upon arrival’ field from the most recent record to the ‘mileage on start’ field for the new record. The same applies for the ‘Arrived at’ field from the most recent record, I would like to copy (or pre-fill) the contents of that field to the ‘Start from’ field in the newly created record.
I can imagine this might be useful for others as well. Would you be able to give an example implementation of a script doing something like that?
Thanks in advance!
DaanSeptember 20, 2018 at 4:01 PM #30688
That’s a really good idea and certainly doable. You would probably want to use a Form script for that. The nice thing about form scripts is you can just reference whatever current record is selected, then tell the form to make a new record within the script, copy the values from the current record and stuff them into the new record.
So what you would do is select the latest record, select the script (which you can also assign to a shortcut key) and run the script.
I’ll make an example form and post it here.September 20, 2018 at 4:12 PM #30689
Ok, here’s a Mileage Tracker form with a Form script that will do what you asked for.
Attachments:You must be logged in to view attached files.September 20, 2018 at 4:15 PM #30691
Here’s what the script looks like:
var vehicle_id = 'fld-64b0b17a635f49b8b40867e45f8d24db'; var start_mileage_id = 'fld-9674e316bfed408ca6710ce81b72bf05'; var end_mileage_id = 'fld-eed890f4533f4672817c5ae5d4dc8257'; var vehicle = record.getFieldValue(vehicle_id); var end_mileage = record.getFieldValue(end_mileage_id); var newRecord = form.addNewRecord(); newRecord.setFieldValue(vehicle_id, vehicle); newRecord.setFieldValue(start_mileage_id, end_mileage); console.log(end_mileage); form.saveAllChanges();
So whatever record is currently selected at the time you run the script will be what
recordis. The code gets the values from that selected records, then tells the selected form to add a new record, then it copies the vehicle name to the same field in the new record and copies the
end_mileagevalue to the new record’s
Easy peasy :)
Ok, for someone who knows how it works. But it’s really not that complicated once you see it written out.September 20, 2018 at 5:11 PM #30692
Thanks Brendan, that is the perfect example that I was looking for! Now I can amend it to my specific needs. Thanks for the great and super fast response!September 20, 2018 at 5:21 PM #30693
Oh, also that
console.log()statement isn’t needed. I was just printing out the end_mileage value to see what was in it.
I’m glad you found it useful.
I think Scripts will open Tap Forms up to a universe of possibilities.October 14, 2018 at 4:12 AM #31023
Brendan, I have one more question. The form.addNewRecord() function creates a new record but does not set any fields with default values specified. Eg I have a dateLeft field defaulting to today’s date; same with timeLeft defaulting to the time the new record is added.
Is there either a way to create a new record with all field defaults applied, or do this in the script? (I do not exactly know what the actual values of those TapForms date and time fields are, so I also do not know how to setFieldValue them…)October 14, 2018 at 10:36 AM #31031
Oops. I guess I forgot to call my method for setting the default values when a new record is created in that scenario. I’ll modify my code to do that.
You must be logged in to reply to this topic.