This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Conditions don't work

Hi,

I'm working on my first "real" M-Files code and first time with conditions. I get type mismatch and that stuff.
What I am trying to achive is: "User sets start and end date that are used to search Projects those dates fit between those user specified dates. Results should be put into MLSU. Search result to MLSU is still to be figured out. I can put one element to MLSU with code there is. So this will be sort of template where user can flip date ranges - Poor mans Gantt on Excel.


option explicit

' Initialize an array of search conditions.
Dim oSearchConditions : Set oSearchConditions = CreateObject("MFilesAPI.SearchConditions")
Dim oSearchCondition1 : Set oSearchCondition1 = CreateObject("MFilesAPI.SearchCondition")
Dim oSearchCondition2 : Set oSearchCondition2 = CreateObject("MFilesAPI.SearchCondition")
Dim oSearchCondition3 : Set oSearchCondition3 = CreateObject("MFilesAPI.SearchCondition")

' Create a search condition for the object class (Ty?maa=Project).
oSearchCondition1.ConditionType = MFConditionTypeEqual
oSearchCondition1.Expression.SetPropertyValueExpression(1026, MFParentChildBehavior.MFParentChildBehaviorNone)
oSearchCondition1.TypedValue.SetValue MFDataType.MFDatatypeLookup, 105
oSearchConditions.Add -1, oSearchCondition1

' Create a search condition for two dates
oSearchCondition1.ConditionType = MFConditionTypeGreaterThanOrEqual
oSearchCondition1.Expression.SetPropertyValueExpression(1124)
oSearchCondition1.TypedValue.SetValue MFDataType.MFDatatypeDate, 1036
oSearchConditions.Add -1, oSearchCondition2

' Create a search condition for two dates
oSearchCondition1.ConditionType = MFConditionTypeLessThanOrEqual
oSearchCondition1.Expression.SetPropertyValueExpression(1125)
oSearchCondition1.TypedValue.SetValue MFDataType.MFDatatypeDate, 1050
oSearchConditions.Add -1, oSearchCondition3

' Execute the search.
Dim oSearchResult
Dim oSearchResults
Set oSearchResults = Vault.ObjectSearchOperations.SearchForObjectsByConditions(oSearchConditions, MFSearchFlagNone, False)

'Err.Raise MFScriptCancel, oSearchCondition1

Dim oPropertyValue: Set oPropertyValue = CreateObject("MFilesAPI.PropertyValue")
Dim oPropertyValues: Set oPropertyValues = CreateObject("MFilesAPI.PropertyValues")

Dim oLookup: Set oLookup = CreateObject("MFilesAPI.Lookup")
Dim oLookups: Set oLookups = CreateObject("MFilesAPI.Lookups")

oLookup.Item = 2
oLookup.Version = -1
oLookups.Add -1, oLookup

oPropertyValue.PropertyDef = 1123
oPropertyValue.TypedValue.SetValueToMultiSelectLookup oLookups
oPropertyValues.Add -1, oPropertyValue

Vault.ObjectPropertyOperations.SetProperties ObjVer, oPropertyValues


Thanks

