找回密码
 注册

QQ登录

只需一步,快速开始

查看: 304|回复: 0

Error handle for Db commit in VBS or Perl

[复制链接]
发表于 2013-5-24 08:34:51 | 显示全部楼层 |阅读模式
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
  1. Sub swbug_Commit(actionname, actiontype)

  2.     ' actionname As String

  3.     ' actiontype As Long

  4.     ' action = tested

  5.     Dim dups  ' Array of all direct duplicates of this defect

  6.     Dim dupsvar ' Variant containing link to a duplicate

  7.     Dim dupsobj ' The same link, but as an object rather than a variant

  8.     Dim record  ' The record extracted from the link

  9.     Dim session

  10.     Dim parent_id ' The display name of this defect

  11. ... Dim RetVal

  12.     ' Make an API to call to see if this record has duplicates

  13.     If HasDuplicates() Then

  14.       Set session = GetSession

  15.       dups = GetDuplicates

  16.       parent_id = GetDisplayName

  17.       For Each dupvar In dups

  18.         Set dupobj = dupvar

  19.         Set entity = dupobj.GetChildEntity

  20.         session.EditEntity entity, "dupdone"

  21.       entity.SetFieldValue "action_reason", "Original " & parent_id & " is tested"

  22. ' validate and commit, with exception and error handling
  23. On Error Resume Next
  24. Err.Clear
  25. 'RetVal is empty on success else it holds an error message string on failure
  26. RetVal = entity.Validate  
  27. if Err.Number <> 0  then      
  28.     ' An exception occurred
  29.     ' Err.description holds the error message
  30.     ' This example prints the error details and reverts the record to
  31.     ' its previous state.
  32.      StdOut "Validation exception:" & vbCrLf &_
  33.         "    Error number: " & Err.Number & vbCrLf &_
  34.         "    Error description: '" & Err.Description & vbCrLf
  35.      entity.Revert  

  36. elseif RetVal <> "" then  
  37.     ' An error message string was returned.indicating that validation failed,
  38.     ' possibly due to one or more fields having unacceptable values. You can
  39.     ' attempt to resolve this problem by determining which fields
  40.     ' have unacceptable values, give them acceptable values, and retry the
  41.     ' call to entity.Validate. This code example prints the error
  42.     ' details and then reverts the record to its original state.
  43.     StdOut "Validation error: " & RetVal & vbCrLf  
  44.     entity.Revert

  45. else
  46.     ' Validate was successful. You can proceed and Commit the change.
  47.     StdOut "Validation was successful." & vbCrLf                     
  48.     Err.Clear
  49.     RetVal = entity.Commit  
  50.     if  Err.Number <> 0  then      
  51.         ' An exception occurred (this indicates that an error occurred before
  52.         ' the values were written to the database). This example code prints the
  53.         ' error details and reverts the record to its previous state.
  54.         StdOut "Commit exception:" & vbCrLf &_
  55.             "    Error number: " & Err.Number & vbCrLf &_
  56.             "    Error description: '" & Err.Description & vbCrLf
  57.         entity.Revert

  58.     elseif RetVal <> "" then
  59.         ' An error message string value was returned. This indicates that an
  60.         ' error occurred after the values were written to the database (for
  61.         ' example, a failure in an action notification hook). You can handle
  62.         ' the error by correcting the failure and trying  to commit again or
  63.         ' revert. This example code prints the error message details.
  64.         StdOut "Commit error (after committing changes): " & RetVal & vbCrLf

  65.     else
  66.         ' No exception or returned error message value
  67.         StdOut "Commit was successful." & vbCrLf
  68.     end if   
  69. end if

  70. ' Clear the error handler
  71. Err.Clear

  72.       Next
  73.     end if
  74. End Sub
复制代码
Perl
  1. sub swbug_Commit {

  2.     my($actionname, $actiontype) = @_;

  3.     # $actionname As string scalar

  4.     # $actiontype as long scalar

  5.     # action is Submit

  6.     # This hook is fired during the "commit" step of an

  7.     # entity update. It is the appropriate place to put an

  8.     # activity which should be bundled into the same

  9.     # transaction as the commit, such as subactions

  10.     # or updates of external data storage.

  11.     my ($RetVal);

  12.     my ($dups, # Array of all direct duplicates of this defect

  13.     $record,   # The record extracted from the link

  14.     $parent_id, # The display name of this defect

  15.     $session,

  16.     $locEntity,

  17.     $dupobj

  18.     );

  19.     # Make an API to call to see if this record has duplicates

  20.     if ($entity->HasDuplicates()) {

  21.       $session = $entity->GetSession();

  22.       $dups = $entity->GetDuplicates();

  23.       $parent_id = $entity->GetDisplayName();

  24.    my $count = $dups->Count();

  25.    my $i = 0;

  26.    for (i=0;$i<$count;$i++){

  27.       $dupobj = $dups->Item($i);

  28.       $locEntity = $dupobj->GetChildEntity();

  29.       $session->EditEntity($locEntity, "dupdone");

  30.       $locEntity->SetFieldValue("action_reason", "Original "
  31.             . $parent_id . " is tested");

  32. ' validate and commit, with exception and error handling

  33.       eval {$RetVal = $locEntity->Validate(); };
  34.       if ($@){
  35.         print "Exception: '$@'\n";
  36.         $locEntity->Revert();
  37.             }
  38.       elsif ($RetVal ne "") {
  39.                   $session->OutputDebugString ("validation error: $RetVal\n");
  40.                   # correct whatever failed validation and then try validate again or revert
  41.                      }   
  42.       else {
  43.             eval {$RetVal = $locEntity->Commit(); };         
  44.             if ($@){
  45.                   print "Exception: '$@'\n";
  46.                   $locEntity->Revert();
  47.                           }
  48.             elsif ($RetVal ne "") {
  49.                                   $session->OutputDebugString ("commit error: $RetVal\n");
  50.                                  # handle error - correct whatever failed and try again or revert  
  51.                 }
  52.                         # commit successful                       
  53.             }

  54.     }
  55.    }
  56. }
复制代码
Related Parent topic: Action hook examples





您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-11 01:56 , Processed in 0.014663 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表