×
Menu
Index

3.3.14.4. Working with M-FILES Value Lists

 
This article shows basic scripts to work with M-FILES value lists using client API and VBScripting:
 
 

Sample 1: Fill Country List in a ChronoScan Field

 
Set the edit property "Help List" to "Show Window".
 
Edit the event "OnEnterField" (right click on your field properties):
 
Vault = "My Vault"
ListId = 154 ' Countries
Set oMFAPIApp = CreateObject("MFilesAPI.MFilesClientApplication")
 
Set g_oVault = oMFAPIApp.BindToVault(Vault, 0, True, True)
 
Set oItems = g_oVault.ValueListItemOperations.GetValueListItems(ListId, True, 0)
 
Call UserField_Country.HelpList_Clear()
 
For Each Item In oItems
    Call UserField_Country.HelpList_AddValueDescription(Item.Name,Item.ID)
Next
 
Call UserField_Country.HelpList_Populate()
 
This will populate the helplist when focusing the field:
 
 
 

Sample 2: Adding Values to an External M-FILES List

 
Use this script as base sample to allow your users to create new values on a M-FILES value list:
 
Edit the event "OnValueChanged":
 
Const MFValueListItemPropertyDefName = 2
Const MFParentChildBehaviorNone = 0
Const MFDatatypeText = 1
Const MFExternalDBRefreshTypeNone = 0
 
Vault = "My Vault"
MyListId = 154
 
' Check If the value exists. If not, ask user to create a new One
Sub Main()
    If UserField_Country.value = "" Then Exit Sub
 
    Set oMFAPIApp = CreateObject("MFilesAPI.MFilesClientApplication")
 
    Set g_oVault = oMFAPIApp.BindToVault(Vault, 0, True, True)
 
    Set scValueListItem = CreateObject("MFilesAPI.SearchCondition")
 
    Call scValueListItem.Expression.SetValueListItemExpression( MFValueListItemPropertyDefName,MFParentChildBehaviorNone, Nothing )
 
    scValueListItem.ConditionType = 1 ' MFConditionTypeEqual
 
    Call scValueListItem.TypedValue.SetValue( MFDatatypeText , UserField_Country.value )
 
    Set arrSearchConditions = CreateObject("MFilesAPI.SearchConditions")
 
    Call arrSearchConditions.Add(-1, scValueListItem)
 
    Set oItems = g_oVault.ValueListItemOperations.SearchForValueListItems(MyListId, arrSearchConditions, True, MFExternalDBRefreshTypeNone)
 
    If oItems.Count = 0 Then
        ' Value Doesn't exist, allow user to create a new one
        If MsgBox("The value ["&UserField_Country.value&"] does not exist in the list COUNTRIES, Do you want to create it ?", vbYesNo ) = vbYes Then
            Set value = CreateObject("MFilesAPI.ValueListItem")
 
            value.ValueListID = MyListId
            value.Name = UserField_Country.value
 
            Set newvalue = g_oVault.ValueListItemOperations.AddValueListItem(MyListId, value)
 
            If Not newvalue Is Nothing Then
                Call UserField_Country.SetListValue(newvalue.Name, newvalue.ID)
            Else
                UserField_Country.value = ""
            End If
        Else
            UserField_Country.value = ""
        End If
    End If
End Sub
 
Call Main()
 

Export Values to a Lookup List Type:

 
To export value to a list field it is necessary to send the list value ID instead of the list value itself. If you're using the samples above, you can export using the %docfield.sysval_list_desc_FIELDNAME% internal value that retains the populated description (ID):
 
 
Note: Use the variable wizard and search for _desc_ to show all available list description id's.