In a script, the active layout is very important, since it's what determines the script's context. Context is critical when you do things like delete records. If you write a script that simply deletes a record, it deletes the current record from whatever layout the user happens to be onwhich may not be the right record at all. You could end up deleting a record from the Customer table instead of the single Invoice record you meant to delete!
Take the script you've created in this chapter, for example. Before it does anything else, your Find Unpaid Invoices script goes to the Invoice List layoutbut what would happen if you left that step out? Figure 13-7 shows the options ScriptMaker gives you for the Perform Find script step. See if you can spot what's missing.
|
Upon close examination of Figure 13-7, you can see that Perform Findlike every other script stepmakes absolutely no mention of which table it should act upon. Just because you're asking it to "Find records when Invoices::Invoice ID > 0" doesn't mean you're looking for invoice records. You could be looking for customers whose attached invoices have a balance due. Or you could be looking for any line items that are on an unpaid invoice. The only way FileMaker can tell what you're looking for is by the context of the layout you're on. That's why the first step in the script goes to the Invoice List layout. That layout is attached to the Invoices table occurrence. This tells FileMaker which records to look through (those in the Invoices table) and from what perspective to resolve relationships (the Invoices table occurrence on the relationship graph).
13.2.1. When to Switch
Now that you've dutifully absorbed the lesson in the previous paragraphs, don't get lured into the idea that you always have to switch to a layout for your script to work. The fact that a script can do something useful from more than one context can be a good thing. In general, you have three choices when you write a script, and here are some guidelines on when to use each:
Tip: Anytime a script can do damage to your database when run from the wrong layout, you may want to use that last option even if you're the only person who uses your database. If your mouse hand slips when you're insufficiently caffeinated, the script can show you an error message rather than running at the wrong time (Section 15.2.1).
Part I: Introduction to FileMaker Pro
Your First Database
Organizing and Editing Records
Building a New Database
Part II: Layout Basics
Layout Basics
Creating Layouts
Advanced Layouts and Reports
Part III: Multiple Tables and Relationships
Multiple Tables and Relationships
Advanced Relationship Techniques
Part IV: Calculations
Introduction to Calculations
Calculations and Data Types
Advanced Calculations
Extending Calculations
Part V: Scripting
Scripting Basics
Script Steps
Advanced Scripting
Part VI: Security and Integration
Security
Exporting and Importing
Sharing Your Database
Developer Utilities
Part VII: Appendixes
Appendix A. Getting Help