Executing a simple Lua script in a trigger action

Overview

To execute a Lua script directly in a trigger action, you use the Execute Lua Script action and embed the Lua script directly in the action.
Since the Lua script is defined directly in the action it can be changed by editing the trigger.

The advantage of using this action instead of the Execute Lua Function From File action is that you don't have to upload a separate Lua script file to the Staging Browser.

This section describes how to define a very simple Lua script, a 1-line script to log an entry into the Exceptions Log.

The export file for this trigger can be downloaded to your computer and then imported into your node using the Workbench:

Trigger_LUA_SCRIPTS_MyNewTrigger.dwx

The steps described in this example are the same steps that would be used for more complex Lua scripts.

Assumptions

  • The Lua extension is installed.
  • You know how to define and edit triggers. The trigger List Editor is shown, the Canvas Editor can be used as well. The flow is similar.

On this page

The following will walk you through the steps to define a trigger that will execute a Lua script:

Step 1 - Defining a new trigger

The following describes how to create an On-Demand trigger event type trigger that uses the Execute Lua Script action.

  1. From Workbench left pane, select the Projects icon.
    The Projects tab appears.
  2. Select the project you want to add a trigger to.
    The project tab appears.
  3. Select the New button from the bottom of the Projects tab.
    The New Trigger window appears.
  4. In the Name box, type a name for the trigger.

  5. From the Event tab, click the Trigger Event Type down-arrow, and then select On-Demand.
    You now have a basic On-Demand trigger that can be fired (executed) using the Workbench Fire Trigger option.
    You will complete the trigger by adding an action for the trigger to perform.

Step 2 - Adding the Execute Lua Script action

You will add the Execute Lua Script action.
The example shows the use of the Trigger List Editor, the Trigger Canvas Editor could be used as well.

  1. From the Actions section at the bottom of the New Trigger window, select Add.
    The New Action window appears.

  2. Expand the Lua Scripting category, select Execute Lua Script, and then select Add.
    The right pane is where the specifics of the Execute Lua Script action are defined.

  3. Fill in the Lua parameter with: dw.log.exception("INFO","Hello Lua.")

  4. Select Validate. If no errors are reported, select Save.
    If there is an error reported, review the information and correct the parameter that has a problem.

Step 3 - Executing the trigger

You will now execute the trigger to cause the Lua script to execute.

  1. Find the trigger displayed in its project.

  2. Select the trigger, then right click the trigger to display its pop-up menu, and then select Start.
    Or select the trigger and then select the Start button at the bottom of the panel.
    The State column for the trigger changes to Started.

  3. Right click the trigger to display its pop-up menu, and then select Fire Trigger.
    This will cause the trigger to execute, which in turn will execute the actions defined in it. In this example, there is just the one Execute Lua Script action.

Step 4 - Checking the result of the trigger

You will check to see that the Lua script actually logged a message to the Exceptions Log.

  1. From Workbench left pane, select the Logs & Reports icon.
    The Logs & Reports tab appears.
  2. Select Exceptions Log tab.

  3. Check that a new entry has been logged in the Exceptions Log.
    This shows that the simple Lua script executed successfully within the Execute Lua Script action.