Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › How to get and add data from other form
- This topic has 6 replies, 3 voices, and was last updated 4 years, 8 months ago by Alejandro.
-
AuthorPosts
-
April 10, 2020 at 9:17 AM #40291
AlejandroParticipantHi! I’m starting to use script in Tap Forms 5.
Perhaps someone can help me with this. I think is easy but I’m stuck.Scenario: I have two forms that are related with the link to form option in a one to many way.
Form Customers:
Fields: Name, Phone, Address, Tickets (Link to Form)Form Tickets:
Fields: Customers (Link From Form), Subject, Date, Resolve, NotesI want to run a script to add the value I see in Form(Tickets)->Customers to Form(Tickets)->Notes.
Thank you!
April 10, 2020 at 10:23 PM #40294
BrendanKeymasterHi Alejandro,
You could do something like this to get a field’s value from the parent Customers form and create a new record in your Tickets form and set the customer value into the Notes field:
function CreateNote() { // get the name value from the parent Customers form var customers_form_id = 'fld-123...'; var customers_form = record.getFieldValue(customers_form_id); var name_id = 'fld-abc...'; var name_value = customers_form.getFieldValue(name_id); // get the Tickets form, create a new record in it, and set the value on the Notes field. var tickets_form = document.getFormNamed("Tickets"); var note_id = 'fld-xyz...'; var ticket_record = tickets_form.addNewRecord(); ticket_record.setFieldValue(note_id, name_value); form.saveAllChanges(); } CreateNote();
April 10, 2020 at 11:31 PM #40295
Sam MoffattParticipantDo you mean the customer name, phone and address details?
I’d create a form script, inside the script editor you are going to be able to double click on the left to grab the Customers form (section header) and then double click on the fields you care about from that form (name, phone and address I guess).
Next step is then double clicking on the notes field to get it’s current value. Then you’re going to want to append to it (presumably). To do that you’re going to want to combine the existing value with a new one.
var notes = record.getFieldValue('fld-1234'); record.setFieldValue('fld-1234', notes + "\n" + name + "; " + phone + "; " + address); form.saveAllChanges();
Something like that will take the values from the name, phone and address field (assuming the variables ended up that way) and will add them to the existing notes field with a new line prepended. You need the
form.saveAllChanges();
to make sure your changes are saved.April 11, 2020 at 8:21 AM #40297
AlejandroParticipantHi Brendan, it works perfect!
April 11, 2020 at 8:40 AM #40298
AlejandroParticipantHi Sam! Thank You! I added your script to the Brendan’s script and I’ve got a new idea.
Bu the way, I add this script that helps me to get the Fld_Ids:
function Get_Flds() {
var records = form.getFields();
for (var index = 0, count = records.length; index < count; index++) {
var theRec = records[index];
var fld_id = theRec.getId();
var name_id = form.getFieldWithId(fld_id).name;console.log(‘FieldName: ‘ + name_id + ‘\n’ + ‘ fld_ID: ‘ + fld_id + ‘\n\n’);
}
}Get_Flds();
April 11, 2020 at 9:31 PM #40301
Sam MoffattParticipantNeat! That’s a similar concept to a form logger script I wrote a while back. The latest version on GitHub includes a mode that outputs the script fields as Javascript variables prefixed by form name to ensure that your fields across multiple forms aren’t confusingly duplicated. I copy the output of the console log for this script to a new form script called “All Fields” which I can reference everywhere and use the same variables.
April 12, 2020 at 3:45 PM #40303
AlejandroParticipantHi Sam!
Thank you! I’m looking for all your scripts at GitHub and are great to learn a lot! -
AuthorPosts
You must be logged in to reply to this topic.