Showing changes to a FieldInfo (field) object
The following example illustrates how to use the Rational® ClearQuest® API to get information about field values that have changed for a given record.
Perl
# entity->GetFieldsUpdatedThisAction returns all fields that have changed...
$oSess = $entity->GetSession();
$username = $oSess->GetUserLoginName();
$action = $entity->GetActionName();
$state = $entity->GetFieldValue("State")->GetValue();
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $time) =
localtime();
$now = sprintf("%2.2d/%2.2d/%4d %2.2d:%2.2d:%2.2d", $mon + 1,$mday, $year +
1900, $hour, $min, $sec);
$body = "Fields Changed by $username, $action->$state, $now\n\n";
$fields = $entity->GetFieldsUpdatedThisAction();
for ($i = 0; $i < $fields->Count(); $i++) {
$field = $fields->Item($i);
$fname = $field->GetName();
$newVal = &GetStrValue($oSess, $field);
$oldVal = &GetStrValue($oSess, $entity->GetFieldOriginalValue($fname));
unless($fname eq "Notes_Log"){
unless($newVal eq $oldVal){
$body .= "* $fname: $val\n";
}
}
}
$body .= "\n";
$fieldlog = $entity->GetFieldValue("FieldLog")->GetValue();
$newLog = $body . $fieldlog;
$entity->SetFieldValue("FieldLog", $newLog);
# Start of Global Script GetStrValue
# This is used to handle different types of fields...
sub GetStrValue {
my ($session, $field) = @_;
$val = "";
$type = $field->GetType();
if ($type lt 6) { #string, int, date, reference
$val = $field->GetValue();
} elsif ($type eq 6) { # Reference_list
$list = $field->GetValueAsList();
foreach $item (@$list) {
$val .= "\n\t$item";
}
} elsif ($type eq 9) { #State
$val = $field->GetValue();
}
return $val;
}
# End of Global Script GetStrValue