Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Now that you have a record number on the Lease Agreement List layout, and have sorted the list, you can start to see how useful the layout really is. The Lease Agreement layout is great for revealing detail, but when the Lease Agreement List layout is sorted by Last Name, it's a cinch to scroll to the record you need without entering Find mode, typing in search criteria, and then performing a find. But it could be easier sort the records on the layout. As it is, you've got to choose Records→Sort Records, select the field you want to sort by, and then click Sort. All this stuff is easy, but efficiency is king in your world. The solution is to write a script to do these things automatically.
If you're familiar with macros in other programs, then you already get the idea of scripts in FileMaker—you set up scripts to perform tasks for you. The task at hand—sorting—is just one command, but it has several steps. They're all quick steps, but when you have to repeat them several times a day and so does everyone else in your office, all that wasted time adds up to real inefficiency. Also, any manual process leaves room for human error. When you make a mistake, no matter how harmless, you have to undo or redo what you just did. A script that handles your sort is more efficient and less susceptible to error.
Here's how to write a script that sorts the records on your list layout alphabetically by Last Name and then First Name:
Choose Records→Sort Records. Set up the window to sort by Last Name and then First Name (Section 1.6), and then click Sort.
Every time you open the Sort window, the last sort order is already in the window. It works the same way for scripts. So save yourself some time by performing the sort first. That way, when you write the script, the order will already be in the Sort window.
Click New.
The Edit Script dialog box appears (Figure 4-20).
In the View pop-up menu, choose Found Sets.
The filters the list of script steps so you can easily pick the one you need.
Double-click the Sort Records script step.
The Sort Records script step appears in the window's Script pane.
Turn on "Perform without dialog".
Without this option selected, you'd see the regular Sort window every time you run the script. Don't turn on this option when you're writing a script that lets the user choose a custom sort as the script runs.
Turn on "Specify sort order".
The regular Sort window appears, with Last Name and then First Name set up already. As you know, FileMaker remembers your most recent sort order, but it's good practice to verify everything when you're scripting. And if you wanted a different sort order, you can change it and the script will remember your changes.
Click OK to close the Sort window, and then, in the Edit Script window, click Close.
FileMaker asks if you want to save the script's changes.
Click Save.
FileMaker saves the script and closes the Edit Script window.
Make sure "Include in Menu" is turned on for the Sort by Last Name script, and then close the Manage Scripts window.
Now that the Manage Scripts window is closed, you need some way to run the script you just wrote. Look in the Scripts menu. It appears there, along with a shortcut. You can run the script by choosing Scripts→Sort by Last Name or by using the shortcut.
Tip:
Windows users can save scripts before closing the Edit Script window by choosing the Edit Script window's File→Save Script command. Both PC and Mac fans can use the Save Script shortcut Ctrl+S (⌘-S).
Now that the script is ready to go, Unsort your records (choose Records→Unsort), and then run the script.
Running the script from the menu saved you a few steps, but you can make it even more convenient by attaching the script to a layout object, which then becomes a button. Then whenever you click the button in Browse mode, the script runs automatically. Here's how:
In Layout mode, click the Last Name Field's label to select it.
It's a common convention to click a column label to sort a column, so help your users out by adopting that principle.
The Button Setup window appears (Figure 4-21).
In the script step list, click Perform Script.
This "controller" script step lets you run any script you've written by attaching it to a button.
Switch to Browse mode, and then Unsort your records, if they're sorted. Finally, click the Last Name field label to see the script run. You haven't put anything on your layout to indicate to your users that the field label does anything useful. FileMaker changes the pointer to a hand icon when it's positioned over any button, but you have to give users a reason to wander up there with their mouses. So change the label's formatting (make it a contrasting color, or put a border around it so that it looks like a button) to help users out. (Learn more about buttons on Section 7.6.)
Tip:
Check out this chapter's Lease Agreement Finished.fp7 file to look under the hood at some formatting options and a beefed up script that can sort by different fields, depending on a script parameter (explained on Section 16.5).
The script was nice, and the button improved things, but you're still not done learning how useful and intuitive scripts can be. Since the point of going to the list layout is to quickly scan a list so you can find a particular Lease Agreement record, it'd been even more convenient if the list just knew to sort itself every time you switch to the layout. And that kind of thing is what Script Triggers are for.
You've just seen that you can run a script from the Scripts menu or from a button. But you can also tell a script to run when you do other things, like enter data in a field or go so a specific layout. Here's how to make the Sort by Last Name script run every time you go to the Lease Agreement List layout:
If you're not viewing the Lease Agreement List layout, switch to it. Then in Layout mode, choose Layouts→Layout Setup.
You'll learn about this dialog box's other options in Chapter 7. For now, you're interested in the Script Triggers tab.
Click the Script Triggers tab.
The Script Triggers tab appears (Figure 4-22).
Click the "Sort by Last Name" script to select it, and then click OK.
The Script Trigger tab is now set up. Notice that you're only enabling the script to run while you're viewing the layout in Browse mode.
Click OK.
The script will run each time you switch to the Lease Agreement List layout.
To test the script trigger, Unsort your list, and then switch to the Lease Agreement layout. Then switch back to the Lease Agreement List layout. The script runs and sorts your list for you.