Faster, Maintainable Scripts with Variables
9 FileMaker Pro Tips using the Set Variable script step to make your scripts more efficient, portable and easier to maintain.
- Use variables instead of global fields. Variables are stored in active system memory while fields are stored on disk. Therefore, accessing a variable is much faster than accessing fields. So, use global variables instead of global fields wherever possible except under the following circumstances:
- global data is needed to define a relationship,
- a user must input or modify the global data.
- Improving performance in looping calculations. The best way to improve the performance of a complex, time-consuming script is to make the loops more efficient. One way to do this is to use local variables to store data to be manipulated in a loop. When the manipulations are complete, copy the data back to a field if it is needed there.
- Managing script parameter contents. When writing a script that accepts a parameter, set a variable with a descriptive name at the beginning of the script with the contents of the parameter (or multiple variables if input is being parsed for multiple delimited values). Use that variable in the script where the data is needed instead of using Get ( ScriptParameter ). This will make the script much easier to understand and future maintenance much easier.
- Use variables with global fields. When writing a script that grabs data from one or more global fields, copy the value of the global fields to variables as the first step or steps in the script. This improves the script’s portability and maintainability, because if you ever have to change the data source, you will only have to change the first few steps in the script. It improves readability of the script, because you can instantly see what fields the script is using by glancing at the first steps.
- More flexible portal looping. When looping through a portal, instead of using Go to Portal Row [Next; Exit after last], consider using an incrementing variable with Go to Portal Row by calculation. This allows you to Commit records and/or move to different layouts or objects without losing the portal row you are on. The script would appear as follows:
Set Variable [ $i ; 1 ]
Loop
Exit Loop If [ $i > Count ( /* primary key of related record */ ) ]
Go to Object [ /* portal object name */ ] (needed if layout has multiple portals)
Go to Portal Row [ No Dialog ; $i ]
…
Do loop operations.
…
Set Variable [ $i ; $i + 1 ]
End Loop
- More flexible record looping. Use the above technique for looping through records as well, thus allowing you to jump to different layouts, records and/or found sets without losing your place or having to open multiple windows.
- Manage smarter sorting operations. Use global variables to store and manage sorting operations. In a future article, I will describe a method of producing column headers that toggle sort order when clicked that uses global variables rather than global fields.
- Create new records more efficiently. When scripting the creation of new related records, copy the value of the primary key to a variable, go to a layout of the related table, create a new record and copy the contents of the variable to the foreign key field. Finally, return to the original layout. With the use of the Freeze Window step, this process can be completely invisible to the user, and you will not have to use the “Allow creation of records in this table via this relationship” feature which causes a blank record at the bottom of your portals which may be awkward and confusing to some users.
- Parsing lists or other conglomerate data. Scripts become difficult to read and maintain when field and/or parameter data is parsed within script steps, especially when the parsed data will be used more than once. Use variables to hold parsed data. Consider the readability of the following script clips which perform the exact same function:
-
Set Variable [ $FirstName ; LeftWords ( Contacts::Name ; 1 ) ]
Set Variable [ $LastName ; RightWords ( Users::Name ; WordCount ( Contacts::Name ) – 1 ) ]
Show Custom Dialog [“Hello ” & $FirstName ; “Dear Mr/Ms ” & $LastName & “, Do you want to add yourself to the ” & $LastName & ” family group?”]
-
Show Custom Dialog [“Hello ” & LeftWords ( Contacts::Name ; 1 ) ; “Dear Mr/Ms ” & RightWords ( Users::Name ; WordCount ( Contacts::Name ) – 1 ) & “, Do you want to add yourself to the ” & RightWords ( Users::Name ; WordCount ( Contacts::Name ) – 1 ) & ” family group?”]
This entry was written by
Danny, posted on
February 26, 2009 at 1:15 am, and filed under
Scripting.
7 Comments