24
Script Triggers FMUG February 5, 2010

Script Triggers - Information Systems & Technology Triggers.pdf · the "When closing this file“ section. ... – If a pre-event trigger fires a script that causes that same pre-event

Embed Size (px)

Citation preview

Script Triggers

FMUGFebruary 5, 2010

What We Will Cover

• What is a script trigger?• Key components of triggers• Examples• Gotchas• Best Practices• Resources

What is a Script Trigger?• A database trigger is procedural code that is

automatically executed in response to certain events on a particular table or view in a database. (Wikipedia)– Triggers are a common database application

programming feature– Just about every database technology has some

version of this feature

• Script Triggers in FileMaker Pro 10 allow you to automatically run scripts based on user actions like entering a field or inputting data. (FMI web site)

Script Trigger Overview

• New feature introduced in FileMaker version 10

• One of the tools available to control the user’s environment

• Allows the developer to streamline the user experience – Less clicking required

The Components

• The Event, or “Trigger”• The Script• The Timing Around the Event• The Associated Layout and Object

The Trigger• The occurrence of an event triggers a script to run• Usually involves user interaction• Some FileMaker events that can trigger script execution:

– After a field is entered– When typing into a field– After a value is changed– Before data is saved– Before exiting a field– When a layout becomes active– When navigating from record to record– Before a record is committed– When an uncommitted record is changed back to its original value– When changing from Browse, Preview or Find mode

Trigger Categories

• Object – “Object” includes interactive elements like fields and tab controls;

not labels or lines– For example, a user may:

• enter or exit a field by clicking or tabbing• modify the value in a field

• Layout– For example, a user may:

• commit a record• switch layouts• change from Browse mode to Find mode

• File Open or Close

Trigger Timing

• Triggers are fired either before or after the event occurs– Pre-event triggers

• Examples: OnRecordCommit, OnLastWindowClose, OnModeExit

• Can cancel the event if the script returns a “false”

– Post-event triggers• Examples: OnLayoutLoad, OnObjectModify

Fire Order

• Order in which triggers fire:– 1.OnFirstWindowOpen– 2.OnLayoutLoad– 3.OnModeEnter– 4.OnRecordLoad– 5.OnObjectEnter– 6.OnObjectKeystroke– 7.OnLayoutKeystroke– 8.OnObjectModify– 9.OnObjectSave– 10.OnObjectExit– 11.OnRecordCommit or OnRecordRevert– 12.OnModeExit– 13.OnLastWindowClose

The Layout

• Most triggers are dependent on user interaction with a layout

• You may configure:– Many triggers per layout– Many triggers per action

• So note the fire order– But only one script per trigger

• How to implement a trigger• How it looks to the developer

Configuring Object Triggers

• Select an object in Layout mode, • Navigate to Format > "Set Script Triggers " • This will open the configuration window for

Object script triggers. OR• Access the configuration window by right-

clicking (control-clicking on Mac) an object, and selecting "Set Script Triggers "

Figure 1: Object Trigger

Configuring Layout Triggers

• Layout Triggers– In Layout mode,

navigate to Layouts > Layout Setup

– Click the "ScriptTriggers" tab.

Configuring File Triggers

• Navigate to File > File Options

• Enable the "Perform script:" option under the "When opening this file“ section and the "When closing this file“ section.

Trigger Visual Indicators

• Objects with triggers display a red “firecracker” in Layout mode

Layout Trigger Indicator

• There are no visual cues for layout triggers• You must run a Database Design Report

– You can see both in the DDR

Examples

• Sort on load• Find dropdown• Navigation• Show duplicates• Data Entry warning

– Cancels Event– Not for validation

Gotchas• Actions that are not triggers:

– Non-interface-based changes to data:• Importing data, script action on a field (setfield),

schema changes, Replace• Lesson: Don’t use triggers for data integrity

• Maintenance– Associated with layouts and objects

• Not centrally catalogued like scripts and layouts• DDR is the only way to know where they all are

– Lesson: Have a good reason for every trigger, not just “‘cuz it’s cool”

Another Gotcha

• Infinite loops– If a pre-event trigger fires a script that causes that same

pre-event to occur on the same object, the script will be executed endlessly

• Example: a script triggered OnObjectKeystrokecontains script steps that cause focus to leave and then return to the same trigger-associated object

– Lesson: Avoid infinite loops

One More Gotcha

• Backwards compatibility– Triggers will not function (except for File

triggers)• You’ve replaced all your navigation buttons with

dropdown lists! Now what?– Triggers will be preserved if objects with

triggers are duplicated in an earlier version, but not with copy/paste

– Lesson: Don’t mix versions if you’re using script triggers

Best Practices

• Test extensively• Never implement new triggers directly on a

production system without testing thoroughly first in a development/test environment

• Use FileMaker Pro Advanced– Script Debugger– Database Design Report

• Always have clean backups and clones

And If All Else FailsUse File Recovery advanced options to check off “Bypass startup script and layout”

Cecilia’s Opinion• Be careful with mixing triggered navigation

with other methods like buttons– Your users may experience inconsistent results– With mixed methods, maintenance could be a

nightmare --not to mention reverse engineering– Do your best to document your use of triggers

• Don’t replace perfectly functional existing feature implementations with triggers unless it really adds value --like automation!

Resources

• More about database triggers:– http://en.wikipedia.org/wiki/Database_trigger

• Script triggers in FileMaker:– http://www.filemaker.com/products/filemaker-

pro/script_triggers.html– FMI Knowledgebase

• http://filemaker.custhelp.com– White paper available to TechNet members