Example: Field Validation

This example demonstrates how to validate a single field value. For example, the following code segment shows how you can ensure that a user in a specific group cannot lower the priority of a defect.

In this example, if the user is in the QATester group and the BG_PRIORITY field is being modified, the new value of the BG_PRIORITY field cannot be lower than the current value.

This example assumes that in the Priority field list for the project, lower priorities come first when the values are sorted in ascending order. For example, the list meets this requirement if the elements are as follows: 1-Low, 2-Medium, 3-High.

Add the code to the Bug_FieldCanChange event procedure so that it is triggered when the user attempts to change a defect field value.

Function Bug_FieldCanChange(FieldName, NewValue)
        ' Initialize the function's return value 
	' to avoid unpredictable behavior.
        Bug_FieldCanChange = True 
        On Error Resume Next
        If User.IsInGroup("QATester") and FieldName ="BG_PRIORITY" _
            If NewValue < Bug_Fields("BG_PRIORITY").Value then
                Bug_FieldCanChange = False
                msgbox "You do not have permission to lower " _
		 & "defect priority."
                Bug_FieldCanChange = True
            End If
            ' Enter your code here.
        End If
        PrintError "Bug_FieldCanChange"
        On Error GoTo 0
End Function