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 - getting the wrong object from GetLatestObjectVersionAndProperties(oObjID, True)

Hi guys!

I want to create a VBscript on a workflow transition to allow or disallow an automatic workflow transition. I've been looking for resources in the forum and got some things done however for some time now I'm stuck. In the function I want to get an object by id and then read a property value from it. After I've done this for some objects I want to compare the values and if the two values are the same I want to allow the transition for the state transition script runs on.

The problem I have is that Set oResult = Vault.ObjectOperations.GetLatestObjectVersionAndProperties(oObjID, True) returns the wrong object and I'm not sure where I'm going wrong.

Am I giving the wrong ID to the oObjID wich leads to GetLatestObjectVersionAndProperties loading the wrong object? When i look at the IDs they are the correct Display IDs.

What could I do better?

Here is the complete script

' *******************************************************************************************
' Check if Antragsteller.Vorgesetzer is equal to Antragsteller.Bereich(e).Führendes GL-Mitglied
' Konfiguration über M-Files GUI nicht möglich, da ansonsten ein Loop gemeldet wird
' *******************************************************************************************

'GET The IDs of the properties and ObjectType
Dim PropIdFGLMitglied, PropIdVorgesetzter, PropIdAntragsteller, PropIdBereich, ObjTypeIDBereich, ObjTypeIDMitarbeiter, ObjIDAntragsteller, ObjIDFuhrendesGLMitglied, ObjIDVorgesetzer
PropIdFGLMitglied = GetPropID("prop.FhrendesGlMitglied")
PropIdVorgesetzter = GetPropID("prop.Vorgesetzter")
PropIdAntragsteller = GetPropID("prop.Antragstellerinnen")
PropIdBereich = GetPropID("prop.Bereiche")
ObjTypeIDBereich = GetObjectTypID("objecttype.Bereich")
ObjTypeIDMitarbeiter = GetObjectTypID("objecttype.Mitarbeiterin")

'GET ObjectID of the Mitarbeiter referenced in Antragsteller --> Try lower one if top is not working
ObjIDAntragsteller = PropertyValues.SearchForProperty(PropIdAntragsteller).TypedValue.GetValueAsLookups().Item(1).DisplayID


ObjIDVorgesetzer = GetReferencedObjectID(ObjTypeIDMitarbeiter, ObjIDAntragsteller, PropIdVorgesetzter)
ObjIDBereich = GetReferencedObjectID(ObjTypeIDBereich, ObjIDAntragsteller, PropIdBereich)
ObjIDFuhrendesGLMitglied = GetReferencedObjectID(ObjTypeIDMitarbeiter, ObjIDBereich, PropIdFGLMitglied)


If cInt(ObjIDFuhrendesGLMitglied) = cInt(ObjIDVorgesetzer) Then
    AllowStateTransition = True
End If





'######Common Functions#####
Public Function GetPropID( alias )
    GetPropID = Vault.PropertyDefOperations.GetPropertyDefIDByAlias( alias )
End Function

Public Function GetObjectTypID(alias)
    GetObjectTypID = Vault.ObjectTypeOperations.GetObjectTypeIDByAlias(alias)
End Function

'--------------------------------------------------------------------------------------------------------

Public Function GetReferencedObjectID(idObjectType, idObjectID, idPropertyID)
    ' Create a new ObjID object
    Dim oObjID : Set oObjID = CreateObject("MFilesAPI.ObjID")
	Call oObjID.SetIDs(idObjectType, CInt(idObjectID)) ' Sets the object type and ID

    
   

    ' Invoke the search operation
    Dim oResult
    Set oResult = Vault.ObjectOperations.GetLatestObjectVersionAndProperties(oObjID, True)

    Output =("ObjIDVorgesetzer: " + CStr(ObjIDVorgesetzer) + ", Type:  " + CStr(VarType(ObjIDVorgesetzer)) + " | ObjIDAntragsteller: " + CStr(ObjIDAntragsteller) + ", Type:  " + CStr(VarType(ObjIDAntragsteller)) + " | oResult: " + CStr(oResult.ToJSON()))


    ' Process the result
    If oResult Is Nothing Then
        Output = "Bases Object Not found" ' Or some other appropriate return value
    Else
           ' Assuming the property value is found and is a lookup
        GetReferencedObjectID = oResult.Properties.SearchForProperty(idPropertyID).TypedValue.GetValueAsLookups().Item(1).DisplayID
    End If
