Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Parent / Child Records
- This topic has 6 replies, 5 voices, and was last updated 2 years, 5 months ago by Andrea Lisari.
-
AuthorPosts
-
November 13, 2019 at 8:47 AM #38026
Stephen AbshireParticipantLet’s assume that I have two forms ‘Parent’ and ‘Child’ that are a one-to-many from Parent to Child. I know that I can get all of the records from Child using the following:
var records = document.getFormNamed(‘Parent’).getRecords();
But what I need to do is only retrieve the records from Child that belong to the currently selected record in Parent. How do I do this? Is there a hidden key field that joins these forms that I should use?
It would be great if I could do this without a brute force loop because I am concerned that as my database grows this could be an area where a performance hit could be taken.
November 13, 2019 at 9:17 AM #38027
Sam MoffattParticipantIn the Script Editor, the linked form should show up on the left as an option. If you double click on the name of the form, it will insert the field ID for the Link to Form field. You can then use
record.getFieldValue
with that field ID and it will give you an array of records that are linked.If you look in the snippets at the bottom, there is a “child records loop” snippet. If you select a field from the child record and click on this, it’ll insert a premade loop for you that will iterate over all of the child records and also pull out the selected record.
On the other side of a 1:M,
record.getFieldValue
can be used with a Link From Form field (enabled via “Show Inverse Relationship”) to retrieve the parent form.November 13, 2019 at 2:57 PM #38035
Stephen AbshireParticipantPerfect got it working thanks!
November 18, 2019 at 11:04 AM #38083
Victor WarnerParticipantI am having difficultly making this work for me – would Brendan or another user be able to provide a simple worked example how this operates?
I am finding JavaScript difficult and general guides are not, at least for me, helping in understanding how to apply much of the example snippets that Tap Form provides.
Any help would be very gratefully received.
November 18, 2019 at 11:56 AM #38084
Stephen AbshireParticipantI used something like this:
function Enumerate_Child_Records() { var child = document.getFormNamed('Child Form Name').getRecords(); for (var index = 0, count = child.length; index < count; index++) { var str = child[index].getFieldValue('child_field_id'); } } Enumerate_Child_Records();
November 18, 2019 at 1:55 PM #38089
Daniel LeuParticipantHere is a small example from a product database that has an inventory field that links to the inventory form:
var inventory_link_id = 'fld-22daea1b41174663aa3a1a2489391186'; var inventory_name_id = 'fld-84198a5bed914ce9b1f726660eec73b2'; for (inventory of record.getFieldValue(inventory_link_id)){ console.log(inventory.getFieldValue(inventory_name_id)); }
With
record.getFieldValue(inventory_link_id))
I get all the child records which I use in my for loop where I iterate over each child.July 21, 2022 at 9:52 AM #47650
Andrea LisariParticipantAbsolute beginner with scripting, even if I did something with FileMaker some years ago.
Got a basic DB with two forms, Authors (Parent) and Books (Child), with M:M relationship (an author can write many books, and a book can be written by more than a single author) that works perfectly. Authors form has just a Name field, and Books form a Title field.
Now I’d like to get a simple Child records count, just to know which author has written the highest number of books. I tried with the “child records loop” snippet by selecting the Title field, Tap Forms created the following scriptfunction recordsLoop() {
var books_id = ‘fld-44222ad745354a45b569beec4109d33b’;
var title_id = ‘fld-d53a50ba8edd4d0fb0024e6a3f15fae2’;
var books = record.getFieldValue(books_id);for (var index = 0, count = books.length; index < count; index++){
var title = books[index].getFieldValue(title_id);
if (title) {
// do something
}
}
return;
}recordsLoop();
but I get nothing but [object TFFormEntry].
By the way, is it possible to have a Number field automatically filled with the number of the Child records on the Parent form?
Sorry for being so newbie :-| -
AuthorPosts
You must be logged in to reply to this topic.