ProcessBatch

The ProcessBatch event triggers when the Runtime Server instance begins processing during the custom processing workflow step.

Syntax

ScriptModule_ProcessBatch(pBatch as SCBCdrPROJLib.ISCBCdrBatch, ByVal InputState as Long, DesiredOutputStateSucceeded as Long, DesiredOutputStateFailed as Long)
Parameter Description
pBatch The Batch Object that is processed.
InputState The input state of the batch when custom processing was activated on it.
DesiredOutputStateSucceeded The output state of the batch if the workflow step succeeds.
DesiredOutputStateFailed The output state of the batch if the workflow step failed.

Use the corresponding Terminate event script instead to delete these empty batches. Do not use both scripts within one project, because the Terminate event script makes it impossible to load the ProcessBatch script.

Sample Code

Add the following sample code to the very beginning of the ProcessBatch event to stop an indefinite looping process of state 0 batches.

This script does not set batches to special state 987. The script repairs a batch and stops looping of the custom processing step.

Note: It is not possible to set the batch state to something other than zero for a batch with no documents because batch state is by definition the lowest state of all enclosed documents. If the number of documents is zero, the program uses the default value, which is zero.
Private Sub ScriptModule_ProcessBatch(pBatch as SCBCdrPROJLib.ISCBCdrBatch, ByVal InputState as Long, DesiredOutputStateSucceeded as Long, DesiredOutputStateFailed as Long)
   Dim lFolderIndex as Long
   Dim lDocIndex as Long
   Dim theWorkdoc as SCBCdrWorkdoc
   Dim vLoadingCompletenessStatus as Variant
   Dim lStatus as Long
   Dim bNeedSafetyRestart as Boolean
   Dim strWorkdocName as String
   Dim theImage as SCBCroImage
   On Error GoTo LABEL_ERROR
   pBatch.BatchPriority = 3 '[AE] [2012-03-27] Boost priority for state zero documents
   Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "ScriptModule_ProcessBatch starting, batch <" & CStr(pBatch.BatchID) & ">, new state <" & CStr(DesiredOutputStateSucceeded) & ">"
   If ScriptModule.ModuleName <> "Server" Then Exit Sub
   For lFolderIndex = pBatch.FolderCount – 1 To 0 Step -1
      If pBatch.FolderDocCount (lFolderIndex) = 0 Then
         Project.LogScriptMessageEx CDRTypeWarning, CDRSeveritySystemMonitoring, "Removed folder with zero documents from batch [" & pBatch.BatchID & "]"
         pBatch.DeleteFolder(lFolderIndex, False)
      End If
   Next lFolderIndex
   If pBatch.FolderCount = 0 Then
      Project.LogScriptMessageEx CDRTypeWarning, CDRSeveritySystemMonitoring,
      "Detected batch with zero folders: [" & pBatch.BatchID & "]"
      pBatch.BatchState = 987
   End If
   On Error Resume Next
   For lFolderIndex = 0 To pBatch.FolderCount-1 Step 1
      For lDocIndex = pBatch.FolderDocCount(lFolderIndex) - 1 To 0 Step -1
         If pBatch.FolderDocState(lFolderIndex, lDocIndex) = InputState Then
            Err.Clear
            bNeedSafetyRestart = False
            strWorkdocName = pBatch.FolderWorkdocFileName (lFolderIndex, lDocIndex, False)
            Set theWorkdoc = pBatch.LoadWorkdoc(lFolderIndex, lDocIndex)
            Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "Loading of zero state Workdoc [" & strWorkdocName & "] proceeded with error number [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
            "Detected batch with zero foldersdirectories: [" & pBatch.BatchID & "]"
            pBatch.BatchState = 987
         End If
      On Error Resume Next
      For lDocIndex = pBatch.FolderDocCount(lFolderIndex) - 1 To 0 Step -1
         If pBatch.FolderDocState(lFolderIndex, lDocIndex) = InputState Then
            Err.Clear
            bNeedSafetyRestart = False
            strWorkdocName = pBatch.FolderWorkdocFileName (lFolderIndex, lDocIndex, False)
            Set theWorkdoc = pBatch.LoadWorkdoc(lFolderIndex, lDocIndex)
            Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "Loading of zero state Workdoc [" & strWorkdocName & "] proceeded with error number [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
            lStatus = 1001
            If Err.Number = 0 Then
               vLoadingCompletenessStatus = theWorkdoc.NamedProperty("LoadingCompletenessStatus")
               lStatus = vLoadingCompletenessStatus
            End If
            For lFolderIndex = 0 To pBatch.FolderCount-1 Step 1
               If Err.Number <> 0 Or lStatus > 0 Then
                  bNeedSafetyRestart = True
                  Project.LogScriptMessageEx CDRTypeWarning, CDRSeverityEmailNotification, "True corruption case detected for Workdoc [" & strWorkdocName & "] with stream exit code [" & CStr (lStatus) & "]"
               End If
               Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "PreErrorChecks: Loading return code is {" & CStr(Err.Number) & "} and loading status is {" & CStr(lStatus) & "}"
               If (lStatus > 0 And lStatus <= 700) Then
                  ' if this value is > 700 but <= 790, then re-OCR is required, if it is greater than 790, then re-importing is needed - extend the script below to set a different output state, other than the standard "DesiredOutputStateSucceeded" one
                  Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "Loading return code is {" & CStr(Err.Number) & "} and loading status is {" & CStr(lStatus) & "}"
                  Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "Ignoring internal error when loading Workdoc [" & theWorkdoc.Filename & "]"
                  Err.Clear
                  theWorkdoc.DocClassName = ""
                  theWorkdoc.Fields.Clear
                  theWorkdoc.RebuildBasicObjects
                  If Err.Number <> 0 Then
                     Project.LogScriptMessageEx CDRTypeWarning, CDRSeveritySystemMonitoring, "Recovery script: RebuildBasicObjects failed with error code [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
                     Err.Clear
                     Project.LogScriptMessageEx CDRTypeWarning, CDRSeveritySystemMonitoring, "Recovery script: Proceeding with attempt to redirecting document to re-OCR state" ' [AE] [2012-02-27]
                     DesiredOutputStateSucceeded = 100 ' [AE] [2012-02-27]
                     theWorkdoc.DocState = CDRDocStateHaveDocs ' [AE] [2012-02-28] This call internally triggeres invoking of ".InternalClear(false,true)
                  End If
                  pBatch.FolderDocState(lFolderIndex, lDocIndex) = DesiredOutputStateSucceeded
                  If Err.Number <> 0 Then
                     Project.LogScriptMessageEx CDRTypeError, CDRSeveritySystemMonitoring, "Recovery script: put_FolderDocState failed with error code [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
                     Err.Clear
                  End If
                  pBatch.UpdateDocument(theWorkdoc, lFolderIndex, lDocIndex)
                  If Err.Number <> 0 Then
                     Project.LogScriptMessageEx CDRTypeError, CDRSeveritySystemMonitoring, "Recovery script: UpdateDocument failed with error code [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
                     Err.Clear
                  End If
               End If
               If Err.Number <> 0 Or (lStatus > 700 And lStatus <= 790) Then ' if this value is > 700 but <= 790, then re-OCR is required, if it is greater than 790, then re-importing is needed - extend the script below to set a different output state, other than the standard "DesiredOutputStateSucceeded" one
                  Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "Loading return code is {" & CStr(Err.Number) & "} and loading status is {" & CStr(lStatus) & "}"
                  Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "Ignoring internal error when loading Workdoc [" & theWorkdoc.Filename & "]"
                  Err.Clear
                  DesiredOutputStateSucceeded = 100
                  theWorkdoc.DocState = CDRDocStateHaveDocs ' [AE] [2012-02-28] This call internally triggeres invoking of ".InternalClear(false,true)
                  pBatch.FolderDocState(lFolderIndex, lDocIndex) = DesiredOutputStateSucceeded
                  If Err.Number <> 0 Then
                     Project.LogScriptMessageEx CDRTypeError, CDRSeveritySystemMonitoring, "Recovery script: put_FolderDocState failed with error code [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
                     Err.Clear
                  End If
                  pBatch.UpdateDocument(theWorkdoc, lFolderIndex, lDocIndex)
                  If Err.Number <> 0 Then
                     Project.LogScriptMessageEx CDRTypeError, CDRSeveritySystemMonitoring, "Recovery script: UpdateDocument failed with error code [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
                     Err.Clear
                  End If
               End If
               ' [AE] [2012-03-05] Test that recovery has been succeeded and the Workdoc can now be loaded with no issues. This is one extra safety solution: "Load document one more time to "test" and recover for (from) real document file corruptions".
               If lStatus > 0 And lStatus <= 790 Then
                  Set theWorkdoc = Nothing
                  Err.Clear
                  Set theWorkdoc = pBatch.LoadWorkdoc(lFolderIndex, lDocIndex)
                  vLoadingCompletenessStatus = theWorkdoc.NamedProperty("LoadingCompletenessStatus")
                  lStatus = vLoadingCompletenessStatus
                  If Err.Number <> 0 Or lStatus > 0 Then
                     lStatus = 799
                  End If
               End If
               ' [AE] [2012-03-27] Additional check for consistency of loaded document files
               If lStatus = 0 Then
                  Err.Clear
                  Set theImage = theWorkdoc.Pages(0).Image(0)
                  If Err.Number <> 0 Or theImage Is Nothing Then
                     lStatus = 999
                     bNeedSafetyRestart = True
                  End If
               End If
               If Err.Number <> 0 Or (lStatus > 790) Then ' if this value is > 700 but <= 790, then re-OCR is required, if it is greater than 790, then re-importing is needed - extend the script below to set a different output state, other than the standard "DesiredOutputStateSucceeded" one
                  Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "Loading return code is {" & CStr(Err.Number) & "} and loading status is {" & CStr(lStatus) & "}"
                  Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "Ignoring internal error when loading Workdoc [" & theWorkdoc.Filename & "]"
                  Project.LogScriptMessageEx CDRTypeWarning, CDRSeverityEmailNotification, "Document [" & strWorkdocName & "] with stream exit code [" & CStr (lStatus) & "] will be redirected to manual processing state"
                  Err.Clear
                  DesiredOutputStateSucceeded = 850
                  pBatch.FolderDocState(lFolderIndex, lDocIndex) = DesiredOutputStateSucceeded
                  If Err.Number <> 0 Then
                     Project.LogScriptMessageEx CDRTypeError, CDRSeveritySystemMonitoring, "Recovery script: put_FolderDocState failed with error code [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
                     Err.Clear
                  End If
                  ' Do not call update document in case of 850 type recovery - just update the document state via the call above
                  ' pBatch.UpdateDocument(theWorkdoc, lFolderIndex, lDocIndex)
                  ' If Err.Number <> 0 Then
                     ' Project.LogScriptMessageEx CDRTypeError, CDRSeveritySystemMonitoring, "Recovery script: UpdateDocument failed with error code [" & CStr(Err.Number) & "] and error description [" & Err.Description & "]"
                     ' Err.Clear
                  ' End If
               End If
               Set theWorkdoc = Nothing
               ' Auto-apply the RTS instance restart after recovering every single case of true document loading failure. This is to ensure that corruption's side effects are not cumulated across multiple auto-recovered documents and clean documents are not negatively affected by attempts to load a corrupted one.
               If bNeedSafetyRestart = True Then
                  Project.PerformScriptCommandRTS(1, 0, 0, "Applying safety recovery restart")
                  GoTo LABEL_SUCCESS
               End If
	      End If
      Next lDocIndex		  
   Next lFolderIndex
   LABEL_SUCCESS:
   Project.LogScriptMessageEx CDRTypeInfo, CDRSeveritySystemMonitoring, "ScriptModule_ProcessBatch finished successfully, batch <" & CStr(pBatch.BatchID) & ">, new state <" & CStr(DesiredOutputStateSucceeded) & ">, old state <" & CStr(InputState) & ">"
   Exit Sub
   LABEL_ERROR:
   Project.LogScriptMessageEx CDRTypeError, CDRSeveritySystemMonitoring, "ScriptModule_ProcessBatch, finished with Error: " & Err.Description
End Sub