Ever get tired of manually setting variables for every field when moving data between layouts? The LayoutVariables() custom function automatically creates local variables for every field on your current layout with just one line of code.
What LayoutVariables() Does
This function scans your current layout and creates local variables for every field:
- Field Name: FirstName → Variable: $FirstName
- Field Name: LastName → Variable: $LastName
- Related Field: Cars::Make → Variable: $__Cars_Make
Usage is simple:
Set Variable [ $vars ; LayoutVariables() ]
That’s it! All your layout fields are now available as variables.
Example Use Cases
1. Data Transfer Between Layouts
Instead of manually setting 15+ variables when creating related records:
# Old way (tedious):
Set Variable [ $firstName ; Customers::FirstName ]
Set Variable [ $lastName ; Customers::LastName ]
Set Variable [ $email ; Customers::Email ]
# … 10 more lines
# With LayoutVariables():
Set Variable [ $vars ; LayoutVariables() ]
2. Form Backup Before Navigation
Capture all field values before leaving a layout so you can restore user input if needed.
3. Audit Trails
Quickly snapshot all visible fields for “before” states in audit logs.
4. Multi-Step Workflows
Collect data from each step of a wizard-style process to combine at the end.
How It Works
The function uses FieldNames() to get all layout fields, loops through them with While(), extracts values using GetField(), builds a JSON object, then uses Evaluate() to create all variables dynamically.
Key Points
Features:
- Works on any layout automatically
- Handles local and related table fields
- Uses special naming for related fields: $__tablename_fieldname
Limitations:
- Portal fields only return first related record
- Requires script context
- Creates local scope variables only
- Makes scripts less readable – Variables are set silently, so developers need to know the layout context to understand which variables are available
When to Use: Best used when you know the layout context well. Since variables are created silently behind the scenes, it trades script readability for brevity. Great for experienced developers working on familiar layouts, but may confuse others who need to maintain the code later.
Sample Implementation
# Capture current form state
Set Variable [ $vars ; LayoutVariables() ]
# Navigate and use captured data
Go to Layout [ “Invoice Entry” ]
New Record/Request
Set Field [ Invoices::CustomerName ; $firstName & ” ” & $lastName ]
Set Field [ Invoices::CustomerEmail ; $email ]
Try It Out
The included demo file (layoutvariables.fmp12) shows the function in action across different layouts and field configurations.
Conclusion
LayoutVariables() eliminates repetitive variable setting and makes scripts cleaner and more maintainable. Whether you’re building workflows, audit systems, or just moving data around, this function will save you significant development time.