Script patterns – Test then act


There are many times you need to test a condition and return different outcomes based on the result. In many of these, the condition results are exclusive — only one can be true.

Go to layout by FileMaker client

Let's consider a simple example where you are testing for the FileMaker client being used. We will navigate to a specific layout designed for the client. The clients tested and layout targets are as follows:

  • FileMaker Pro: go to layout "Pro"
  • FileMaker Go: go to layout "Go"
  • FileMaker Server or FileMaker Cloud: go to layout "Server"
  • FileMaker WebDirect: go to layout "WebD"
  • Other clients: go to layout "OtherClient"

The clients are exclusive. Only one client can be in use at any time.

Find at least three different ways to achieve the goal

As developers we often fall into the trap of doing things the same way each time. It is good to challenge yourself to find different ways of doing the same thing. The FileMaker platform is well known for providing multiple ways to do the same thing.

When you have exhausted your options, review them and decide which would be best to use in the particular use case.

#1: Use standard If/Else If

When most FileMaker developers look at the above example, they will default to a standard structured If statement.


If [ PatternCount ( Get(ApplicationVersion); "pro")]
 Go to Layout [ "Pro" (system) ]
Else If [ PatternCount ( Get(ApplicationVersion); "go")]
 Go to Layout [ "Go" (system) ]
Else If [ PatternCount ( Get(ApplicationVersion); "server")]
 Go to Layout [ "Server" (system) ]
Else If [ Get ( SystemPlatform ) = 4 // webdirect ]
 Go to Layout [ "WebD" (system) ]
Else
 Go to Layout [ "OtherClient" (system) ]
End If

Note that testing for FileMaker WebDirect requires the use of a different Get function. This distinguishes the specific web technology being used.

#2: Test client and go to layout by calculation

This second method is similar to the first but we will test the client before entering the If statement.


Set Variable [ $client ; Value: --see below-- ]
Go to Layout [ $client ]

The calculation used for the $client is:


Let ( [ 
 clientVersion = Get ( ApplicationVersion )
]; 
 Case ( 
  PatternCount (clientVersion; "Pro"); "Pro"; 
  PatternCount (clientVersion; "Go"); "Go"; 
  PatternCount (clientVersion; "Server"); "Server"; 
  Get ( SystemPlatform ) = 4; "WebD"; 
  "OtherClient" )
)

The calculation results deliberately match the names of the target layouts. This is then used to go to a layout by calculation. This sets up a layout name dependency and you must be careful in future about changing layout names.

More methods in the next post

So far, we’ve looked at two ways to achieve the same result. In the next post, we’ll find a third method (and possibly a few more) and compare the options.

Do you use a standard method for this? Let me know before tomorrow and I may be able to include it in the next post.


Are you late to the ScriptLogic party? If you have missed out on some of these emails, catch up here: Email Archive​

​

If you know someone else who you think might like this email, you can send them this link to subscribe: Subscribe to ScriptLogic Daily​

​