PJ

  • Hi

    Maybe oLookups variable is not type of MFilesAPI.Lookups?
    I see oLookups is declared (with dim keyword) in line 32 but I don't see where lookups object is set to new instance of MFilesAPI.Lookups .

    Maybe changing line 32 into following helps?:
    Dim oLookups: set oLookups = CreateObject("MFilesAPI.Lookups")



    That line 32 you proposed removed errors, but nothing goes to my MLSU. Is there a way to debug lookups?

    The code now:
    option explicit

    ' Initialize an array of search conditions.
    Dim oSearchConditions : Set oSearchConditions = CreateObject("MFilesAPI.SearchConditions")

    ' Create a search condition for the object class (Ty?maa=Project).
    Dim oSearchCondition1 : Set oSearchCondition1 = CreateObject("MFilesAPI.SearchCondition")
    oSearchCondition1.ConditionType = MFConditionTypeEqual
    oSearchCondition1.Expression.SetPropertyValueExpression MFBuiltInPropertyDefClass, MFParentChildBehaviorNone, Nothing
    oSearchCondition1.TypedValue.SetValue MFDataType.MFDatatypeLookup, 3
    oSearchConditions.Add -1, oSearchCondition1

    ' Create a search condition for two dates StartDate
    Dim oSearchCondition2 : Set oSearchCondition2 = CreateObject("MFilesAPI.SearchCondition")
    oSearchCondition2.ConditionType = MFConditionTypeGreaterThanOrEqual
    oSearchCondition2.Expression.DataPropertyValuePropertyDef = 1036
    oSearchCondition2.TypedValue.SetValue MFDataType.MFDatatypeDate, 1124
    oSearchConditions.Add -1, oSearchCondition2

    ' Create a search condition for two dates - EndDate
    Dim oSearchCondition3 : Set oSearchCondition3 = CreateObject("MFilesAPI.SearchCondition")
    oSearchCondition3.ConditionType = MFConditionTypeLessThanOrEqual
    oSearchCondition3.Expression.DataPropertyValuePropertyDef = 1050
    oSearchCondition3.TypedValue.SetValue MFDataType.MFDatatypeDate, 1125
    oSearchConditions.Add -1, oSearchCondition3

    ' Execute the search.
    Dim oSearchResult
    Dim oSearchResults
    Set oSearchResults = Vault.ObjectSearchOperations.SearchForObjectsByConditions(oSearchConditions, MFSearchFlagNone, False)

    Dim oLookups: Set oLookups = CreateObject("MFilesAPI.Lookups")
    Dim iCounter
    For iCounter = 1 To oSearchResults.Count
    Dim oResult: Set oResult = oSearchResults.Item(iCounter)
    Dim oLookup: Set oLookup = CreateObject("MFilesAPI.Lookup")
    oLookup.Type = oResult.ObjVer.Type
    oLookup.Item = oResult.ObjVer.ID
    oLookup.SetLatestVersion
    oLookups.Add -1, oLookup
    Next

    'Err.Raise MFScriptCancel, oLookups

    Dim oPropertyValue: Set oPropertyValue = CreateObject("MFilesAPI.PropertyValue")
    Dim oPropertyValues: Set oPropertyValues = CreateObject("MFilesAPI.PropertyValues")

    oPropertyValue.PropertyDef = 1123
    oPropertyValue.TypedValue.SetValueToMultiSelectLookup oLookups
    oPropertyValues.Add -1, oPropertyValue

    Vault.ObjectPropertyOperations.SetProperties ObjVer, oPropertyValues


    PJ
  • Hi,

    You could check count's of both oSearchResults and oLookups.

    Err.Raise MFScriptCancel, oSearchResults.Count

    and
    Err.Raise MFScriptCancel, oLookups.Count


    if search result count is 0 then nothing was found and you should look into conditions. For example try to manually create same search conditions in m-files client and see how many objects are returned from there

  • I'd like to draw your attention to Lines 17 and 24.
    What date do you expect 1124 and 1125 to evaluate?
    Maybe something else should be there.
    As debugging measure, you could try with date literals (eg. ISO 8601 date surrounded with # sign).
    Try putting something you know that will find something.

    for example:
    oSearchCondition2.TypedValue.SetValue MFDataType.MFDatatypeDate, #1991-01-01#

    and
    oSearchCondition3.TypedValue.SetValue MFDataType.MFDatatypeDate, #2019-01-28#


    later change it to what ever value you want to pass dynamically.


  • I'd like to draw your attention to Lines 17 and 24.
    What date do you expect 1124 and 1125 to evaluate?
    Maybe something else should be there.
    As debugging measure, you could try with date literals (eg. ISO 8601 date surrounded with # sign).
    Try putting something you know that will find something.

    for example:
    oSearchCondition2.TypedValue.SetValue MFDataType.MFDatatypeDate, #1991-01-01#

    and
    oSearchCondition3.TypedValue.SetValue MFDataType.MFDatatypeDate, #2019-01-28#


    later change it to what ever value you want to pass dynamically.


    You are the man tigu!
    Problem was those dates. I just wonder why?


    oSearchCondition2.TypedValue.SetValue MFDataType.MFDatatypeDate, #2018-01-01# '1124
    oSearchCondition3.TypedValue.SetValue MFDataType.MFDatatypeDate, #2020-12-31# '1125


    1124 and 1125 are both date fields and contain those same dates that user sets as query for dates that should be returned.
    Is it some date formatting issue or what? Those look like this in my environment 1.1.2018 and 31.12.2020. Go figure...

    PJ




  • I'd like to draw your attention to Lines 17 and 24.
    What date do you expect 1124 and 1125 to evaluate?
    Maybe something else should be there.
    As debugging measure, you could try with date literals (eg. ISO 8601 date surrounded with # sign).
    Try putting something you know that will find something.

    for example:
    oSearchCondition2.TypedValue.SetValue MFDataType.MFDatatypeDate, #1991-01-01#

    and
    oSearchCondition3.TypedValue.SetValue MFDataType.MFDatatypeDate, #2019-01-28#


    later change it to what ever value you want to pass dynamically.


    You are the man tigu!
    Problem was those dates. I just wonder why?


    oSearchCondition2.TypedValue.SetValue MFDataType.MFDatatypeDate, #2018-01-01# '1124
    oSearchCondition3.TypedValue.SetValue MFDataType.MFDatatypeDate, #2020-12-31# '1125


    1124 and 1125 are both date fields and contain those same dates that user sets as query for dates that should be returned.
    Is it some date formatting issue or what? Those look like this in my environment 1.1.2018 and 31.12.2020. Go figure...

    PJ
    [/quote]
    Based on the code above, you've not been setting the search conditions to properties, but rather you've been trying to search for objects where condition 1, as a date, matches '1124', and where condition 2 matches '1125' (which, if we try and convert these literal values to dates in Excel, give 28/01/1903 and 29/01/1903, which may or may not also be the date values M-Files is trying to search for) Try replacing these with
    PropertyValues.SearchForProperty(1124).TypedValue.Value

    There's no explicit method for getting a property value as a date, but if the property is a date field, then TypedValue.Value should get the value in the property already formatted as a date.
  • Thanks a lot everyone that helped me. Now it works as I wanted. Here is the code if someone else is fighting with the same.

    option explicit

    ' Initialize an array of search conditions.
    Dim oSearchConditions : Set oSearchConditions = CreateObject("MFilesAPI.SearchConditions")

    ' Create a search condition for the object class (Ty?maa=Project=ID3).
    Dim oSearchCondition1 : Set oSearchCondition1 = CreateObject("MFilesAPI.SearchCondition")
    oSearchCondition1.ConditionType = MFConditionTypeEqual
    oSearchCondition1.Expression.SetPropertyValueExpression MFBuiltInPropertyDefClass, MFParentChildBehaviorNone, Nothing
    oSearchCondition1.TypedValue.SetValue MFDataType.MFDatatypeLookup, 3
    oSearchConditions.Add -1, oSearchCondition1

    ' Create a search condition for two dates StartDate
    Dim StartDate : StartDate = 1124
    Dim oSearchCondition2 : Set oSearchCondition2 = CreateObject("MFilesAPI.SearchCondition")
    oSearchCondition2.ConditionType = MFConditionTypeGreaterThanOrEqual
    oSearchCondition2.Expression.DataPropertyValuePropertyDef = 1036
    oSearchCondition2.TypedValue.SetValue MFDataType.MFDatatypeDate, PropertyValues.SearchForProperty(StartDate).TypedValue.Value
    oSearchConditions.Add -1, oSearchCondition2

    ' Create a search condition for two dates - EndDate
    Dim EndDate : EndDate = 1125
    Dim oSearchCondition3 : Set oSearchCondition3 = CreateObject("MFilesAPI.SearchCondition")
    oSearchCondition3.ConditionType = MFConditionTypeLessThanOrEqual
    oSearchCondition3.Expression.DataPropertyValuePropertyDef = 1050
    oSearchCondition3.TypedValue.SetValue MFDataType.MFDatatypeDate, PropertyValues.SearchForProperty(EndDate).TypedValue.Value
    oSearchConditions.Add -1, oSearchCondition3

    ' Execute the search.
    Dim oSearchResult
    Dim oSearchResults
    Set oSearchResults = Vault.ObjectSearchOperations.SearchForObjectsByConditions(oSearchConditions, MFSearchFlagNone, False)

    'Err.Raise MFScriptCancel, oSearchResults.Count

    Dim oLookups: Set oLookups = CreateObject("MFilesAPI.Lookups")
    Dim iCounter
    For iCounter = 1 To oSearchResults.Count
    Dim oResult: Set oResult = oSearchResults.Item(iCounter)
    Dim oLookup: Set oLookup = CreateObject("MFilesAPI.Lookup")
    ' oLookup.Type = oResult.ObjVer.Type
    oLookup.Item = oResult.ObjVer.ID
    oLookup.SetLatestVersion
    oLookups.Add -1, oLookup
    Next

    'Err.Raise MFScriptCancel, oLookups.Count

    Dim oPropertyValue: Set oPropertyValue = CreateObject("MFilesAPI.PropertyValue")
    Dim oPropertyValues: Set oPropertyValues = CreateObject("MFilesAPI.PropertyValues")

    oPropertyValue.PropertyDef = 1123
    oPropertyValue.TypedValue.SetValueToMultiSelectLookup oLookups
    oPropertyValues.Add -1, oPropertyValue

    Vault.ObjectPropertyOperations.SetProperties ObjVer, oPropertyValues


    Cheers

    PJ
  • Thanks for sharing this. It really assisted me with assigning results from a search to a Multiselect data type.