4.3 Advanced scripting techniques
Now that you've mastered the basics of scripting in FileMaker, it's time to move on to advanced techniques that will help you develop more complex and robust applications. In this tutorial, we'll focus on error handling in scripts, debugging and documenting scripts, and creating complex workflows with multiple scripts. These techniques will improve your ability to create powerful and reliable FileMaker applications.
Table of contents
- 4.3 Advanced scripting techniques
- Error handling in scripts
- FileMaker ERP with clear and fully documented script structure and developer module.
- gFM-Business Open Source FileMaker Basis ERP The software for a crash course
- Create complex workflows with multiple scripts
- Use of partial scripts and script parameters in FileMaker
- Pass multiple script parameters
- Advantages of partial scripts and script parameters
- Tips, instructions and interfaces for FileMaker and gFM-Business ERP
- Frequently asked questions about advanced script functions
Error handling in scripts
Introduction to error handling
Error handling is an essential part of any well-developed script. In FileMaker, this means that your Script so that it can react to unexpected situations, such as when a file is not found, a calculation fails or a user makes an invalid input. Effective error handling ensures that your script does not simply "crash", but instead recognizes the error, reacts appropriately and informs the user.
Basics of error handling
- Error detection through
Set error recording: With the script stepSet error recording [On]you can prevent FileMaker from automatically displaying an error message when an error occurs. Instead, you can handle the error handling yourself. - Check the error status with
Get(LastErrorNo): You should check the error status after each critical step in the script.Get(LastErrorNo)returns an error code that you can enter in aWhen-condition to react to the error. Example:Set error recording [On]
Open file ["Database.fmp12"]
If [Get(LastErrorNo) ≠ 0]
Show custom dialog box ["Error"; "The file could not be opened."]
Exit current script
End If - Insert error handling stepsIf an error is detected, you can add appropriate steps to fix the problem or notify the user. This could include undoing changes, saving an error log or suggesting an alternative action.
Advanced error handling
- Using loops for retries: In some cases, it may be useful to make several retries in the event of an error before terminating the script. Example:
Set variable [$Vtry; value: 1]
Loop
Open file ["Database.fmp12"]
If [Get(LastErrorNo) = 0]
Exit loop when [Get(LastErrorNo) = 0]
Otherwise If [$attempt ≥ 3]
Show custom dialog box ["Error"; "The file could not be opened after 3 attempts."]
Exit current script
End IfExit loop if [$attempt > 3]
Set variable [$attempt; value: $attempt + 1]
End of loopError loggingIn complex applications, it can be helpful to keep an error log in which details of errors that have occurred are saved. This can be done in a separate error log table in your FileMaker database.
Example:
If [Get(LastErrorNo) ≠ 0]Go to Layout [Name: "Error log"]
New data record/query
Set field value [ErrorLog::Date; Get( SystemTimestamp )]
Set field value [ErrorLog::ErrorCode; Get(LastErrorNo)]
Set field value [Error log::User; Get( AccountName )]
Save data set/query [With dialog: Off]
End If
FileMaker ERP with clear and fully documented script structure and developer module.
More information
Debugging and script documentation
Introduction to debugging
Debugging scripts is a critical step in ensuring that your scripts work correctly and perform as expected. The Script Workspace in FileMaker provides powerful tools for debugging, such as the Script Debugger and the Data Viewer function.
Use script debugger
- Activate script debuggerOpen the script workspace and start the script debugger via Tools > Script debugger. The debugger allows you to go through your script step by step and check the current status of each variable and each script step.
- Step-by-step executionUse the buttons in the script debugger to execute your script step by step. This allows you to see exactly where an error occurs or unexpected results are generated.
- Set breakpointsYou can set breakpoints in your script to stop execution at a certain point and check the current status. This is particularly useful for analyzing complex areas of your script.
Use Data Viewer
The Data Viewer shows you the value of fields, variables and calculations in real time while your script is running. This is particularly useful for observing the effects of individual script steps on your data.
- Open Data Viewer: The Data Viewer can be accessed via Tools > Data Viewer be opened.
- Monitor expressionsAdd the fields or variables that you want to monitor in order to check their values during the execution of the script.
Script documentation
Clear and well-structured documentation of your scripts is important to ensure the maintainability and comprehensibility of your FileMaker solution. Good documentation not only contains comments in the script itself, but also a description of the script, its purpose and the parameters used.
- Comment on your script: Use the
Comment-function in the script workspace to document each section of your script. Explain what each section does and why it is necessary.example:# This script checks whether the file can be opened and issues an error message if not. - Add script description: Enter a general description of the script in the script workspace. This helps you or other developers to quickly understand what the script does.
- Managing version controlRecord different versions of your scripts, especially when you make changes. Make a note of when and why changes were made.
gFM-Business Open Source FileMaker Basis-ERP
The software for the crash course
Download for free
Create complex workflows with multiple scripts
Introduction to complex workflows
In larger FileMaker applications, several scripts often have to work together to implement a complete workflow. These scripts can be executed one after the other or linked together via conditions and triggers to map complex processes.
Scripts for module creation
- Division into modulesBreak down complex tasks into smaller, reusable modules. Each of these modules should fulfill a specific task and can be called by other scripts.example: A script module that validates data, another that generates a report, and another that exports the report.
- Use script parametersPass parameters between the scripts to make them flexible and reusable. This allows you to use a script in different contexts with different input data, for example:
# Main scriptExecute script
If [Get(ScriptParameter) = "Report"]
Execute script ["Generate report"]
Otherwise If [Get(ScriptParameter) = "Validation"]
End If - Call scripts: Use
Perform Script-steps to call other scripts within a main script. This makes it possible to create complex workflows consisting of several steps and modules.
Error handling and workflow management
- Error monitoring in complex workflowsIn workflows with multiple scripts, it is important to coordinate error monitoring between the scripts. Make sure that error codes are passed between the scripts and that the main script reacts accordingly.
- Rollback strategiesIn critical workflows, it may be necessary to implement a rollback strategy in which all changes are undone if an error occurs. This can be achieved by resetting fields or deleting newly created records.
Optimization and performance
- Optimize scriptsEnsure that your scripts are executed as efficiently as possible by avoiding unnecessary steps and minimizing the number of database queries.
- Minimize loading timesWhen working with large amounts of data or complex calculations, you should ensure that loading times are minimized. Use indexes, optimized queries and lean layouts.
Use of partial scripts and script parameters in FileMaker
In FileMaker you can Partial scripts and Script parameters can be used to maximize the efficiency and reusability of scripts. Partial scripts are smaller, specialized scripts that are called by other scripts to perform recurring tasks. This enables better structuring of the code and facilitates maintenance, as changes to a sub-script automatically take effect in all calling main scripts. Script parameters make it possible to pass information between different scripts, which increases the flexibility and dynamics of the script calls.
Example: Use of partial scripts and script parameters
Suppose we want to use a frequently recurring element such as the Creating a new data record and reuse the transfer of information to this data set across multiple scripts.
1. main script: Calling up a partial script
A subscript is called in the main script that creates the new data set. A Script parameters is used to send the data to be transferred to the partial script:
# Main script: "Create new customer"Set variable [1TP4Customer name; value: "Max Mustermann"]
Set variable [$customer number; value: "12345"]
# Calling the partial script with transferred parameters
Perform Script ["Customer system"; Parameter: JSONSetElement ( "{}" ; ["name"; 1TP4CustomerName; JSONString]; ["number"; 1TP4CustomerNumber; JSONString] )]In this example, the "Customer creation" subscript is called. The information on the customer name and customer number is transferred in JSON format as script parameters. This offers the advantage that several data points can be transferred in a single parameter.
2. subscript: Processing script parameters
The "Customer creation" subscript takes the script parameter, extracts the information and creates a new data record based on the transferred data:
# Partial script: "Customer system"
Set variable [$param; Value: Get ( ScriptParameter )]
# Extracting the data from the JSON parameter
Set variable [$name; Value: JSONGetElement ($param; "name")]
Set variable [$number; Value: JSONGetElement ($param; "nummer")]
# New data record based on the transferred values
New data record/query
Set field value [Customer::Name; $name]
Set field value [Customer::Number; $number]In this sub-script, the passed JSON parameter is processed using JSONGetElement processed. The variables $name and $number are extracted and entered in the corresponding fields of the new data record.
Pass multiple script parameters
Several script parameters can be passed in FileMaker in different ways. A common method is to pass the parameters as List and to pass it later with the function GetValue() individually. Alternatively, a method can be used in gFM-Business in which the parameters are separated by superscripts (|) are separated. These values are then calculated using the "StringColumns" split up again and processed individually. These approaches enable the flexible and structured transfer of several parameters, which can then be further processed in the corresponding scripts.
Advantages of partial scripts and script parameters
- ReusabilitySubscripts can be called by several scripts, which makes the code modular and easy to maintain. Changes to a partial script only have to be made once and affect all calling scripts.
- FlexibilityScript parameters can be used to pass dynamic information to sub-scripts, which makes the scripts more flexible. The use of JSON parameters makes it possible to pass complex data structures in a single parameter.
- Clarity and structureBy splitting scripts into smaller sub-scripts, the code becomes clearer and better structured. This not only facilitates development, but also debugging.
Tips, instructions and interfaces for FileMaker and gFM-Business ERP
Tips & instructions
Frequently asked questions about advanced script functions
- How can I check scripts for errors in FileMaker and correct them?
- In FileMaker, you can control error handling using the Set Error Logging command, which prevents standard error messages from being displayed. You can then use the Get(LastErrorNo) command to check the error status and define appropriate actions based on the error code. This allows alternative script paths to be executed or custom error messages to be displayed to inform users.
- How does the debugging of scripts in FileMaker work?
- The Script Debugger tool in FileMaker Pro allows you to go through scripts step-by-step. You can monitor the progress of a script in real time, set breakpoints and check the current status of variables and fields. This allows you to quickly localize and correct errors. You can start the debugger in the "Tools" > "Script Debugger" menu.
- I don't see a tool menu, how can I activate it?
- If you cannot find a tool menu in the menu bar, go to the FileMaker preferences (Mac under "FileMaker Pro", Windows under "Edit") and select the option "Use advanced tools" in the first tab "General". Confirm the dialog and restart FileMaker Pro once.
- How can I link multiple scripts in FileMaker?
- In FileMaker, you can link multiple scripts together by using the "Run Script" command to call another script. This allows you to divide complex processes into smaller, more manageable scripts that work together. By splitting scripts into sub-scripts, you can reuse specific tasks and improve the maintainability of your scripting solutions.
- What are partial scripts and when should I use them?
- Subscripts are smaller, specialized scripts that are called as part of a larger script. They are useful for encapsulating recurring tasks such as creating new data sets or navigating between layouts. Partial scripts promote a clean and modular structure in your scripts, which makes maintenance easier. If a process is used frequently, you should outsource it to a sub-script and call it from other scripts.
- How do I use script parameters in FileMaker?
- Script parameters allow you to pass data from one script to another. When calling a script, you can pass one or more parameters to the script by using the "Execute script" command and setting parameters. Within the script, you can retrieve and use the parameters with the "Get(ScriptParameter)" command. This is useful for dynamically controlling which actions the called script executes.
- How do I pass multiple script parameters?
- To pass several script parameters, you can combine them in a list or a JSON format. You can use the "List()" function to transfer parameters in a list separated by line breaks and retrieve them in the script using the GetValue("Get (ScriptParameter)"; number) function. The number identifies the xth script parameter. Alternatively, you can use JSON to pass parameters in a structured format and then use functions such as "JSONGetElement" to access the specific parameters.
- gFM-Business uses the "StringColumns" function for several script parameters, which separates the parameters with an apostrophe "|". This procedure has the advantage that passed parameters can also contain line separations.
- How do I manage global variables in scripts?
- Global variables (starting with $$) store data that is accessible during the entire session, even after the script has been completed. You can define global variables with the "Set Variable" command in a script and retrieve or change their value at any time. They are particularly useful for sharing data between different scripts or layouts. However, as they persist until the file is closed, global variables should be used with caution to avoid unwanted interference.
- How do local variables work in FileMaker scripts?
- Local variables (starting with $) are only valid during the runtime of a script. They are often used to temporarily store data that is only required in a specific script or sub-step. As soon as the script ends, the local variable is deleted. Local variables are well suited to saving calculation results or intermediate values without them remaining in the entire session.
- How can I use conditions effectively in a script?
- You can use "If" conditions to control the flow of a script based on certain criteria. You can check whether a condition is fulfilled and execute different actions depending on the result. A typical example would be a condition that checks whether a field is empty before a new record is created. For more complex cases, you can also use nested if-conditions or if/else scenarios to check multiple conditions, for which the "Else if" command can be used.
- How can I use repetitions and loops in scripts to process data?
- Loops allow you to repeat a series of actions until a certain condition is met. A typical example would be running through all data records in a search result and updating a specific field. You start the loop with the "Loop" command and end it with "Exit loop when" when the desired condition is met, e.g. when the last data record has been reached.
- How can I make scripts and partial scripts modular and reusable?
- To make scripts modular and reusable, you should outsource general processes to sub-scripts and use script parameters to control the process dynamically. This reduces redundancies and allows you to reuse scripts in different contexts. You can also use global and local variables efficiently to transfer data within a script or between scripts without having to create new scripts each time. If required, you can also transfer several script parameters from one script to another script, which you can read using the "Get(ScriptResult)" function.
