7 Replies Latest reply on Aug 16, 2016 1:30 PM by DavidGunter

    Script trigger problem: endless loop when triggered

    DavidGunter

      This is a problem I'm having with script triggers. I have created this script for updating a bank register. The fields are check, deposit and balance.

      $bal is set to the opening balance. The script is called to recalculate the current balance on the same record after a check or deposit is manually entered, just like a bank register.

      Go to layout ["bankregister"]
      Got record/Request/Page/First
      Loop
      Set variable[$bal;Value:bankregister::balance]
      Go to record/Request/Page[Next;Exit after last]
      Insert Calculated Result [Select;bankregister::balance; $bal-          bankregister::check+bankregister::deposit]
      EndLoop

      Works great when the script is manually selected to run, but when I use any sort of a trigger
      it runs into the endless loop, making it useless. The trigger would ideally be when a deposit or check is entered manually, then commited with the return key. Another trigger could be a radio button with 'account type' ('current' or 'checking' are the values here)

      Any ideas? Thanks in advance

       

        • 1. Re: Script trigger problem: endless loop when triggered
          SteveMartino

          Wouldn't it be easier (or did you try) just making the balance field a calculation field.

          As far as triggers, which ones have you used that didn't work?

          • 2. Re: Script trigger problem: endless loop when triggered
            David Moyer

            Hi,

            I'm guessing that you have two triggers firing the same script.  Like a layout-level trigger on the "blankregister" layout; and/or an object-level trigger on the "balance" field.

            • 3. Re: Script trigger problem: endless loop when triggered
              DavidGunter

              Using a calculated field  does not give the correct retrospective, meaning that every balance for every historical record reads as  the current  balance. The script performs perfectly by placing each new balance after a deposit or check by recalculating the entire set if transactions on each run.

               

              I used the script trigger menu in layout setup using both commit and enter and also the set script trigger under format, which may be the same thing. All attempts result in an endless loop.

               

              I think the script keeps getting triggered inside the loop, but for some reason does not when I just run the script manually.

              • 4. Re: Script trigger problem: endless loop when triggered
                DavidGunter

                Thanks, but thats not the case.

                • 5. Re: Script trigger problem: endless loop when triggered
                  Johan Hedman

                  Do you use FileMaker Pro Advanced? With that software you are able to use the Debugger to catch all Triggers that you shot out during your script/layout changes. Very good tool in many ways if you dont have it.

                  • 6. Re: Script trigger problem: endless loop when triggered
                    Mike_Mitchell

                    DavidGunter wrote:

                     

                    Go to layout ["bankregister"]
                    Got record/Request/Page/First
                    Loop
                    Set variable[$bal;Value:bankregister::balance]
                    Go to record/Request/Page[Next;Exit after last]
                    Insert Calculated Result [Select;bankregister::balance; $bal- bankregister::check+bankregister::deposit]
                    EndLoop

                    ...The trigger would ideally be when a deposit or check is entered manually, then commited with the return key.

                     

                    Every time you go to a new record, the Commit event fires. Is your trigger set to OnRecordCommit? If so, then that's your problem. You've created an unbounded recursive loop, which fires this script every time you go to a new record (a loop within a loop within a loop ...)

                     

                    What you can do is set a global variable and then trap the trigger so it fires only once. For example:

                     

                    If [ IsEmpty ( $$noTrigger )

                         Set Variable [ $$noTrigger ; Value: 1 ]

                         Go to layout ["bankregister"]

                         Go to record/Request/Page/First

                         Loop

                              Set variable[$bal;Value:bankregister::balance]

                              Go to record/Request/Page[Next;Exit after last]

                              Insert Calculated Result [Select;bankregister::balance; $bal - bankregister::check+bankregister::deposit]

                         End Loop

                         Set Variable [ $$noTrigger ; Value: "" ]

                    End If

                     

                    (Note: Instead of using Insert Calculated Result, it's considered a best practice to use Set Field instead. That way, your field doesn't have to be on the layout for it to work properly.)

                     

                    HTH

                     

                    Mike

                    2 of 2 people found this helpful
                    • 7. Re: Script trigger problem: endless loop when triggered
                      DavidGunter

                      Mike, you nailed it! The script now works exactly as intended. Thanks so much.