Order of Workflow Events

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Knowing when events occur and in what order they occur is important, because it affects how and when your script runs. If you have two script procedures that are meant to run in a certain order, you must make sure that the events the procedures are associated with occur in that order.

There are six events: OnCreate, OnDelete, OnEnter, OnExit, OnChange, and OnExpire. Each of these has two associated script procedures — validation and event. The validation script procedure must return True for the associated event script procedure to be executed.

If you have a series of events — such as for a transition where you exit state A, transition from state A to state B, and then enter state B — all validation procedures that are defined must return True before any**event script is executed.

Note   If you do not have OnExit and OnEnter events defined for the states involved in a transition, only the transition validation is executed.

The following sections explain the order that events are executed for the different types of workflow events:

  • Creating a New Record
  • Deleting a Record
  • Entering and Exiting a State
  • Transition from One State to Another
  • Editing a Record
  • Remaining in a State for a Certain Time

Creating a New Record

You add the OnCreate event by dragging a transition from the Item Created shape to any valid initial state.

Order of events

  • OnCreate Þ OnEnter

Deleting a Record

You add the OnDelete event by dragging a transition from any valid state to the Item Deleted shape.

Order of events

  • OnExit Þ OnDelete

Entering and Exiting a State

The OnEnter and OnExit events are executed for each state transition. Therefore, an OnEnter event is fired for inserts, and an OnExit event is fired for deletes. Both events are fired for each state transition.

Order of events

  • OnExit (current state) Þ OnChange (to a new state) Þ OnEnter (next state)

Transition from One State to Another

The OnChange event moves the item from its current state to the next state defined in the workflow process.

Order of events

  • OnExit (current state) Þ OnChange Þ OnEnter (next state)

Editing a Record

The OnChange event updates the database with any changes made to the data. You must add a Transition Within to a workflow state to be able to edit a record in that state.

Order of events

  • OnExit (current state) Þ OnChange Þ OnEnter (same state)

Remaining in a State for a Certain Time

The OnExpire event executes code after a designated amount of time has passed. The Microsoft® SQL Server™ Agent manages the time and triggers the event by calling stored procedures on the server.

Each state can have multiple OnExpire events. For example, you can define events that should occur at 30, 60, and 90 days.

Order of events

  • OnExpire

    Note   If the OnExpire event does not cause a state change, then OnExit and OnEnter events are not triggered.

Order of events if the Expiry script causes a state change

  • OnExpire Þ OnEnter (next state)

For example, if you want to change the status description of a record to Overdue when the record has not been modified in 30 days, you can use script similar to this example:

Function Expiry1_OnExpireValidate()
    If now - session.item("DateModified") >= 30 Then
        Expiry1_OnExpireValidate = True
    End If
End Function

Sub Expiry1_OnExpire()
    session.item("statusDescription") = "Overdue" 
    session.item.updatebatch
End Sub

See Also

Workflow Scripting Reference | Workflow Events | Session Object Model Reference | Logger Object Model Reference | Scripting Workflow Events