Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Using Tap Forms › Copying and Pasting fields using Script
- This topic has 4 replies, 3 voices, and was last updated 3 years, 3 months ago by Sam Moffatt.
-
AuthorPosts
-
September 5, 2021 at 9:03 PM #45178
Martin FurseParticipantHi,
being someone who doesn’t know Java Script, I have been trying to copy three fields and place them into a fourth field, which is a different field to where the script will reside. As it is Street, Suburb and State that I want copied I also need spaces between the three fields, so when pasted into the “Map location Address” it will return the google map location of the address. I’ve attached the relevant part of the form to hopefully make it clear what I am trying to do. I couldn’t find my question in the forum, so if it has has been covered previously, I apologies in advance.
Cheers
Trusty
Attachments:
You must be logged in to view attached files.September 6, 2021 at 12:47 AM #45181
BrendanKeymasterHi Martin,
Can you please upload your form template so I can try it out on your form?
A Location field is a bit of a special kind of field because it actually contains more than one value inside it, stored as a dictionary of keys and values. So in order to update a Location field you need to set the
title
key in the dictionary to your address value.Thanks,
Brendan
September 6, 2021 at 1:04 AM #45182
Sam MoffattParticipantSo you need to get the field values via
record.getFieldValue
, join it into a comma separated string and then userecord.setFieldValue
anddocument.saveAllChanges
to set and save the new value.Here is a script that takes a street, city and state fields to then join them into comma separated single line and then sets a location field and also a website field. You will need to change the ID’s to match your own ID’s. How do you find the ID of the field? It’s underneath the description field of the form editor or you can use the ID button in the script editor to also get the field ID or copy it from below the field list when selecting that field.
function Location_Copy() { var fields = ['fld-b5b69ac18a584b9998908bc82f42d7a5', // Street 'fld-5943325d2ee6480b89a1d044b8695e6c', // City 'fld-105b4e8304524a0bb7710c2b507f0590', // State ]; var location_id = 'fld-4d374af0f2644f798ea1e6c3c434290a'; // Location type field var google_maps_link_id = 'fld-db45b343c30f4410aa78c690ad92b2ff'; // Website type field var pieces = []; for (var field of fields) { var item = record.getFieldValue(field); item ? pieces.push(item) : null; } var joined = pieces.join(', '); console.log(joined); record.setFieldValue(location_id, joined); record.setFieldValue(google_maps_link_id, <code>https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(joined)}</code>); document.saveAllChanges(); } Location_Copy();
Ok to take this apart a little. This first bit sets a variable called
fields
that has the list of fields in the order you want them to show up in the address. For my example I did street, city and state following your ask but you could add another line for country with the field ID of your country field.function Location_Copy() { var fields = ['fld-b5b69ac18a584b9998908bc82f42d7a5', // Street 'fld-5943325d2ee6480b89a1d044b8695e6c', // City 'fld-105b4e8304524a0bb7710c2b507f0590', // State ];
These next two lines are setting the field ID’s of our target fields, the location field and a website field. Again, you can select your version of this field and click “ID” to get an output that looks like this. The
//
is a comment that I added manually.var location_id = 'fld-4d374af0f2644f798ea1e6c3c434290a'; // Location type field var google_maps_link_id = 'fld-db45b343c30f4410aa78c690ad92b2ff'; // Website type field
This next part creates a list of
pieces
and then uses the list offields
to be able to get the field values of each of the fields (e.g. street, city, state) and then check if they’re set to a value (thats theitem ?
line) and if they are set, push them into the array of items (pieces.push
) or do nothing (thenull
). This means that if a field is empty, it won’t be added and you won’t get a blank comma for example. Also means that you can add more fields by just adding the ID’s in the right order and everything will still work:var pieces = []; for (var field of fields) { var item = record.getFieldValue(field); item ? pieces.push(item) : null; }
This is where we
join
thepieces
together to create our comma separated list. I also log this for debugging because it’s useful to see what is going on.var joined = pieces.join(', '); console.log(joined);
We then set the field value (
record.setFieldValue
) of the location field and the website field with a Google Maps link. The location field can just be set with the simple joined value however the website link needs some extra data to hook it up to Google Maps. We need to save the changes which is whatdocument.saveAllChanges()
does for us.record.setFieldValue(location_id, joined); record.setFieldValue(google_maps_link_id, <code>https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(joined)}</code>); document.saveAllChanges(); }
Lastly we need to tell Tap Forms to call the function we just created:
Location_Copy();
September 6, 2021 at 3:00 AM #45183
Martin FurseParticipantThank you Sam and Brendan for all your help. I really do appreciate it.
September 6, 2021 at 1:31 PM #45186
Sam MoffattParticipantOne minor fix because the forum munged it, where it has this line:
record.setFieldValue(google_maps_link_id, <code>https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(joined)}</code>);
There is
<code>
there which should actually be a back tick, correct line should read:record.setFieldValue(google_maps_link_id, `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(joined)}`);
Hopefully that works out for you :)
-
AuthorPosts
You must be logged in to reply to this topic.