Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Name Lookup
Tagged: lookup script forms
- This topic has 16 replies, 2 voices, and was last updated 1 year, 6 months ago by Chris Jordan.
-
AuthorPosts
-
April 15, 2023 at 12:04 PM #49338
Chris JordanParticipantHi,
I have two Forms. One called Donations and the other called People. In the Donations form I have the script below where I’m attempting to look up the name of a person by Envelope number. I double-clicked the “Child records loop” Snippet with the People – Envelope field highlighted.For some reason I’m not getting any records (people.length = 0) even though there are 26 Records in the People Form. I am running the script in the Editor and length prints out 0. I must be doing something wrong. Can someone please give me some guidance.
Thanks,
Chrisfunction Script() { function recordsLoop() { var people_id = 'fld-16486faf17c94f779b495e39db0d2a51'; var envelope_id = 'fld-86ff175986c74617a1c71822bf7e9f2c'; var people = record.getFieldValue(people_id); console.log(people.length); for (var index = 0, count = people.length; index < count; index++){ var envelope = people[index].getFieldValue(envelope_id); console.log(envelope); if (envelope) { // do something console.log(envelope); } } return; } recordsLoop(); } Script();
- This topic was modified 1 year, 7 months ago by Brendan. Reason: added back ticks around script code
April 15, 2023 at 12:48 PM #49340
BrendanKeymasterMaybe the function definition within a function definition is messing things up.
Remove the outer function declaration and see if that solves the issue.
April 15, 2023 at 1:07 PM #49341
Chris JordanParticipantThanks Brendan, I tried your suggestion. I’ve reduced it to this and I get “undefined” about 15 times which is the number of records in the Donation form.
function recordsLoop() { var people_id = 'fld-042db77a64be4be0a8f041e927d5ff24'; var envelope_id = 'fld-86ff175986c74617a1c71822bf7e9f2c'; var people = record.getFieldValue(people_id); var cnt = people.length; console.log(cnt); for (var index = 0, count = people.length; index < count; index++){ var envelope = people[index].getFieldValue(envelope_id); console.log(index); if (envelope) { // do something } } return envelope; } recordsLoop();
- This reply was modified 1 year, 7 months ago by Brendan. Reason: Added back ticks to format code
April 15, 2023 at 8:34 PM #49343
BrendanKeymasterHi Chris,
FYI, I keep adding back-ticks to your posts so the code gets formatted properly. Just add a back tick character on the first line of your code at the begging and at the end of the last line of your code.
I don’t see anything wrong with your code.
Can you post your form template?
April 16, 2023 at 5:01 AM #49344
Chris JordanParticipantYes, here is my form template.
Thanks for your help,
Chris
Attachments:
You must be logged in to view attached files.April 17, 2023 at 8:56 PM #49347
BrendanKeymasterHi Chris,
Ok, so your form is setup with People as the parent and Donations as the child.
Your script is in your Donations form. Your script is attempting to loop through the parent People record. However, it’s not an array because it’s just one object. A Donation record has only a single People parent record. So you can just get the People record, then ask it for one of the fields.
Since your script is called
Firstname
, I’m assuming you’re trying to get the People record’sFirst Name
field.So all you need to do is first get the parent
People
record, then ask that record for the value from theFirst Name
field.Here’s code to do that:
function firstName() { var people = record.getFieldValue('fld-042db77a64be4be0a8f041e927d5ff24'); var first_name = people.getFieldValue('fld-8c3880a29ec949e0a8ff1d502b527ae7'); return first_name; } firstName();
That’ll do it. Hopefully that’s what you’re looking to do.
April 17, 2023 at 8:58 PM #49348
BrendanKeymasterBy the way, to write that code, all I had to do was double-click on the
People
field on the left side of the Script Edit window. Then double click on theFirst Name
field. Then all I typed was thereturn first_name
and I wrapped it into a function structure.April 18, 2023 at 7:04 AM #49349
Chris JordanParticipantHi Brendan,
Yes that is exactly what I wanted! I was confused because I thought the Child Records Loop snippet was giving me the right code. But, I was wrong in my thinking. So in the People Form the field Donations, which is a Link to a Form datatype, is what establishes the Parent (People) / Child (Donations) relationship? Also, I am used to thinking in SQL, so are forms the same as tables? And documents the same as databases?
I really appreciate your prompt replies and excellent explanations. Great product too!
Thanks,
Chris
April 18, 2023 at 11:59 AM #49350
Chris JordanParticipantHello again Brendan,
I guess what I was originally trying to do, was write a script that:
1. Automatically connected each donation to its parent record based on Envelope number.
2. Automatically fill in the name field based on the Envelope number found in the parent record.Number two is what you just helped me with, however I had to manually select the parent record in the Inverse Relationship field, by looking up and connecting the correct Envelope number for the person. I know you have removed the SQL language, however now I have to understand the inner modeling of the application in order to write Javascript to pull the data.
Still learning,
Chris
April 18, 2023 at 12:47 PM #49351
BrendanKeymasterhi Chris,
The Child Records Loop snippet is designed to fetch the child records, not the parent record. So in your case, the People form is the parent and the Donations form is the child. So if you wanted to put a field on the People form that say, concatenated all of the donations for that People record together, that’s when you would use the Child Records Loop.
Yes, Documents are like Databases. Logically and technically as each
.tapforms
document is actually a folder that contains within it its own SQLite database.And yes, Forms are like tables precisely. Records are of course like SQL records and fields are like Table Columns in SQL.
If you want to automatically connect your forms together based upon some key field, then you would need to use the Join Link Type on your Link to Form field and set the common fields up. Then Tap Forms will auto-join for you.
But that does in fact change the JavaScript code you would need to get the name values. Because with a Join Link Type, that is in fact the same as an automatic Many to Many relationship. That is, for each Donation record, there technically could be many People records and for each People record there could be many Donation records.
So now when you get the value of the People field from the record, you’ll get an array instead of just the one object like in my code example. In this case, the Child Records Loop would work as expected.
Hope that makes sense.
Thanks,
Brendan
April 18, 2023 at 4:01 PM #49352
Chris JordanParticipantHi Brendan,
Well thanks for your explanations! Now that I understand how TapForms works I was able to achieve what I wanted by making the Parent/Child relationship Many-to-Many. This gave me access to all the records and I was able to automatically lookup the name by envelope number. Here is my resulting code. Thanks again for your help!!
Chris
function Name() { // Get the Donation record data var envelope_donation_id = 'fld-99a449db91cc4bd9ace9a8c92e9e47b9'; var envelope_donation = record.getFieldValue(envelope_donation_id); // Get the People record data var people_id = 'fld-042db77a64be4be0a8f041e927d5ff24'; var envelope_id = 'fld-86ff175986c74617a1c71822bf7e9f2c'; var people = record.getFieldValue(people_id); var last_name_id = 'fld-f5e0010890dc47e886215eb097695315'; var first_name_id = 'fld-8c3880a29ec949e0a8ff1d502b527ae7'; // Loop through the People records for (var index = 0, count = people.length; index < count; index++){ var envelope = people[index].getFieldValue(envelope_id); var first_name = people[index].getFieldValue(first_name_id); var last_name = people[index].getFieldValue(last_name_id); // Does the Donation Envelope match the People Envelope? if (envelope = envelope_donation) { // Yes, now get the name data if(first_name && last_name) return first_name + " " + last_name; else if(!first_name && last_name) return last_name; else if(first_name && !last_name) return first_name; } } // We only get here if we can't find an envelope match return "Not Found!"; } Name();
April 18, 2023 at 8:33 PM #49353
BrendanKeymasterif (envelope = envelope_donation) {
You should use
==
since that’s a comparison operator. You’ve used=
which is an assignment operator.April 19, 2023 at 4:15 AM #49354
Chris JordanParticipantOh darn! I knew that. Fixed it. Thanks for the code review Brendan!
April 19, 2023 at 12:35 PM #49355
BrendanKeymasterAnother little trick:
var name = []; name.push(first_name); name.push(last_name); return name.join(" ");
Basically it puts the first and last names into an array and then joins them together with a space. And if only one value is in the array because it’s not empty, then that’s the only value you get. But if both values are in the array, then you get them joined together with a space character in between.
Saves having to do all that
if
statement logic.April 20, 2023 at 1:39 PM #49362
Chris JordanParticipantThat’s a good tip! Thanks Brendan.
My script still isn’t working the way I wanted it to because in the Donations table I still have to manually select the Parent data in the “People” inverse relationship field. If I don’t select anything my script doesn’t work. I wanted to be able to put the Envelope ID in the Donations record and have it automatically look up all the relevant data in the People table. I don’t know if I am making any sense or not.
People Donations
====== =========
Envelope Envelope (manually entered)
First Name First Name (auto fill from the script)
Last Name Last Name (auto fill from the script)Anyway, I think I am going to just have one form and put the Donations on each person’s data in the People table.
Thanks,
Chris
April 20, 2023 at 8:23 PM #49365
BrendanKeymasterYou’ll need a Join Link Type for that. Do you have that set? Or are you using Many to Many?
April 21, 2023 at 6:05 AM #49367
Chris JordanParticipantWell, I am giving up on scripts. I keep going round and round with this. I get the script to work in the console window while editing and then I go to the form and the field doesn’t update. I refresh both tables and go back to the script edit window and now the console values don’t work. So frustrating. I think there is an update issue in the software.
-
AuthorPosts
You must be logged in to reply to this topic.