Category Archives: Custom Functions

  • 1

Multiple Parameters for Scripts

A common complaint about FileMaker (https://www.geistinteractive.com/2014/04/07/top-feature-request-filemaker-14/) is the lack of a native method to pass multiple parameters to a script. Many developers have tackled this problem using different methods, including include Word Count, Value Count, delimiting characters, and creating a dictionary. Read more at (http://www.merlyn383.com/home/2011/7/27/filemaker-passing-multiple-script-parameters.html). One of the most straightforward, stable and easy to use is a custom function from SeedCode (http://www.seedcode.com/pmwiki/pmwiki.php?n=SeedCodeComplete2.ScriptParameters).

For simplicity and clarity, we started with the SeedCode custom function, but changed the name from SeedCode_GetScriptParameter to MultiParam. The new name name is shorter, to the point, and reminds you what the function does. At DBHQ, we use this custom function in every database we create. It’s a building block which will be used in other script examples on this blog.

In fact, because we do use MultiParam in every database we create, you’ll probably see us employ it even when a script only needs one parameter and Get ( ScriptParameter ) would suffice. The logic behind this is that it’s much easier to use MultiParam (even when you only have one parameter) than to go back and retrofit if you later decide that your script is going to need more than one parameter in certain circumstances.

The function lets you create name/value pairs, separated by a semicolon. First you pass the values into the script using the “optional script parameter” field when you specify the script you’re running. Type:

“action = start ; status = active”

In the code above, the word “action” is the name of a thing you’re passing. Its value is “start.” The semicolon marks the end of the first pair. Note that the spaces around the “=” and the semicolon are for legibility. If you just run the code together, the custom function will still work, but your parameter isn’t as easy to read.

After you’ve passed the parameters, you’ll use Set variable script steps, one for each parameter you’ve passed, to read and store the values within a script:

Set variable [$action = MultiParam ( “action” )]
Set variable [$status = MultiParam ( “status” )]

 

In this example, $action is will have a value of “start” and $status will have a value of “active”.

To use a field name in the script parameter instead of static text, use standard FileMaker concatenation:
” action = start ; status = ” & Customer::Status

You can string together as many parameters as you need for your script. Here’s one with three parameters, using a field in the second to illustrate how to construct the parameter when you introduce a field name in the center of it.

“action = start ; status = ” & Customer::Status & ” ; state = ” & Customer::State

Refer to “Creating Custom Functions” (ADD LINK) to see the steps to add this custom function to your database.

The MultiParam function itself has one parameter (Name). The MultiParam function should be made available to all accounts.

—————-BEGIN CODE—————————
/*

Name:
SeedCode_GetScriptParameter

History:
Created by John Sindelar, SeedCode LLC, based on Anton Anderson’s FileMaker Advisor Article of Dec 2005.
www.seedcode.com
Creation Date: 26 Dec 2005
Last Modified Date: 21 Jan 2006

Purpose:
Returns just the named parameter when multiple parameters are passed into a script.

Parameters:
Name

Example:
SeedCode_GetScriptParameter ( “Operation” ) returns “begin” when the script parameter is “Operation = Begin ; Status = Estimate”.

Requires Other Custom Functions:
None

Other Notes:
Where “Name” is the name in a Name/Value pair like: “Operation = Begin”.
Separate Name/Value pairs with semicolons like: “Operation = Begin ; Status = Estimate”.
Quotes are optional in the value: “Operation = Begin ; Response = \”OK\””
Fields can be entered like this: “Operation = Begin ; Status = ” & job::status
If a requested value is not present, the result is blank.

Options:
None

*/

Let ( [
string = Substitute ( Get ( ScriptParameter ) ; [ “\”” ; “^^” ] ; [ ” ;” ; “;” ] ; [ “;” ; “\” ;” ] ; [ “= ” ; “=” ] ; [“=” ; ” = \”” ] ; [“¶” ; “~~” ] ) & “\”” ;
eval = Evaluate ( “Let ( [” &  string  & “] ;” & Name & ” )” ) ;
result = Substitute ( eval ; [ “^^” ; “\”” ] ; [ “~~” ; “¶” ] )
] ;

If ( result =”?” ; “” ; result )

)

—————-END CODE—————————


  • -

Creating Custom Functions

When we provide instructions for creating a custom function, we will specifically provide any Parameters which need to be defined, and also provide the code for the function.

The code will be indicated with the following beginning/ending indicators:

———–BEGIN CODE—————————

The actual code will be here. Do not copy the “Begin Code” and “End Code” lines, but do copy everything between them.

———–END CODE—————————

To create the Custom Function, select File—> Manage—>Customer Functions.

 

The Custom Function dialog box appears. Click “New.”

 

In the Edit Custom Function dialog box, enter the Function Name in the top left field.
In the Function Parameters field, enter each parameter separately and click the “+” button to add it to the list.
In the large field that encompasses the bottom half of the dialog box, paste the code we have provided.
Be sure to indicate whether the function should be available to “All accounts” or “Only accounts assigned full access privileges.”

 

Click OK to close all dialog boxes.