End Function

 

  • Where is this code running exactly?

    Edit: sorry, you said on a workflow state transition.

  • Is the issue that you're trying to use display IDs when you should be using the internal item ID?  If these are external object types or value lists then this is an important distinction.  Should line 16, for example, return the "Item" property rather than the "DisplayID"?  There's more about this here: Internal and External IDs (m-files.com)

  • Thank you  it was a problem of using the External ID instead of the Internal ID. It was not clear to me from the documentation i read that I need to use.item. Thanks to your help and some other small fixes I got it running like charm. 

    Here is the script if anyone else can use it also an image of the workflow I use it with. I use the script for the state transition to basically prevent that someone must check the same thing twice.

    ' *******************************************************************************************
    ' Check if Antragsteller.Vorgesetzer is equal to Antragsteller.Bereich(e).Führendes GL-Mitglied
    ' Konfiguration über M-Files GUI nicht möglich, da ansonsten ein Loop gemeldet wird
    ' *******************************************************************************************
    
    'GET The IDs of the properties and ObjectType
    Dim PropIdFGLMitglied : PropIdFGLMitglied = GetPropID("prop.FhrendesGlMitglied")
    Dim PropIdVorgesetzter : PropIdVorgesetzter = GetPropID("prop.Vorgesetzter")
    Dim PropIdAntragsteller : PropIdAntragsteller = GetPropID("prop.Antragstellerinnen")
    Dim PropIdBereich : PropIdBereich = GetPropID("prop.Bereiche")
    Dim PropIdBetragProPerson : PropIdBetragProPerson = GetPropID("prop.BetragpersonChf")
    Dim ObjTypeIDBereich : ObjTypeIDBereich = GetObjectTypID("objecttype.Bereich")
    Dim ObjTypeIDMitarbeiter : ObjTypeIDMitarbeiter = GetObjectTypID("objecttype.Mitarbeiterin")
    
    ' Get object IDs and values
    Dim ObjIDAntragsteller : ObjIDAntragsteller = PropertyValues.SearchForProperty(PropIdAntragsteller).TypedValue.GetValueAsLookups().Item(1).Item 'GET ObjectID of the Mitarbeiter referenced in Antragsteller --> With .Item you get the internal Object ID unlike with DisplayID for it to run properly you need the internal ID
    Dim ValueBetragProPerson : ValueBetragProPerson = PropertyValues.SearchForProperty(PropIdBetragProPerson).TypedValue.DisplayValue
    Const MaxBetrag = 5000 'The max monetary value which decides the case in the workflow
    
    Dim ObjIDVorgesetzer : ObjIDVorgesetzer = GetReferencedObjectID(ObjTypeIDMitarbeiter, ObjIDAntragsteller, PropIdVorgesetzter)
    Dim ObjIDBereich : ObjIDBereich = GetReferencedObjectID(ObjTypeIDMitarbeiter, ObjIDAntragsteller, PropIdBereich)
    Dim ObjIDFuhrendesGLMitglied : ObjIDFuhrendesGLMitglied = GetReferencedObjectID(ObjTypeIDBereich, ObjIDBereich, PropIdFGLMitglied)
    
    'TWO Versions of the If condition for the two different cases
    If cInt(ObjIDFuhrendesGLMitglied) = cInt(ObjIDVorgesetzer) AND cInt(ValueBetragProPerson) > MaxBetrag Then
    'If cInt(ObjIDFuhrendesGLMitglied) = cInt(ObjIDVorgesetzer) AND cInt(ValueBetragProPerson) <= MaxBetrag Then
        AllowStateTransition = True
    End If
    
    '######Common Functions#####
    Public Function GetPropID(alias)
        GetPropID = Vault.PropertyDefOperations.GetPropertyDefIDByAlias(alias)
    End Function
    
    
    Public Function GetObjectTypID(alias)
        GetObjectTypID = Vault.ObjectTypeOperations.GetObjectTypeIDByAlias(alias)
    End Function
    
    
    Public Function GetReferencedObjectID(idObjectType, idObjectID, idPropertyID)
        ' Create a new ObjID object
        Dim oObjID : Set oObjID = CreateObject("MFilesAPI.ObjID")
    	Call oObjID.SetIDs(idObjectType, CInt(idObjectID)) ' Sets the object type and ID
    
    
        ' Invoke the search operation
        Dim oResult
        Set oResult = Vault.ObjectOperations.GetLatestObjectVersionAndProperties(oObjID, True)
    
    
        ' Process the result
        If oResult Is Nothing Then
            Err.raise mfscriptcancel, "Bases Object Not found"
        Else
               ' Assuming the property value is found and is a lookup
            GetReferencedObjectID = oResult.Properties.SearchForProperty(idPropertyID).TypedValue.GetValueAsLookups().Item(1).Item
        End If
    End Function