How to get property value from previous version?

Hi,

I’m trying to make a script for allowing state transition if property auditors(1237) have changed. I ‘m stuck with this since it seems like the script doesn't get the value for property 1237 from the previous version.

I know that the value hasn’t changed between the versions so I added lines 21 & 31 shell.logevent 4 and it looks in the event log that there is just empty value whereas there should be the same value as in current version (I have at least triple checked that this hasn’t changed between the versions). It feels like the script is correctly accessing the previous version but is having trouble retrieving the property value.

Any ideas how to change the script? Is there maybe something wrong with the GetProperty on line 20?

This is what I get to event log from line 9:


 This is what I get to event log from lines 21 & 31

 

This is the accessed previous version (line 27):

And this is how the property looks like in that version in UI:

This is my script: 

Option Explicit

'For Event viewer events.
Dim shell : Set shell = CreateObject("WScript.Shell")

' Get the current auditor(s).
Dim szAuditors
szAuditors = PropertyValues.SearchForProperty(1237).TypedValue.DisplayValue
shell.LogEvent 4, "Current Auditors: " & szAuditors

' Get the previous version Auditors value 
Dim sObjVersions
Set sObjVersions = Vault.ObjectOperations.GetHistory(ObjVer.ObjID)

Dim szAuditorsOfPreviousVersion
If sObjVersions.Count > 1 Then
    Dim prevObjVer
    Set prevObjVer = sObjVersions.Item(sObjVersions.Count - 2).ObjVer
    On Error Resume Next
    szAuditorsOfPreviousVersion = Vault.ObjectPropertyOperations.GetProperty(prevObjVer, 1237).TypedValue.DisplayValue
	shell.LogEvent 4, "Previous Auditors: " & szAuditorsOfPreviousVersion
    If Err.Number <> 0 Then
        shell.LogEvent 1, "Error retrieving property from previous version: " & Err.Description
        Err.Clear
    End If
    On Error GoTo 0
    shell.LogEvent 4, "Previous Version Number: " & (sObjVersions.Count - 2)
Else
    szAuditorsOfPreviousVersion = ""
End If
shell.LogEvent 4, "Previous Auditors: " & szAuditorsOfPreviousVersion

' Logic test if the fields are not the same then move to next in workflow.
If szAuditors <> szAuditorsOfPreviousVersion Then
    AllowStateTransition = True
End If

  • What happens if you remove the "On Error Resume Next" statement?

  • Thanks, Graig! Your advice put me on the right track. I removed the "On Error Resume Next" and added logging with shell.LogEvent 4, "Previous Version ObjVer ID: " & prevObjVer.ID & ", Version: " & prevObjVer.Version. This revealed that prevObjVer.Version was 3, whereas it should have been 98 or something similar. Previously, I logged just (sObjVersions.Count - 2), which referred to the correct second-to-last version but didn’t highlight the actual issue with prevObjVer.

    I was using the GetHistory method, which retrieved versions in an order where the latest entry was listed first. I switched to GetHistoryEx, and I believe this has finally resolved the issue.

    In case anyone else is looking for something similar here is my version that I think works now:

    Option Explicit
    
    ' For Event viewer events.
    Dim shell : Set shell = CreateObject("WScript.Shell")
    
    ' Get the current auditor(s).
    Dim szAuditors
    szAuditors = PropertyValues.SearchForProperty(1237).TypedValue.DisplayValue
    shell.LogEvent 4, "Current Auditors: " & szAuditors
    
    ' Get the previous version Auditors value 
    Dim sObjVersions
    Set sObjVersions = Vault.ObjectOperations.GetHistoryEx(ObjVer.ObjID, MFHistoryRetrievalAscending)
    
    Dim szAuditorsOfPreviousVersion
    If sObjVersions.Count > 1 Then
        Dim prevObjVer
        Set prevObjVer = sObjVersions.Item(sObjVersions.Count - 2).ObjVer
        shell.LogEvent 4, "Previous Version ObjVer ID: " & prevObjVer.ID & ", Version: " & prevObjVer.Version
       
        ' Attempt to retrieve the property value from the previous version
        Dim prevPropertyValue
        Set prevPropertyValue = Vault.ObjectPropertyOperations.GetProperty(prevObjVer, 1237)
        
        If Not prevPropertyValue Is Nothing Then
            szAuditorsOfPreviousVersion = prevPropertyValue.TypedValue.DisplayValue
            shell.LogEvent 4, "Previous Auditors: " & szAuditorsOfPreviousVersion
        Else
            shell.LogEvent 1, "Previous property value is Nothing."
            szAuditorsOfPreviousVersion = ""
        End If
    Else
        szAuditorsOfPreviousVersion = ""
    End If
    shell.LogEvent 4, "Previous Auditors: " & szAuditorsOfPreviousVersion
    
    ' Logic test if the fields are not the same then move to next in workflow.
    If szAuditors <> szAuditorsOfPreviousVersion Then
        AllowStateTransition = True
    End If
    

  • Awesome to hear you got it working!

    A couple of general points:

    Use "on error resume next" sparingly, if ever. It constantly hides issues that turn out to be the core problem.

    Microsoft is on a depreciation path for VBScript. This will affect all applications using it, not just M-Files. Nothing is changing right now, but it will in a few years. All new vault development should focus on the Vault Application Framework to ensure that you're future-proofing your implementation.