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

  1. Patroba Ambale
    Posted October 11, 2011 at 5:02 am | Permalink

    Hey,
    Thanks for this blog,the above article has helped me solve an issue that has been pending for sometimes,Thanks for coming up with a blog that will surely mold the next generation of FileMaker Developers.

  2. Posted October 11, 2011 at 9:54 pm | Permalink

    Glad to help Patroba. Thanks for the kind words!

  3. Erik
    Posted October 23, 2012 at 1:35 pm | Permalink

    Good stuff!

  4. robert jesus Land Tidd Lewis
    Posted January 28, 2013 at 12:28 pm | Permalink

    i was searching web, to verify, if looping portals offers greater performace THAN using calculations? Seems when using calculations requires more window refreshing!?

  5. Posted January 31, 2013 at 10:09 pm | Permalink

    Looping portals would most definitely NOT offer greater performance than calculations. When you loop through a portal, you are still using the calculation engine but with the added overhead of the portal looping in the interface. If a calculation is efficiently written, it will out perform portal looping every time. I am not sure what you mean by “window refreshing” though. It’s not possible for a calculation to cause a window refresh. Am I missing something in your question?

  6. Bob Racinowski
    Posted January 11, 2014 at 1:56 pm | Permalink

    Hey Danny, What a great blog. I have looked at your tips on set variables in scripts and have picked up some get ideas. Thank you again.

  7. jade
    Posted July 30, 2015 at 10:21 pm | Permalink

    I”m looking for info on HOW to do If status = and if container = print results

    Please help direct me on how to do that. Or even a website that breaks it down pretty simple..like me. :)

    Thank you…

Post a Comment

Your email is never shared. Required fields are marked *

*
*