Action initialization hook example

Initialization hooks perform complex initialization at the beginning of an action. For example, you can use this hook to reset fields or to assign different values to fields based on the type of action.

The following code is a hook that runs when a user attempts to reassign a defect to another user. The hook clears the contents of the action_reason field at the beginning of a reassign action. If the behavior of this field is set to Mandatory, the user must provide a reason for reassigning the defect.

VBScript


Sub swbug_Initialization(actionname, actiontype)

    ' actionname As String

    ' actiontype As Long

    ' action = reassign

    ' Empty the string at the beginning of the action

    SetFieldValue "action_reason", ""

End Sub 

Perl


sub swsub_Initialization {

    my($actionname, $actiontype) = @_;

    # $actionname as string scalar

    # $actiontype as long scalar

    # action is reassign

    # do any setup for the action here

    # Empty the string at the beginning of the action

    $entity->SetFieldValue("action_reason", "");

} 
Note: If your schema requires users to provide a reason for performing each action, you can use a DEFAULT_VALUE hook to clear the action_reason field at the beginning of each action. In the preceding example, clearing the field is required only for a reassign action, which makes the action initialization hook the more appropriate hook to use.