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
Parents



  • 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.
Reply



  • 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.
Children
No Data