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

VBScript alteration conditional on Class

I'm new to the VB Scripting side of MFiles, please be kind.

We inherited a chunk of code from our VAR that sets field "Due Date" to +60 days of field "Review Date" in any of the Classes that "Due Date" and "Review Date" are in.  This works fine.

Original Code

Option Explicit
'Add 60 days to this property from property specified
'**************************************
'Variables to change
'**************************************
Dim pdDateReviewStartedID: pdDateReviewStartedID = 1178
Dim zDaysToAdd : zDaysToAdd = 60
'**************************************
''Do Not alter any code below.
'**************************************
Dim oPropertyValues: Set oPropertyValues = Vault.ObjectPropertyOperations.GetProperties(ObjVer)
Dim zIndex: zIndex = oPropertyValues.IndexOf(pdDateReviewStartedID)
'Double Check property exists before trying to run calc
If zIndex > -1 Then

Dim pdDateReviewStartedVal: pdDateReviewStartedVal = oPropertyValues.SearchForProperty(pdDateReviewStartedID).TypedValue.DisplayValue
If pdDateReviewStartedVal <> "" Then
Dim zNewDate: zNewDate = DateAdd("d", zDaysToAdd, pdDateReviewStartedVal)
Output = zNewDate
End IF
End If

However, we're wanting a small change.  The department that controls one of the Classes now wants "Due Date" set to +90 days of "Review Date", while the remaining Classes want the DueDate to remain +60 days.  In my changes, I'm attempting to check what the Class is, and if the Class is equal to 23 (aka the ClassID of the class that I want the changes) set the zDaysToAdd to 90.

Option Explicit
'Add 60 days to this property from property specified
'**************************************
'Variables to change
'**************************************
Dim pdDateReviewStartedID: pdDateReviewStartedID = 1178
Dim zDaysToAdd : zDaysToAdd = 60
Dim ClassIndex : ClassIndex = 100
'**************************************
''Do Not alter any code below.
'**************************************
Dim oPropertyValues: Set oPropertyValues = Vault.ObjectPropertyOperations.GetProperties(ObjVer)
Dim zIndex: zIndex = oPropertyValues.IndexOf(pdDateReviewStartedID)
Dim vClass : vClass = oPropertyValues.IndexOf(ClassIndex)

If vClass = 23 Then
zDaysToAdd = 90
End If

'Double Check property exists before trying to run calc
If zIndex > -1 Then
Dim pdDateReviewStartedVal: pdDateReviewStartedVal = oPropertyValues.SearchForProperty(pdDateReviewStartedID).TypedValue.DisplayValue
If pdDateReviewStartedVal <> "" Then
Dim zNewDate: zNewDate = DateAdd("d", zDaysToAdd, pdDateReviewStartedVal)
Output = zNewDate
End IF
End If

Text in Red is what I have attempted to change.  Whenever I create a new document in ClassID = 23, I still get a DueDate of +60 days instead of +90 Days like we want.  If I remove the "IF" statement, the DueDate is the correct +90 days, so I'm pretty sure the problem lies in the IF statement or the Dim statement.

Help?

  • Hi you are on the right track but the following line does not actually return the value of the class ID though:

    Dim vClass : vClass = oPropertyValues.IndexOf(ClassIndex)

    It returns the index/position in the PropertyValues array that the property exists. If you want to get the id of the class you can try doing something like this:

    Dim vClass : vClass = oPropertyValues.SearchForProperty(ClassIndex).Value.GetLookupID()

    As a side note if you want to troubleshoot the M-Files script it is sometimes helpful to make the code throw an error on purpose to return some useful information so you can see if you have the expected values. For example , adding a line like this in your code temporarily: 

    Err.Raise MFScriptCancel, "the id in my vClass variable is: "& vClass 

  • That got it, thanks for the help!