8 Replies Latest reply on Apr 27, 2016 3:17 PM by dsvail

    Create related record in popover




      I am trying to create a new related record from a popover.  Here is a little context:

      Contacts -- Enrollments -- Courses

      where enrollments is a join table between contacts and courses. 


      From the CONTACTS layout I have a popover which will serve as the registration form. 

      In the popover I created global fields and used the set field script to populate the fields in the ENROLLMENT table (via a button in the popover). 

      There is a script trigger to clear to global fields when the popover closes.


      Everything works fine....ONCE.  If I attempt to register the same contact to a different course....it alters the existing enrollment instead of creating a new one. 


      What am I doing wrong?


      Thanks for the help!

        • 1. Re: Create related record in popover

          filemaker only views the FIRST related record via a relationship in what you have setup. That is why it is modifying and only works once.


          What you need to do is create a new related record first, then set the values in that new record into the new record.


          Usually, people write simple scripts tied to a "save" button to do that:


          Set Variable [ $id ; Table1::primaryKey ]

          Set Variable [ $field1 ; Table1::global1 ]

          Go To Layout [ table2 ]

          New Record

          Set Field [ table2::foreignKey ; $id ]

          Set Field [ table2::field1 ; $field1 ]

          Go To Layout [ original layout ]

          • 2. Re: Create related record in popover

            user26927 wrote:

            What am I doing wrong?

            Hard to tell without seeing your script.

            • 3. Re: Create related record in popover

              Thanks for the help.


              Can you expand a little on your example script?

              What do the two set variable commands do?

              Will the Go To Layout command actually take the user to that layout?  I want to stay in the popover.

              Thank you

              • 4. Re: Create related record in popover

                The Set Variables are not needed for the Global fields but it is needed for the Primary key since it is not a global.


                Mike gave you a good suggestion.  If you are looking for another suggestion you could create another relationship between Contacts and Enrollment.  Create a global text field in Contacts called g_uuid and a text field field in Enrollment called uuid.

                Your relationship is g_uuid to uuid.  Set the relation to allow creation of new records.  In your save script add Set Field [g_uuid;Value: Get(UUID)] to the beginning of the script.  Then use Set fields to set the relevant values into the Enrollment table. The Set Field will create the new record and you never have to leave your layout to use this technique.

                • 5. Re: Create related record in popover

                  Or, if you don't use UUIDs, also create an “allow creation” relationship, put a very small portal based on that relationship into the popover, give it scrollbars, 3 rows, an object name and a “Hide if” condition of 1, and script


                  Go to Object [ "portalName" ]

                  Go to Portal Row [ last ]

                  Set Field [ PortalTO::someField ; LayoutTO::gSomeGlobal ]

                  Set Field [ PortalTO::someOtherField ; LayoutTO::gSomeOtherGlobal ]

                  # […]


                  which all happens within the popover.

                  • 6. Re: Create related record in popover

                    It works as you suggested!


                    I don't understand how it works....but it works.

                    • 7. Re: Create related record in popover

                      It works because the uuid is a unique value that can only create one related record. Each time you save, it reset the uuid to another unique value. Using Set Field is an undocumented feature in FileMaker that allows you to create related records. If you don't want to create the two extra fields, the technique that Erolst described also works well. If you already have a relationship set up to allow creation of new records you wouldn't need a new relationship. Beware that if you relationship allow creation of new records, anytime you use the relationship in a portal,the user might be able to create a new record in the portal by going to the last row in the portal and entering data.

                      • 8. Re: Create related record in popover

                        the two variables are the record ids you need to populate your "join" table ... add a script step New Window then  Go to Layout.... a utility table(the join table) offscreen to create a new record [table2]. Close the Utility Window and you'll be back on the popup.



                        Set Variable [ $id ; Table1::primaryKey ] // ContactID

                        Set Variable [ $field1 ; Table1::global1 ] // CourseID

                        New Window [ Style: Document ; Top: (Get (WindowDesktopHeight) ; Left: Get (WindowDesktopWidth)]

                        Go To Layout [ table2 ] // this is a utility table / the join table for Enrollment

                        New Record

                        Set Field [ table2::foreignKey ; $id ] // foreign key in enrollment

                        Set Field [ table2::field1 ; $field1 ] // foreign key in enrollment

                        Commit Records/requests

                        Close Window [Current Window} // dismisses the utility window

                        //Go To Layout [ original layout ]  //don't need