2 If VBScript

I need some help with my script.

My script should be checking 2 properties

If PropVal is not "Self" the other value (Contact) should be Null or empty.

Option Explicit

Dim szPropertyName, szPropVal
szPropertyName = PropertyDef.Name
szPropVal = PropertyValue.GetValueAsUnlocalizedText

If not szPropVal = "Self" then
Dim szContactVal : szContactVal = PropertyValues.SearchForProperty(1257).TypedValue.DisplayValue

If szContactVal <> "Self" Then
Err.Raise MFScriptCancel, "ContactVal should be empty."

End if
End if

Thank you.

  • Hi eihr0921,
    I have a question so that we can give you a proper solution.

    What data type is the property definition that holds the value of "Self"?

    If this is a list, you could use the metadata card configuration to hide the Contact property definition is the "Self" item is selected; assuming you are on a current version of M-Files. This would remove the need for scripting. 

    If it is text-based, then you would place a validation script in the Contact property definition; which I can help you create the script.


  • Actually i can do it on the configuration, but unfortunately this properties are implemented long enough that might affect old documents. I mistyped my validation script, but you're right it will be text-based or string. Here is the example below.

    If the user selected the a value that is not "Self" on Property1, then Property2 should be empty. 

    But this only applies if Property1 has a value. 

    Option Explicit

    Dim szPropertyName, szPropertyval1
    szPropertyName = PropertyDef.Name
    szPropertyval1 = PropertyValue.GetValueAsUnlocalizedText

    If not szPropertyval1  = "Self" then
    Dim szPropertyval2 : szPropertyval2 = PropertyValues.SearchForProperty(1257).TypedValue.DisplayValue

    If isNull (szPropertyval2) = False Then
    Err.Raise MFScriptCancel, "szPropertyval2 should be empty."

    End if
    End if

    Thank you in advance.

  • Here is the solution I came up with according to your requirements. Place this script in the Validation section of the Contact property definition.

    Option Explicit
    Const pd_Self = 1150 'ID of property def to evaluate the text value. Locate this from your vault

    ' PropertyValues is not exposed within validation scripts so we create it
    Dim oPropVals: Set oPropVals = CreateObject("MFilesApi.PropertyValues")
    Set oPropVals = Vault.ObjectPropertyOperations.GetProperties(ObjVer)

    'The property definition value to inspect.
    Dim selfvalue : selfvalue = oPropVals.SearchForProperty(pd_Self).TypedValue.Value

    'PropertyValue represents the property definition where the validation script is running. 
    'Check if there is a value and compare if the other contains "Self"
    If Not PropertyValue.TypedValue.IsNull() AND selfvalue = "Self" Then
    Err.Raise MFScriptCancel, "The Contact property should be empty"
    End IF

    A few notes about this solution to be aware of:

    1. Hardcoding text values leave a bit of room for error. If the user types "self" or " self" or "self " the validation will not occur. To secure this for future logical errors, I would add a boolean property definition and validate according to that value. 

    2. I would also suggest to use Aliases when scripting. This gives your scripts scalability if you need to use it within another vault, such as from Dev to Prod. 

    Let me know if this script works for you by marking it as an answer. 

    Happy Scripting,