Action notification hook example
Notification hooks trigger additional actions after a set of changes are committed to the database. For example, you can send an e-mail notification to one or more users or modify related records.
You must stop and restart the DevOps Plan Mail Service every time you make changes to an e-mail notification hook.
The following example opens a window for each field in the defect that was modified. You might also use a notification hook to generate an e-mail message and send it to an appropriate distribution list.
An action that is initiated from a hook does not trigger a notification unless you set the session variable CQHookExecute to a value of 1 in the hook script. This variable is a Long scalar in Perl.
Perl
sub swsub_Notification {
my($actionname, $actiontype) = @_;
# $actionname as string scalar
# $actiontype as long scalar
# action is Submit
# Post-commit notifications about actions may be handled here
my ($fieldnames,
$session,
$fieldname,
$oldinfo,
$newinfo,
$newstat,
$oldstat,
$oldval,
$oldempty,
);
$session = $entity->GetSession();
$fieldnames = $entity->GetFieldNames();
# Get three kinds of values
foreach $fieldname (@$fieldnames) {
$oldinfo = $entity->GetFieldOriginalValue($fieldname);
$newinfo = $entity->GetFieldValue($fieldname);
$oldstat = $oldinfo->GetValueStatus();
if ($oldstat == $CQPerlExt::CQ_HAS_NO_VALUE) {
$oldempty = 1;
} else {
$oldempty = 0;
$oldval = $oldinfo->GetValue();
}
$newstat = $newinfo->GetValueStatus();
if ($newstat == $CQPerlExt::CQ_HAS_NO_VALUE) {
$newempty = 1;
} else {
$newempty = 0;
$newval = $oldinfo->GetValue();
}
# Compare the values
if ($oldstat == $CQPerlExt::CQ_VALUE_UNAVAILABLE) {
$session->OutputDebugString("Field " . $fieldname . ":
original value unknown\n");
} else {
if ($newempty && !$oldempty) {
$session->OutputDebugString ("Field " & $fieldname .
" had its value deleted\n");
} elsif ($oldempty && !$newempty) {
$session->OutputDebugString ("Field " . $fieldname . " now = " .
$newval. "\n");
} elsif ($oldval != $newval) {
$session->OutputDebugString ("Field " . $fieldname . " was = " .
$oldval. "\n");
$session->OutputDebugString ("Field " . $fieldname . " now = " .
$newval. "\n");
} else {
$session->OutputDebugString ("Field " . $fieldname . " is
unchanged\n");
}
}
}
$session->OutputDebugString ("Modify action & notification hook completed\n");
}