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
  • May be easier to see with the actual error stack, but at a glance it appears that you only ever populate "oSearchCondition1", but you add the other search conditions to the collection. Did you mean to instead populate oSearchCondition2 and oSearchCondition3?

    Regards,


    Craig.

  • I see you've also not set the ObjectType for oLookup. You initialise it, and then try to select a value from it without telling it what collection it should be pulling from.
  • First time with this stuff...

    Some typos, so now it works without errors.

    Lets assume I have all the projects in "SearchResults" from the condition.
    So I'm at the line 31. Rest is still totally unknow for me. Some help maybe?


    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(1026)
    oSearchCondition1.TypedValue.SetValue MFDataType.MFDatatypeLookup, 105
    oSearchConditions.Add -1, oSearchCondition1

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

    ' Create a search condition for two dates
    Dim oSearchCondition3 : Set oSearchCondition3 = CreateObject("MFilesAPI.SearchCondition")
    oSearchCondition3.ConditionType = MFConditionTypeLessThanOrEqual
    oSearchCondition3.Expression.SetPropertyValueExpression(1125)
    oSearchCondition3.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
  • In general terms what you need to do is iterate over the search results and create a set of Lookup objects for each result.

    The below is untested but should give you an idea of what you need to do:


    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


    So you basically iterate over your results, copy the Type and ID out of the search result and into the Lookup, then add the lookup to the collection. Once you have your Lookups collection populated, you can simply set the property as you are currently doing.

    One quick word of warning, though. Read this page: developer.m-files.com/.../

    Regards,

    Craig.
  • I'm quite close to get it to work. There is something wrong in the end of the code where it should put lookups to MLSU 1123

    My eyes cannot find anything wrong with it but it says "Cannot use parentheses when calling a sub". Maybe some of you with more experience on this can fix it?

    Error:
    DlgInternalEventHandlers.cpp, 529, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    InternalEventHandlers.cpp, 297, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    InternalEventHandlers.cpp, 498, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    RPCMethodCallWithRetry.h, 178, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    RPCMethodCallWithRetry.h, 178, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    RPCEventHandlersAdmin.cpp, 264, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    RPCEventHandlersAdmin.cpp, 105, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    RPCEventHandlersHelper.cpp, 225, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    RPCEventHandlersHelper.cpp, 879, VBScript-koodi tapahtumak?sittelij?lle 'Ty?maat multiselectiin' on virheellinen. (0x8004084B)
    RPCEventHandlersHelper.cpp, 879, Cannot use parentheses when calling a Sub (0x80040008)
    RPCEventHandlersHelper.cpp, 947, Cannot use parentheses when calling a Sub (0x80040008)
    VaultDBSessionEvents.cpp, 1295, Cannot use parentheses when calling a Sub (0x80040008)
    VaultDBSessionEvents.cpp, 1670, Cannot use parentheses when calling a Sub (0x80040008)
    VaultScriptSessionTemplates.cpp, 195, Cannot use parentheses when calling a Sub (0x80040008)
    CoActiveScriptSite.cpp, 896, Cannot use parentheses when calling a Sub (0x80040008)
    CoActiveScriptSite.cpp, 745, Cannot use parentheses when calling a Sub (0x80040008)
    BeforeCheckInChanges::Ty?maat multiselectiin, 40, Cannot use parentheses when calling a Sub (0x80040008)
    MErrorHelper.cpp, 2390, Cannot use parentheses when calling a Sub (0x80040008)
    (M-Files 19.1.7279.6)



    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
    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")

    '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

  • I'm quite close to get it to work. There is something wrong in the end of the code where it should put lookups to MLSU 1123

    My eyes cannot find anything wrong with it but it says "Cannot use parentheses when calling a sub". Maybe some of you with more experience on this can fix it?

    Thanks

    PJ

    Just remove the brackets from line 40 where you call oLookups. Add
    oLookups.Add -1, oLookup


  • I'm quite close to get it to work. There is something wrong in the end of the code where it should put lookups to MLSU 1123

    My eyes cannot find anything wrong with it but it says "Cannot use parentheses when calling a sub". Maybe some of you with more experience on this can fix it?

    Thanks

    PJ

    Just remove the brackets from line 40 where you call oLookups. Add
    oLookups.Add -1, oLookup

    [/quote]

    Thanks,

    Now it stops at Type mismatch :"oPropertyValue.TypedValue.SetValueToMultiSelectLookup oLookups" - third line from the bottom.
    ......




  • I'm quite close to get it to work. There is something wrong in the end of the code where it should put lookups to MLSU 1123

    My eyes cannot find anything wrong with it but it says "Cannot use parentheses when calling a sub". Maybe some of you with more experience on this can fix it?

    Thanks

    PJ
    [/quote]

    Just remove the brackets from line 40 where you call oLookups. Add
    oLookups.Add -1, oLookup

    [/quote]

    Thanks,

    Now it stops at Type mismatch :"oPropertyValue.TypedValue.SetValueToMultiSelectLookup oLookups" - third line from the bottom.
    ......
    [/quote]
    You're using the method correctly, so this tells me that the property you're trying to set it to, 1123, isn't defined as a multi-select lookup in your vault.
  • 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")




  • I'm quite close to get it to work. There is something wrong in the end of the code where it should put lookups to MLSU 1123

    My eyes cannot find anything wrong with it but it says "Cannot use parentheses when calling a sub". Maybe some of you with more experience on this can fix it?

    Thanks

    PJ
    [/quote]
    [/quote][/quote]

    Just remove the brackets from line 40 where you call oLookups. Add
    oLookups.Add -1, oLookup

    [/quote]

    Thanks,

    Now it stops at Type mismatch :"oPropertyValue.TypedValue.SetValueToMultiSelectLookup oLookups" - third line from the bottom.
    ......
    [/quote]
    You're using the method correctly, so this tells me that the property you're trying to set it to, 1123, isn't defined as a multi-select lookup in your vault.
    [/quote]

    1123 is multiselect, so it is not that. Maybe the oLookups is not correcly populated and therefore that is not the line where the actual error is.