|
|
|
Action commit hook example Commithooks allow you to perform additional actions before a record is committedto the database.
The following example checks whethera defect has duplicates (dups). If the original defect was marked as tested,the hook marks the duplicates as dupdone, indicating that they shouldbe evaluated again to verify that they are fixed. If there is a failure tocommit one of these updates, all of the database transactions will be rolledback, including the one to which this hook belongs.
Note: You can also performthe additional actions in a Validation or Notification hook. When callingthe Validate and Commit methods, makesure your code checks for exceptions and return message strings. The examplesin this section provide examples of error and exception handling. See Error checking and validation for more information. Also note thatyou can use the IsEditable method of the Entity objectto determine if you need to revert the commit operation as part of the exceptionhandling. You may not want to revert for all validation failures. callingthe Revert method does not work after a successful commit(even if it returns a post-notification warning because the Entity is alreadycommitted to the database).
VBScript
Perl
- sub swbug_Commit {
- my($actionname, $actiontype) = @_;
- # $actionname As string scalar
- # $actiontype as long scalar
- # action is Submit
- # This hook is fired during the "commit" step of an
- # entity update. It is the appropriate place to put an
- # activity which should be bundled into the same
- # transaction as the commit, such as subactions
- # or updates of external data storage.
- my ($RetVal);
- my ($dups, # Array of all direct duplicates of this defect
- $record, # The record extracted from the link
- $parent_id, # The display name of this defect
- $session,
- $locEntity,
- $dupobj
- );
- # Make an API to call to see if this record has duplicates
- if ($entity->HasDuplicates()) {
- $session = $entity->GetSession();
- $dups = $entity->GetDuplicates();
- $parent_id = $entity->GetDisplayName();
- my $count = $dups->Count();
- my $i = 0;
- for (i=0;$i<$count;$i++){
- $dupobj = $dups->Item($i);
- $locEntity = $dupobj->GetChildEntity();
- $session->EditEntity($locEntity, "dupdone");
- $locEntity->SetFieldValue("action_reason", "Original "
- . $parent_id . " is tested");
- ' validate and commit, with exception and error handling
- eval {$RetVal = $locEntity->Validate(); };
- if ($@){
- print "Exception: '$@'\n";
- $locEntity->Revert();
- }
- elsif ($RetVal ne "") {
- $session->OutputDebugString ("validation error: $RetVal\n");
- # correct whatever failed validation and then try validate again or revert
- }
- else {
- eval {$RetVal = $locEntity->Commit(); };
- if ($@){
- print "Exception: '$@'\n";
- $locEntity->Revert();
- }
- elsif ($RetVal ne "") {
- $session->OutputDebugString ("commit error: $RetVal\n");
- # handle error - correct whatever failed and try again or revert
- }
- # commit successful
- }
- }
- }
- }
复制代码 Related Parent topic: Action hook examples
|
|