Search code examples
kofax

Get Values from ReleaseData.Values


When executing the OpenScript method of my release script I want to store the indexfields, batchfields and variables to lists. I created a snippet for this

Dictionary<string, string> indexFields = new Dictionary<string, string>();
Dictionary<string, string> batchFields = new Dictionary<string, string>();
Dictionary<string, string> kofaxValues = new Dictionary<string, string>();

foreach (Value val in documentData.Values)
{
    if (val.TableName.IsEmpty())
    {
        string sourceName = val.SourceName;
        string sourceValue = val.Value;

        switch (val.SourceType)
        {
            case KfxLinkSourceType.KFX_REL_INDEXFIELD:
                indexFields.Add(sourceName, sourceValue);
                break;

            case KfxLinkSourceType.KFX_REL_VARIABLE:
                kofaxValues.Add(sourceName, sourceValue);
                break;

            case KfxLinkSourceType.KFX_REL_BATCHFIELD:
                batchFields.Add(sourceName, sourceValue);
                break;
        }
    }
}

I want to do this because I need the field value. Each field name is unique so I can use it as a key.

When storing my custom properties to the ReleaseSetupData I can read them from the ReleaseData. Let's say two custom properties would return me the field name and the field type, so I know that the field is an IndexField and it's name is "MyIndexField".

I can use these information to access the Dictionary<string, string> indexFields and get the value from that Indexfield.

Currently I setup my ReleaseSetupData with this code

releaseSetupData.CustomProperties.RemoveAll();

// Save all custom properties here

releaseSetupData.CustomProperties.Add("myCustomProperty", "fooBar");

releaseSetupData.Links.RemoveAll();

foreach (IndexField indexField in releaseSetupData.IndexFields) // Save all IndexFields
{
    releaseSetupData.Links.Add(indexField.Name, KfxLinkSourceType.KFX_REL_INDEXFIELD, indexField.Name);
}

foreach (BatchField batchField in releaseSetupData.BatchFields) // Save all BatchFields
{
    releaseSetupData.Links.Add(batchField.Name, KfxLinkSourceType.KFX_REL_BATCHFIELD, batchField.Name);
}

foreach (dynamic batchVariable in releaseSetupData.BatchVariableNames) // Save all Variables
{
    releaseSetupData.Links.Add(batchVariable, KfxLinkSourceType.KFX_REL_VARIABLE, batchVariable);
}

When the OpenScript method of my release script gets executed, the dictionaries (shown in the first snippet) stay empty. This is because documentData.Values is empty.

How can I fill documentData.Values?


Solution

  • You can't. The order of events is as follows:

    1. OpenScript() is called - once per batch.
    2. ReleaseDoc() is called - once per document
    3. CloseScript() is called - once per batch.

    The Values collection holds information specific to an individual document and as such will be empty during OpenScript(). Sometimes this isn't what you want - you may want to access another document's values, or exporting them all at once - e.g. in a single web service call.

    Here's what I would recommend:

    Create a wrapper class for Kofax' Document object. Here's my approach (only properties are shown). This class has a constructor that accepts a ReleaseData object as the single parameter, and all respective properties are populated in said contructor.

    public class Document
      {
          public Dictionary<string, string> BatchFields { get; private set; }
          public Dictionary<string, string> IndexFields { get; private set; }
          public Dictionary<string,string> KofaxValues { get; set; }
          public Dictionary<string, string> TextConstants { get; set; }
          public Dictionary<string, string> CustomProperties { get; private set; }
          public Dictionary<string,string> ConfigurationSettings { get; set; }
          public List<Table> Tables { get; private set; }
          private List<Column> Columns;
          public List<string> ImageFileNames { get; private set; }
          public string KofaxPDFFileName { get; private set; }
          public string XdcFilePath { get; private set; }
          public XDocument XDocument { get; private set; }
          public string ImageFilePath { get; private set; }
          public string KofaxPDFPath { get; private set; }
          public string TextFilePath { get; private set; }
          public byte[] BinaryImage { get; private set; }
          public char CellSeparator { get; set; }
    }
    

    Then, during ReleaseDoc(), I would just add all of my Documents to a collection. Note that the connection documents is a defined as private in your ReleaseScript:

    public KfxReturnValue ReleaseDoc()
    {
      documents.Add(new Document(DocumentData));
    }
    

    You can then decide when and where to export your data. It could be during the CloseScript() event as well, but keep in mind that sanity checks and potential exceptions related to document data (invalid index field values, et cetera) must be thrown during ReleaseDoc(). Using a custom wrapper class and a collection adds a lot of flexibility and features native to .NET to your Export Connector, such as LINQ - here's an example (this is impossible with Kofax' COM objects):

    var noPdfs = documents.Where(x => x.KofaxPDFPath.Length == 0);