AutoStore: AutoCapture Folder Name List



Can an AutoCapture list field be loaded with the folder names located in a particular directory?

The short answer is absolutely, and in this post, we'll look at a simple example which does just that. Where a folder lookup can be useful is when the name of the subfolder represents an entity within its parent folder, and a fresh list is needed to complete the destination path.

To illustrate, let's begin by examining a Vendors folder that contains subfolders representing different vendors.

  • Shared Drive
    • Accounting
      • Vendors
        • Vendor A
        • Vendor B
        • Vendor C
        • Vendor D

The Vendor names could be manually added to an AutoCapture String list field, or also added dynamically using a form script. We'll examine a couple of touch points:

  • Loading the list field
  • Default a blank list item


We'll begin by building a new configuration using the AutoCapture capture component, and the Send to Folder route component. Create an AutoCapture form, and add a new String List field named Vendor Name. This field will represent the name of the Vendor to be selected.

Load the List Field


There are a few major parts we'll need to implement to fill the list field with items:
  • Establish the folder path of the parent directory
  • Get the names of all the subfolders in the parent directory
  • Load those names into the AutoCapture String List field

In the VBScript section of the local form setup, to the right of the Script Path field, clicking on the ellipsis [...] button opens a script template with empty event handlers. This serves as our starting point to introduce our functionality.

The AutoCapture script template
Sub Form_OnLoad(Form)

End Sub 


Function Form_OnValidate(Form)

End Function


Sub Field_OnChange(Form, FieldName, FieldValue)

End Sub


Function Field_OnValidate(FieldName, FieldValue)

End Function


Sub Button_OnClick(Form, ButtonName)

End Sub



Next, the target directory needs to be defined. In this example, we'll declare a constant to hold that path at the top of the script.

Define the target directory path
' Reference a UNC Path or local physical disk ...
Const PATH_VENDORS = "\\FileServer\Accounting\Vendors"

Sub Form_OnLoad(Form)
...

Note: As a best practice, either use a UNC path to a directory on a remote server, or a path on a local physical disk. Avoid using a mapped-letter drive path, which is a virtual network reference bound to (part of) a user login session.


To obtain all of the folder names in the target directory, we can use a custom function that returns a pipe-delimited list that we'll need to parse through. This function will be placed after the event handlers at the bottom of the script.

GetSubFolderList() Function returns a delimited string of subfolder names
Function GetSubFolderList(folderPath)
    ' Declare variables ...
    Dim strFolderList : strFolderList = ""
    Dim objFSO, objFolder, objFolderCollection, objSubFolder
    ' Assign the FileSystemObject ...
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ' Assign the target folder object ...
    Set objFolder = objFSO.GetFolder(folderPath)
    ' Assign the folder collection object ...
    Set objFolderCollection = objFolder.SubFolders
    ' Go through the subfolders and build a delimited string ...
    For Each objSubFolder In objFolderCollection
        If Len(strFolderList) = 0 Then
            strFolderList = objSubFolder.Name
        Else
            strFolderList = strFolderList & "|" & objSubFolder.Name
        End If
    Next
    ' Return the delimited string ...
    GetSubFolderList = strFolderList
End Function


Using the example directory, the formed string return would look like:

"Vendor A|Vendor B|Vendor C|Vendor D"


Now, we can use the string returned by the GetSubFolderList() function to set each folder name by splitting the items on each delimiter. We'll establish this in the Form_OnLoad event handler so the list is available when the AutoCapture form is loaded.

Obtain the subfolder names, and load the list field
' Reference a UNC Path or local physical disk ...
Const PATH_VENDORS = "\\FileServer\Accounting\Vendors"

Sub Form_OnLoad(Form)
    ' Declare variables ...
    Dim strSubFolders, arrSubFolders, strItem, strLabel, strValue
    ' Obtain the subfolders ...
    strSubFolders = GetSubFolderList(PATH_VENDORS)
    ' Split the pipe-delimited return ...
    arrSubFolders = Split(strSubFolders, "|")
    ' Go through the subfolders ...
    For Each strItem In arrSubFolders
        strLabel = strItem
        strValue = strItem
        ' Add the label and value to the Vendor Name list ...
        Form.Fields.Field("Vendor Name").AddListItem strLabel, strValue
    Next
