Now that you’ve written a script to sort your Main List records, let’s make the script run automatically whenever the user views the layout, ensuring that the Last Initial subsummary part is displayed consistently. This automation is accomplished by using a script trigger.
Script triggers cause scripts to execute when certain events take place, such as when a layout loads, a window opens, or when a field value is changed via the interface. FileMaker Pro provides many different script trigger options to choose among. They are associated with the following events:
- Files or windows being opened or closed
- Various actions being performed on a layout such as loading the layout or entering Browse mode
- Various actions being performed on layout objects such as fields or buttons
- A given interval of time passing
It’s beyond the scope of this training to cover script triggers comprehensively. For more detailed information see this help page.
In this case, you’ll use the OnLayoutEnter trigger, which causes a script to run when you change from one layout to another. Note that deciding what trigger to use takes thoughtful evaluation — click here if you’d like to know why we chose this one.
Associate a script trigger with the Main List layout that causes the "Sort Contacts" script to run whenever the user switches to this layout or enters Browse mode.
Set up the script triggers
- Make sure you are viewing your Main List layout in Layout mode.
- Choose the Layouts menu > Layout Setup…
A Layout Setup dialog window appears.
- Switch to the Script Triggers tab.
- Select the OnLayoutEnter option from the Event list.
A Specify Script dialog window appears.
- Select your new sort script.
In the example solution, the script is “Sort Contacts”.
- Select only the Browse and Preview checkboxes for the Enable In option.
The script will only be triggered for those modes that you select. Since sorting is not relevant for Find mode, it’s best not to select it. You get a small performance improvement by not calling the script unnecessarily.
- Click the OK button to dismiss the Specify Script dialog window.
- Click the OK button to dismiss the Layout Setup dialog window.
Test the script trigger
- Choose the Records menu > Unsort
The records are returned to their original unsorted state and the subsummary part disappears. If this menu command is grayed out, the records are already in this state.
- Switch to your Main Detail layout.
- Switch back to your Main List layout.
When you enter the Main List layout, your script trigger causes your sort script to run, sorting the found set and causing the Last Initial subsummary part to be displayed.