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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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
  6. 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.
  7. 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.
  8. 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.
  9. 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:
    1. 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?”]
    2. 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?”]

7 Comments