Classify based on Batch Field

There’s a question that keeps surfacing over and over again: using a batch field, how could KTM classify a document to match that field’s value? Such a behaviour is desired if you:

  • Already classify before scan time, when creating physical batches;
  • Want to have one KTM setup for multiple documents;
  • Want to keep the batch classes in KC low.

Here’s an example use case: you scan invoice for three different accounting areas. You use only one KTM setup as the invoices share many characteristics, such as the fields and even some locators, so you make use of inheritance – one base class, and three child classes. The child classes have their specialized setup, such as different trainable group locators, database locators for supplier identification, and so on. Still, you want only one batch class, but the users have to pick which accounting area – or class in KTM – the scanned batch belongs to.

I’ve seen many overly complicated attempts, parsing XValues in the BatchOpen event, storing values to a field – or even worse – a globally accessible property; however, I believe this is the most efficient approach:

Step 1: Make batch fields accessible

In order for KTM to access batch fields, you need to check the following box in the extended synchronization settings. Then, and only then, batch fields are being made available as XValues in the XFolder object.

extended-sync-settings

 Step 2: Getting the batch field’s value

Using the following method, you can access a batch field’s value (source).


Public Function GetBatchField(FieldName As String, ByVal pXDoc As CASCADELib.CscXDocument) As String

   Dim folder As CscXFolder
   Set folder = pXDoc.ParentFolder
   While Not folder.IsRootFolder
      Set folder = folder.ParentFolder
   Wend
   If folder.XValues.ItemExists("AC_FIELD_" & FieldName) Then
      GetBatchField = folder.XValues.ItemByName("AC_FIELD_" & FieldName).Value
   Else
      GetBatchField = ""
   End If

End Function

 Step 3: Reclassify the document

Based on the value, you decide what to do with the given document. In the following script sample, the batch field’s value is used to reclassify a document to a class with the same name – given that class exists in KTM (the helper method ClassExists).


Private Sub Document_BeforeClassifyXDoc(ByVal pXDoc As CASCADELib.CscXDocument, ByRef bSkip As Boolean)

   Dim docClass As String

   ' reclassify the document based on a batch field's value
   docClass = GetBatchField("Class", pXDoc)
   ' check if the class does exist in KTM, and if yes, reclassify the document
   If ClassExists(docClass) Then
      pXDoc.Reclassify(docClass, 1)
   End If

End Sub

' checks if a given class does exist in the project
Public Function ClassExists(className) As Boolean

   Dim i As Integer
   For i = 0 To Project.ClassCount - 1
      If Project.ClassByIndex(i).Name = className Then ClassExists = True
   Next

End Function

 Last step: test!

Now, after creating the batch and selecting the respective value, all documents in the batch will be classified accordingly (to class blue in the screenshot below).

mapping

By the way, the question on how to retain KC’s classification has been already answered here.