Action validation hook example
Action validation hooks check conditions that are difficult to verify at the field level, such as whether a set of related field values is valid. Field-level validation hooks run immediately after the field is modified; action validation hooks do not run until the user is finished modifying the record and is ready to commit it to the database.
The following example verifies that the user enters a correct client operating system (OS) value for the given project. If the operating system specified for the project is not supported, this hook generates and returns a validation error message.
VBScript
Function defect_Validation(actionname, actiontype)
' actionname As String
' actiontype As Long
' defect_Validation As String
' action = teststate
set sessionObj = GetSession
' Get the client OS platform the user indicated.
platform = GetFieldValue("client_os").GetValue()
' Get the project name the user indicated. This information
' is stored on a referenced, stateless record.
projectName = GetFieldValue("project.name").GetValue()
' Check the project name against the OS type. If the given project
' is not targeted for that platform, return a validation error.
If projectName = "Gemini" Then
If platform <> "NT" Then
defect_Validation = "That project only supports NT."
End If
ElseIf projectName = "Aquarius" Then
If platform <> "Unix" Then
defect_Validation = "That project only supports Unix."
End If
End If
End Function
Perl
sub defect_Validation {
my($actionname, $actiontype) = @_;
my $result;
# $actionname as string scalar
# $actiontype as long scalar
# $result as string scalar
# action = teststate
# Returns a non-empty string explaining why the action
# can not commit with the current values.
# Or, if it is valid, returns an empty string value.
my ($session,
$platform,
$projectRecordID,
$projectRecord,
$projectName,
);
$session = $entity->GetSession();
# Get the client OS indicated by the user.
$platform = $entity->GetFieldValue("client_os")->GetValue();
# Get the project name the user indicated. This information
# is stored on a referenced, stateless record.
$projectName = $entity->GetFieldValue("project.name")->GetValue();
# Check the project name against the OS type. If the
# given project is not targeted for that platform,
# return a validation error.
if ($projectName eq "Gemini") {
if ($platform != "NT") {
$result = "That project only supports NT.";
}
} elsif ($projectName eq "Aquarius") {
if ($platform != "Unix"){
$result = "That project only supports Unix.";
}
}
return $result;
}