10 Replies Latest reply on Sep 26, 2013 2:38 PM by Mike_Mitchell

    Script to Validate Unique Entry?


      Hi All!


      Thank you in advance for your help! Please pardon me if this is repeating something on the forums - tried to find what I needed but couldn't quite get where I need to be.


      I have a Filemaker Go layout for warehouse personal who pull materials for custom manufactured jobs. I have a JobNum field which needs to have a unique job number. They need to search this field and if no records exist, create a new record. Originally I used the Filemaker Validation to make sure this value was unique, and that worked okay. Now though, I prevent field entry on this field in browse mode on the main layout, so that they can't accidentally modify the job number of an existing job. To create a new job, they click a "New Order" button which takes them to a new layout with one field for the Job Number.


      They enter the job number here to create the record, then click a "CREATE ORDER" button which runs a script to commit the record and send them back to the original layout with order details. The only caveat to this, is that with the built-in Unique Validation, it brings up multiple pop-ups asking about reverting records, etc. and then pop-ups re: stopping the script that runs to commit and send them back to the main layout, etc.


      What I want to do is this: Have them enter a job number and click the "Create" button and run a script that looks for that job number. If that job number exists, it kicks them to the proper releated record for that job number. If that job number ISN'T found, I want it to create the record, then send them to the related record in the proper layout. I have it working 99% with the script I've pasted below.


      My only issue with the below script is that after the find (testing this on my desktop mind you), there's a dialog box that says "No Records Match This Criteria" with 3 buttons: "Modify Find", "Continue" and "Cancel". If I hit continue, it creates the record and goes to the proper layout as it should. Ideally, I'd like to supress this dialog box and just proceed with the script...



      Set Variable [ $temp; Value:Orders::JobNum ]

      Perform Find [ ]

      If [ Get ( FoundCount ) = 0 ]

      New Record/Request

      Set Field [ Orders::JobNum ; $temp ]

      Commit Records/Requests

      Go to Related Record [ From table: “Orders”; Using layout: “MatTrack_WH” (Orders) ]

      Halt Script


      Go to Related Record [ From table: “Orders”; Using layout: “MatTrack_WH” (Orders) ]

      Halt Script

      End If



      Thank you again for your help!



        • 1. Re: Script to Validate Unique Entry?

          Bradley -


          What you want here is the Set Error Capture script step. It causes FileMaker to suppress all normal error dialogs, relying on you (the developer) to handle error notifications to the user instead.


          (Did you catch that? The part about relying on you to handle error notifications for ALL errors? More on that in a moment.)


          What you'll want to do is modify your script slightly:


          Set Variable [ $temp ; Orders::JobNum ]

          Set Error Capture [ On ]

          Perform Find

          Set Variable [ $err ; Get ( LastError ) ]

          Set Error Capture [ Off ]

          If [ $err = 401 ]     // This means no records were found

            New Record / Request


          Else If [ $err = 0 ]

            Go to Related Record

            Exit Script   // In general, you want to use "Exit Script" instead of "Halt Script" because of the possibility of leaving the system in an indeterminate state


            Show Custom Dialog [ "Error" ; "An unidentified error has occurred. Contact tech support." ]

            Exit Script [ $err ]

          End If


          Note that last leg. Basically, this traps everything other than the "I didn't find that record" error. It lets the user know that something went wrong that isn't specifically planned for. It's a good practice to put something like this in because, as I said before, when you turn on the error capture, you've disabled FileMaker's ability to let your user know about anything that went wrong.


          You can also write any errors to a log for later review. This is a good practice; it allows you to go back and figure out what blew up (and something always blows up at some point).    





          • 2. Re: Script to Validate Unique Entry?

            Thanks for that, Mike!


            I tried used the Error Capture originally but couldn't get it to work. Turns out I wasn't setting a variable and was just trying to use the Get(LastError) on it's own, which is probably why it didn't work! Should've know better from making custom dialogs, but C'est la vie!


            I'll try what you've suggested this afternoon.


            Re: Halt Script vs. Exit Script - I learned that in the classes I took (about Exit being better), but in some of the Validation scripts I've written that kick users back to a field/object to enter or correct information, it still leaves a line at the top like the script is paused and you have to click the light blue "Continue" button in the upper right to finish the script out...


            If I use the halt scrip step, that doesn't happen...


            Any idea what I'm doing wrong there?


            Thanks again for your help! Truly appreciated!

            • 3. Re: Script to Validate Unique Entry?

              The "Continue" button will appear if you've paused the script. That can happen as a result of the Pause Script script step, or if the button the user has clicked / tapped is set to pause the current script. You're correct in noting that Halt Script will stop this behavior, because it's like slamming on the brakes. But then, do you really want to slam the brakes on a train at full throttle?   


              Probably better to fix the problem in the engine compartment first.   





              • 4. Re: Script to Validate Unique Entry?

                The only issue is that I can't find what is pausing the script...I include the "Exit Script", first leaving the "result" blank. Then tried changing the result to "1", 1, "0", 0 and "" and nothing exits the script...


                The only thing that even has a "Pause" option is the EnterBrowseMode, but I have the "Pause" unchecked...


                I don't know what's up with what I'm doing, but this has happened on other validation scripts I have in place and the only thing that works is Halt.


                Any pointers?


                Thanks again!



                • 5. Re: Script to Validate Unique Entry?

                  I might not be in the script. Are there any buttons involved? Check them and see if they have the "Pause" option enabled.


                  Another thing to look at is, if this is iOS, the user tapping the screen while the script is running. If that happens, it will allow the user to pause the script. So you'll want to Set User Abort at the top of the script.

                  • 6. Re: Script to Validate Unique Entry?

                    Not going to lie, Mike...I'm feeling pretty unintelligent right now, haha! It was in the button...you were totally right. Jeez...always the simple stuff!


                    I will make a note set the user abort at the beginning of that script.


                    I'll try the rest of what you mentioned later this afternoon and will let you know how it works.


                    Again, MANY MANY thanks. Truly invaluable and very much appreciated!



                    • 7. Re: Script to Validate Unique Entry?

                      Alright, Mike...


                      Used your additions re: Error capture and it worked perfectly. Thank you again!


                      One quick question and then I promise I won't bother you again until next time:  "Exit Script [ $err ]" at the end...


                      This just exits the script if there's an error other than 401 or 0, correct?


                      Thank you again! Many many thanks!



                      • 8. Re: Script to Validate Unique Entry?

                        Just chiming in on the Halt versus Exit question. A major difference is that Exit will end the script this step is within, whereas Halt will stop any script which is currently running. So if the script has been called by another script the subscript will stop and the calling script will resume if Exit is used within the subscript, but both the subscript and the calling script will stop if Halt is used within the subsript.

                        • 9. Re: Script to Validate Unique Entry?

                          Almost. It does exit the script on any error other than 401 or 0. However, it also passes the error back to the calling script. This allows you to use Get ( ScriptResult ) to determine what happened. This is another good practice - when you use a script as a subscript, set it up so that, if anything goes kerflooey (that's the technical term), you pass back the error condition to the calling script.



                          • 10. Re: Script to Validate Unique Entry?

                            Precisely. This is one of the primary reasons for avoiding Halt Script; it kills not only the current script but the entire script chain (and may therefore leave you in a very indeterminate state).