When a script runs and encounters an error, FileMaker usually displays an error message on the screen. Using the script step Set Error Capture, you can replace this generic statement with a message that not only alerts you to the problem, but provides options for a solution. For this example, we create a message prompt that appears when no records are found by a search script.
To show error messages with customized choices
1 .Choose Scripts > ScriptMaker (Control+Shift+S/Command+Shift+S). Double-click a script with a Perform Find step in it, or create a new one. Our example is called Find Date (Figure 9.51).
Figure 9.51. This script goes to a layout, enters Find mode, and performs a new search.
2 .In the script-assembly list, select the script step above the steps you want. In our example, we select the step above the Enter Find Mode step (Figure 9.52).
Figure 9.52. New steps are inserted after the selected step.
3 .In the step list on the left, double-click Set Error Capture. By default, On is selected under Script Step Options (Figure 9.53).
Figure 9.53. Use Set Error Capture to suppress FileMaker's own messages.
Turning on Error Capture tells FileMaker to take note of the error code for any error that occurs.4 .While the Set Error Capture step is selected in the script-assembly list, double-click Loop in the step list on the left.5 .Click the double arrow to the left of the End Loop step and drag it above the last step in the script (Figure 9.54).
Figure 9.54. Loop will repeat the steps before End Loop.
The Loop step indents anything you put between it and End Loop, so you can easily see what takes place inside the Loop.6 .In the script-assembly list, select the Perform Find step. In the step list on the left, double-click Exit Loop If (Figure 9.55). In Script Step Options, click Specify to open the Specify Calculation dialog box.
Figure 9.55. Exit Loop If must occur after the step or steps that might create an error.
7 .In the Specify Calculation dialog box, choose "Get functions" from the View drop-down menu on the right to access the detailed list of Get functions (Figure 9.56).
Figure 9.56. Choose Get functions to see a list of only this function category.
8 .Double-click the Get(LastError) function (Figure 9.57). Click to the right of the formula and type =0 (Figure 9.58). Click OK.
Figure 9.57. Get(LastError) looks to see if anything has gone wrong.
Figure 9.58. Get(LastError) is 0 if there is no error.
The script is now set to exit the loop if there is no error.9 .Scroll down to the Miscellaneous section of the step list on the left, and double-click Show Custom Dialog. Under Script Step Options, click Specify.10 .When the "Show Custom Dialog" Options dialog box appears, type your custom error message in the Message box and add a title for the dialog box if desired.11 .Double-click in the Default Button box and replace the OK with "Try Again," or another short phrase that will fit on the button. Leave Cancel in the Button 2 box. Click OK (Figure 9.59).
Figure 9.59. You can customize the message and the buttons that will appear in the error dialog box.
When you create a search that turns up no records matching your find criteria, your custom error message will be displayed (Figure 9.60).
Figure 9.60. Your message will appear when a user uses search criteria that don't match the records in the file.
12 .In the step list on the left, double-click If. Under Script Step Options, click Specify to access the Specify Calculation window.13 .From the View drop-down menu, choose "Get functions," then double-click the Get(LastMessageChoice) function (Figure 9.61).
Figure 9.61. Get(LastMessageChoice) will tell the script which button was clicked.
14 .Click to the right of the parentheses and type =2. Click OK.The second message button is Cancel, so this script step specifies that if you choose Cancel in the dialog box, the If step will end.15 .In the Found Sets section of the step list on the left, double-click Show All Records.16 .In the Navigation section of the step list on the left, double-click Go to Layout.17 .In the step list on the left, scroll up to the Control section and double-click Exit Script (Figure 9.62).
Figure 9.62. If the second button (Cancel) is clicked, the script will exit the loop and return you to the original layout, with all records displayed.
The three steps you just added specify that if you choose Cancel, the script will show all records, return you to the layout, and exit the script so you can try again.After the End Loop, you can add any commands you want to take place after a successful find.18 .When you're finished, click OK twice.