1 2 Previous Next 15 Replies Latest reply on Nov 16, 2010 10:59 AM by AndrewFoo

    Running a Script when creating a new record

    AndrewFoo

      Title

      Running a Script when creating a new record

      Post

      How do I go about running a script automatically when a new record is made?

      Thanks!

      Test_script.JPG

        • 1. Re: Running a Script when creating a new record
          philmodjunk

          It'd be useful to have such a script trigger in FileMaker, (along with a trigger for delete record), but we don't.

          If you have FileMaker Advanced you can write your own script for creating a new record and then create a custom menu that uses your script in place of the standard new record action.

          • 2. Re: Running a Script when creating a new record
            AndrewFoo

            Thanks.

            I can't remember how I did it, but I managed to get a copy and paste script to run automatically when a new record is made (copying ID number's to related tables).

            It was really simple when I did it, I just can't retrace my steps.

            • 3. Re: Running a Script when creating a new record
              philmodjunk

              You can always create a button for creating new records where you can also do additional steps such as you describe, but I know of no way you can do this automatically in FileMaker other than through the custom menu approach I've already described. (and I've been developing in FileMaker for many years Wink).

              PS. using copy and paste script steps is a less than optimum method for moving data around. Set Variable/Set Field combinations are a much better approach.

              • 4. Re: Running a Script when creating a new record
                AndrewFoo

                Thanks!

                Basically what I've done is I have a "Temporary Employee ID #" on the main table, and all the other tables are related with the same ID #. But I would like to create a record every time a record is made in the parent table (one-to-one relationship) so I have a script that copies the ID number from the parent table to all of the child tables.

                How should I implement your method?

                • 5. Re: Running a Script when creating a new record
                  philmodjunk

                  Here's one possibility:

                  Freeze window
                  Set Variable [$ID ; Value: YourParentTable::YourIDField]
                  Go To Layout [//layout to related child table]
                  New Record/Request
                  Set Field [YourChildTable::YourIDfield ; $ID ]
                  Go To Layout [original layout]

                  This keeps from using a copy action which would overwrite any data copied to the clipboard by your user--which will irritated them. Set Field and Set Variable will also continue to work correctly even if a field they refer to is not physically present on the layout--unlike copy, paste and insert script steps that will silently fail in those situations.

                  • 6. Re: Running a Script when creating a new record
                    AndrewFoo

                    Thank you!

                    I'll try it out

                    • 7. Re: Running a Script when creating a new record
                      AndrewFoo

                      @PhilModJunk

                      Just a couple things,

                      1. Regarding auto-running a script on a new record

                      On the database I have, under "layout mode" in the Layout settings section there's a part where it has a "script trigger".

                      I enabled "OnRecordLoad" and it seems to automatically run the script.

                      2. I tried creating a script similar to what you created, but it sent the program unto an infinite loop and now I have a bunch of empty records.

                      Is there a way to delete a mass amount of records at once?

                              I attached a .jpeg of the script in the original post.

                      Thanks again for all the help!

                       

                      • 8. Re: Running a Script when creating a new record
                        philmodjunk

                        1. Regarding auto-running a script on a new record

                        On the database I have, under "layout mode" in the Layout settings section there's a part where it has a "script trigger".

                        I enabled "OnRecordLoad" and it seems to automatically run the script.

                        That will fire the script everytime you navigate to this layout or move from record to record. I imagine that would run this script under a lot of cases where you wouldn't want it to run.

                        2. I tried creating a script similar to what you created, but it sent the program unto an infinite loop and now I have a bunch of empty records.

                        Is there a way to delete a mass amount of records at once?

                        I don't see any Jpeg of the script in your first post. I suspect the OnRecordLoad trigger you described or some other trigger you've set is involved in this loop as there is no loop shown in the actual script that I posted.

                        You perform a find to pull up all your blank records and then use Delete Found set in the Records menu to delete them all in one go--just make sure you don't delete more  records than you intend here.

                        • 9. Re: Running a Script when creating a new record
                          AndrewFoo

                          Just attached the jpeg

                          • 10. Re: Running a Script when creating a new record
                            philmodjunk

                            And there's no loop there, so if you are getting an infinite loop, there's probably a script trigger on one of the layouts that is involved here. If you have advanced, you can enable the script debugger before running this script, then step through it watching to see if any script triggers kick in an fire this script all over again.

                            Generally speaking, it's usually unecessary to create all these blank related records each time a new parent record is created. There are usually some pretty simple ways to create the new related records on an "as needed" basis. Entering data in a portal can automatically create a new related record or navigation buttons that take the user to a related record can create a blank related record if none exists--just to name two possibilities.

                            • 11. Re: Running a Script when creating a new record
                              AndrewFoo

                              Gotcha,

                              I removed the script trigger but to no avail (attempted to run the script independently).

                              If say an employee with and I.D. of #30 doesn't have qualifications under every category and no record is created in that related field and then a future employee with record ID #35 is entered and has those qualifications, the record in the related table would say #35 and not #30 correct?

                              Thanks

                              • 12. Re: Running a Script when creating a new record
                                philmodjunk

                                I removed the script trigger but to no avail (attempted to run the script independently).

                                What happened, what results, if any do you get?

                                If say an employee with and I.D. of #30 doesn't have qualifications under every category and no record is created in that related field and then a future employee with record ID #35 is entered and has those qualifications, the record in the related table would say #35 and not #30 correct?

                                Assuming they are related by ID number, yes. And this should not be a problem should it?

                                • 13. Re: Running a Script when creating a new record
                                  AndrewFoo

                                  It appears to create a related record but it doesn't copy over the ID #, only blank records are created.

                                  But each related table has a blank record.

                                  • 14. Re: Running a Script when creating a new record
                                    philmodjunk

                                    Just spotted a typo in your script.

                                    The 2nd line should read:

                                    Set Variable [$ID ; Value:  Temporary Employee Database::Temporary Employee Pool ID]

                                    You've got an expression entered into the repetition box and this is putting the data in the wrong repetition of the variable.

                                    1 2 Previous Next