End Sub


All of the basics for the form script are now in place. In the script editor's Toolbar, click on the Compile button to check for any compilation errors.



If there are no errors, then the script's syntax is understandable to the compiler.



Save and close the script. For the AutoCapture form configuration, make sure the option to Run on form load is enabled so that the statements we placed within the Form_OnLoad event are executed.



Go to the Send to Folder route component to configure the root path to the destination, and include the Vendor Name RRT to the path.



Save the CFG, and start the service to perform an end to end test. Now, your AutoCapture form should show the subfolder names in the list field.



With the basics of loading the list field out of the way, there is another element we should implement. Notice that the list field's value is set to the first subfolder. Without the user actually making a selection, the form is ready to be submitted. We do not want that to happen.

Default Blank List Item


Let's now initialize the list field so that an actual vendor name needs to be picked by the user. Inside of our existing script, we'll need to do the following:
  • Establish the initial list field value
  • Load the initial list field value into the list field
  • Make sure the initial list field value is not selected

Open the AutoCapture component to edit the form, and enable the Run on form validate option.



Click on the Script File ellipsis [...] button to open the script editor. We'll add another constant at the top of the script to set the value for the initial list item. In this case, two double-quote marks ("") indicates a zero-length string, which appears as a blank value.

Define the initial value for list field
' Reference a UNC Path or local physical disk ...
Const PATH_VENDORS = "\\FileServer\Accounting\Vendors"

' The initial value to load for the Vendor Name list ...
Const LOADLISTITEM = ""

Sub Form_OnLoad(Form)
...


When the Vendor Name list is loaded, this initial value needs to be included. So, we'll go back into the Form_OnLoad event handler to set this value into the list field. We want to make sure that the initial list item to load is done before the subfolder items are added.

Add the list field's initial value
Sub Form_OnLoad(Form)
    ' Declare variables ...
    Dim strSubFolders, arrSubFolders, strItem, strLabel, strValue
    ' Add the initial item to the list ...
    Form.Fields.Field("Vendor Name").AddListItem LOADLISTITEM, LOADLISTITEM
    ' Obtain the subfolders ...
    strSubFolders = GetSubFolderList(PATH_VENDORS)
    ' Split the pipe-delimited return ...
    arrSubFolders = Split(strSubFolders, "|")
    ' Go through the subfolders ...
    For Each strItem In arrSubFolders
        strLabel = strItem
        strValue = strItem
        ' Add the label and value to the Vendor Name list ...
        Form.Fields.Field("Vendor Name").AddListItem strLabel, strValue
    Next
End Sub


Although the Vendor Name field is required in the form configuration, the initial value ("") is a zero-length string, which technically is an actual value. So, if we submit the form with what appears to be an empty value, the form will still go through.

In the Form_OnValidate event handler, we can check the value of the Vendor Name list field, and prevent the form from being submitted if the list field value is the same as the constant value that we defined.

Define the Vendor Name list field's initial value
Function Form_OnValidate(Form)
    If Form.GetFieldValue("Vendor Name") = LOADLISTITEM Then
        Form_OnValidate = "Select an item from the 'Vendor Name' list."
    End If
End Function


Save the script, and check out the new list. That initial item appears to be blank.



With that blank item selected, when a form submission occurs, the user is informed to make a selection in the list field.



Select one of the subfolders in the list, and the form allows the file to be delivered to the subfolder.

In Closing


For the initial value, you could also substitute the zero-length string ("") with something like "Please select an item" as an example. Because the constant is referenced in the working statements of the script, the form validation would work the same way.

Also, any function that produces a pipe-delimited string could be used as the items for the list. Perhaps the values come from another type of data source, such as a text file, or maybe a database. If the function can get it, the AutoCapture list field can show it.

Finally, here is a link to download the entire sample configuration. We started out with fetching the subfolder names, and adding each one to the list field. Then, adding a blank value leads the user to make a selection from the list. Until next time, cheers!

Comments

Popular posts from this blog

VBScript: Ensure Backslash Folder Path

AutoStore: Create a Custom RRT

AutoStore: Workflow Loop Example