Record script example

When you use VBScript, record scripts, field hook, and action hooks are implicitly associated with an Entity object; unless you specifically name another Entity object, all calls to the methods of the Entity class refer to this implicit object. When you use Perl, reference this association with the predefined variable, $entity.

The following example shows a Record script capable of responding to both button clicks and context-menu item selections. When the button is clicked, this hook puts the name of the component lead engineer in the component_ref field, which displays the person assigned to work on the defect.

This example provides a general idea of how you might add a record script to your schema. The example does not include error checking. Check the return value of the Validate API to verify that it includes no errors before you commit the record to the database.

VBScript


Function Request_AssignEngineer(param)

    ' param As Variant

    ' This hook responds to changes in the current component and

    ' assigns the request to the lead engineer for that component.

    Dim eventType, componentObj, leadname

    eventType = param.EventType

    If eventType = AD_BUTTON_CLICK Then

      ' Get the lead person for the given component

      leadName = GetFieldValue("component_lead").GetValue

      If leadName = "" Then

        Request_AssignEngineer = "Couldn't get Component Lead value"

        Exit function

      End if

      ' Put that person's name in the Assigned To: field

      SetFieldValue "component_ref", leadName

      Request_AssignEngineer = SetFieldValue "component_ref", leadName

    Elseif eventType = AD_CONTEXMENU_ITEM_SELECTION Then

   SetFieldValue "component_ref", GetSession.GetUserFullname

   Request_AssignEngineer = SetFieldValue "component_ref", GetSession.GetUserFullname

    End if

End Function