Adding Multiple Values to a Single Property Field via Event Handler Search

Hello,

The current scenario is that an order has been placed and there are a number of commissions that need to be tied to this order. I've tried to create an event handler with limited success. It is able to grab one of the many commissions but not all. Would anyone happen to know how to achieve this?

I've attached the event handler and the current output to this post.

option Explicit

If ObjVer.Type = 111 Then
	
	Dim PDSOPOrder, OTSOPNumberOrder, OTCommission, ValueSOPNumberOrder, CLOrder, PDSOPNumberOrder, PDCommission
	
	OTSOPNumberOrder = Vault.ObjectTypeOperations.GetObjectTypeIDByAlias("OT.SopNumberOrders")
	
	OTCommission = Vault.ObjectTypeOperations.GetObjectTypeIDByAlias("OT.Commission")
	
	PDSOPOrder = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("PD.OrderNumber")
	
	CLOrder = Vault.ClassOperations.GetObjectClassIDByAlias("CL.Order")
	
	PDSOPNumberOrder = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("PD.SOPNumberOrder")
	
	PDCommission = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("PD.Commission")
	
	Dim PropertyValues : Set PropertyValues = Vault.ObjectPropertyOperations.GetProperties(objver)
	
	If PropertyValues.SearchForProperty(100).TypedValue.GetValueAsLookup.Item = CLOrder Then
		
		If PropertyValues.IndexOf(PDSOPOrder) <> -1 Then
			
			ValueSOPNumberOrder = PropertyValues.SearchForProperty(PDSOPOrder).TypedValue.DisplayValue
			
			If ValueSOPNumberOrder <> "" Then
				
				Dim oSC, oSCs
				
				Set oSC = CreateObject("MFilesAPI.SearchCondition")
				Set oSCs = CreateObject("MFilesAPI.SearchConditions")
				
				oSC.ConditionType = MFConditionTypeEqual
				oSC.Expression.DataStatusValueType = MFStatusTypeObjectTypeID
				oSC.TypedValue.SetValue MFDataTypeLookup, OTCommission
				
				oSCs.Add -1, oSC
				
				oSC.ConditionType = MFConditionTypeEqual
				oSC.Expression.DataStatusValueType = MFStatusTypeDeleted
				oSC.TypedValue.SetValue MFDataTypeBoolean, False
				
				oSCs.Add -1, oSC
				
				oSC.ConditionType = MFConditionTypeEqual
				oSC.Expression.DataPropertyValuePropertyDef = PDSOPNumberOrder
				oSC.TypedValue.SetValue MFDataTypeText, ValueSOPNumberOrder
				
				oSCs.Add -1, oSC
				
				Dim oSearchResults
				Set oSearchResults = Vault.ObjectSearchOperations.SearchForObjectsByConditions(oSCs, MFSearchFlagNone, False)
				
				If oSearchResults.Count > 0 Then
					
					Dim PropertyValue
					Set PropertyValue = CreateObject("MFilesAPI.PropertyValue")
														
					PropertyValue.PropertyDef = PDCommission
						
						For Each oSearchResults In oSearchResults
						PropertyValue.TypedValue.SetValue MFDataTypeMultiSelectLookup, oSearchResults.ObjVer.ID
					
						Vault.ObjectPropertyOperations.SetProperty ObjVer, PropertyValue
					Next
				End If
			End If
		End If
	End If
End If

Parents
  • You would have to create a list of lookups as a variable. Then in your For Each loop you should add the result to that list. When done you use the list as input to the property value. There is a trick to this that Craig Hawker once taught me, but right now I can't recall it.

    Craig, you are a fair bit  younger - please help us out here....

  • Damnit, put me on the spot Karl... Joy

    I don't recall a trick.  There are some helper methods in C#, which I would use for new code in any vault these days (as Microsoft has said it'll stop supporting VBScript at some point, so implementing new VBScript is not something I'd recommend).

    But this should work:

    ' Add all items to the lookups.
    Dim oLookups: Set oLookups = CreateObject("MFilesAPI.Lookups")
    For Each oSearchResults In oSearchResults
        oLookups.Add -1, oSearchResults.ObjVer.ID
    Next
    
    ' Set the value.
    PropertyValue.TypedValue.SetValueToMultiSelectLookup oLookups
    Vault.ObjectPropertyOperations.SetProperty ObjVer, PropertyValue
    

    Some points:

    • The lookups collection contains all items you want on the property
    • The setting of the property value is moved outside the loop
Reply Children
  • Oh yes, of course.  The Lookups collection expects a Lookup.

    ' Add all items to the lookups.
    Dim oLookups: Set oLookups = CreateObject("MFilesAPI.Lookups")
    For Each oSearchResults In oSearchResults
        Dim oLookup: Set oLookup = CreateObject("MFilesAPI.Lookup")
        oLookup.Item = oSearchResults.ObjVer.ID
        oLookups.Add -1, oLookup
    Next
    
    ' Set the value.
    PropertyValue.TypedValue.SetValueToMultiSelectLookup oLookups
    Vault.ObjectPropertyOperations.SetProperty ObjVer, PropertyValue