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

How to find properties off Contact object

Hi, I am trying to write VBScript to use as pre-condition on a workflow, to validate that a user has permission to perform a task

The test is: Current user is listed on the Project object as a Project User
The Project has a list of 'Project Users', which are Contact objects
The Contact objects have a property 'M-Files User' which links to their M-Files User Account   (you may guess where the problem now lies)

My code so far will find the Project object, then loop through all the Project Users' Contact objects, and compare that to the CurrentUserID

' Verify the Created by user is a valid role on the Project selected

Dim pProjectObj : pProjectObj = Vault.ObjectTypeOperations.GetObjectTypeIDByAlias("H.Object.Project")
Dim pProject : pProject = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("H.Property.Project")
Dim pSupProjUser : pSupProjUser = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("H.Property.ProjectUser")

' Get the selected Project 
Set buCollection = PropertyValues.SearchForProperty(pProject).TypedValue.GetValueAsLookups
Set bu = buCollection( 1 )
Set objID = CreateObject("MFilesAPI.ObjID")
objID.ID = bu.Item
objID.Type = pProjectObj
Set objectVersion = Vault.ObjectOperations.GetLatestObjVer( objID, false, false )

	' Check the Project actually has any listed Project Users
	Set objProps = Vault.ObjectOperations.GetObjectVersionAndProperties(objectVersion, true).Properties
	If objProps.SearchForProperty(pSupProjUser).TypedValue.IsNULL() then
	    Err.Raise MFScriptCancel, "The selected Project has no listed Project Users"
	End If

' Get all the Project Users from the selected Project
Dim pSPULookups : Set pSPULookups = objProps.SearchForProperty(pSupProjUser).TypedValue.GetValueAsLookups

' Compare the Current User -vs- the Project Users from the selected Project
Dim lookup : Set lookup = CreateObject("MFilesAPI.Lookup")
Dim IsValidCreate : IsValidCreate = 0

For Each lookup In pSPULookups
    If lookup.Item = CurrentUserID Then IsValidCreate = 1
Next

' Display error if the comparison finds the current user isn't a Project User on the selected Project
If IsValidCreate = 0 Then
	Err.Raise MFScriptCancel, "You are not listed as a Project User on the selected Project."
End If

However, the Contact IDs don't relate to the M-Files User Account IDs
So I need to read the M-Files User property from the Project Users, and then compare that to CurrentUserID

I think the middle section should be something like this, but it's not working and I've spent too long starting at it and can't think straight now, please help Slight smile

For Each lookup In pSPULookups

		Dim pMFilesUser : pMFilesUser = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("H.Property.MFilesUser")

	    Dim oMFilesUser : Set oMFilesUser = CreateObject("MFilesAPI.ObjVer")
	    oMFilesUser.Type = lookup.Type
	    oMFilesUser.ID = lookup.Item

		Dim iMFilesUser : iMFilesUser = Vault.ObjectPropertyOperations.GetProperty(oMFilesUser, pMFilesUser).TypedValue.DisplayValue


    If iMFilesUser = CurrentUserID Then IsValidCreate = 1

  • Hi Spooky,

    If I understood correctly in your project you have something similar and in Workflow you move the project.

    Then there must be something like that in the precondition code:

    ' Get instance from Properties of current object (Project)
    Dim oPropVals: Set oPropVals = CreateObject("MFilesApi.PropertyValues")
    Set oPropVals = Vault.ObjectPropertyOperations.GetProperties(ObjVer)
    
    'It is highly recommended to use the Aliases generated by M-Files and to follow their naming convention avoid confusion
    Dim iOTContact: iOTContact = Vault.ObjectTypeOperations.GetObjectTypeIDByAlias("OT.Contact")
    Dim iCLContact: iCLContact = Vault.ClassOperations.GetObjectClassIDbyAlias("CL.Contact")
    
    Dim iPDMfilesUser: iPDMfilesUser = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("PD.MfilesUser") ' Refers real users (Build-in object Users)
    Dim iPDProjectUsers: iPDProjectUsers = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("PD.ProjectUsers") ' Refers pseudo users (object Contacts)
    
    ' Get the Current Project Project Users Collection
    Set oProjectUsersCollection = oPropVals.SearchForProperty(iPDProjectUsers).TypedValue.GetValueAsLookups
    Set oProjectUser = CreateObject("MFilesAPI.Lookup")
    
    boolUserFound = False
    
    For Each oProjectUser in oProjectUsersCollection
    	Set oContactObjVer = CreateObject("MFilesAPI.ObjVer")
    	oContactObjVer.SetIDs iOTContact, oProjectUser.Item, -1
    	
    	Set oObjectInfo = CreateObject("MFilesAPI.ObjectVersion")
    	Set oObjectInfo = Vault.ObjectOperations.GetObjectInfo(oContactObjVer, True, False)
    	
    	' Get instance from Properties of Contact Object
    	Set oContactProperties = CreateObject("MFilesAPI.PropertyValues")
    	Set oContactProperties = Vault.ObjectPropertyOperations.GetProperties(oObjectInfo.ObjVer)
    	
    	'Get choosed ID from single select Lookup
    	intiPDMfilesUser = oContactProperties.SearchForProperty(iPDMfilesUser).TypedValue.GetValueAsLookup.Item
    	If intiPDMfilesUser = CurrentUserID Then
    		boolUserFound = True
    	End If
    Next
    
    If Not boolUserFound Then
    	Err.Raise MFScriptCancel, "You are not listed as a Project User on the selected Project."
    End If	

  • I think I had an idea of what I wanted to do, along the lines of what you provided about creating an ObjVer for the contact record and then pulling its properties. But was just too tired to get there

    This is what I ended up using, it's a merge of your idea into my code, so uses the alias style you didn't like and some of the other stuff Slight smile Thanks again

    ' Workflow "Set HTA" > Workflow State "Open" > Preconditions 
    
    ' Define Aliases
    Dim pProjectObj : pProjectObj = Vault.ObjectTypeOperations.GetObjectTypeIDByAlias("H.Object.Project")
    Dim iOTContact: iOTContact = Vault.ObjectTypeOperations.GetObjectTypeIDByAlias("H.Object.Contact")
    Dim iCLContact: iCLContact = Vault.ClassOperations.GetObjectClassIDbyAlias("H.Class.Contact")
    Dim pProject : pProject = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("H.Property.Project")
    Dim pProjUser : pProjUser = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("H.Property.ProjectUser")
    Dim pMFilesUser : pMFilesUser = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("H.Property.MFilesUser")
    
    Dim IsValidCreate : IsValidCreate = 0
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Verify the Created by user is a valid role on the Project selected
    
    ' Get the selected Project 
    Set buCollection = PropertyValues.SearchForProperty(pProject).TypedValue.GetValueAsLookups
    Set bu = buCollection( 1 )
    Set objID = CreateObject("MFilesAPI.ObjID")
    objID.ID = bu.Item
    objID.Type = pProjectObj
    Set objectVersion = Vault.ObjectOperations.GetLatestObjVer( objID, false, false )
    
    	' Check the Project actually has any listed Project Users
    	Set objProps = Vault.ObjectOperations.GetObjectVersionAndProperties(objectVersion, true).Properties
    	If objProps.SearchForProperty(pProjUser).TypedValue.IsNULL() then
    	    Err.Raise MFScriptCancel, "The selected Project has no listed Project Users"
    	End If
    
    ' Get all the Project Users from the selected Project
    Dim pPULookups : Set pPULookups = objProps.SearchForProperty(pProjUser).TypedValue.GetValueAsLookups
    
    ' Compare the Current User -vs- the Project Users from the selected Project
    Dim lookup : Set lookup = CreateObject("MFilesAPI.Lookup")
    
    For Each lookup in pPULookups
    	Set oContactObjVer = CreateObject("MFilesAPI.ObjVer")
    	oContactObjVer.SetIDs iOTContact, lookup.Item, -1
    	
    	Set oObjectInfo = CreateObject("MFilesAPI.ObjectVersion")
    	Set oObjectInfo = Vault.ObjectOperations.GetObjectInfo(oContactObjVer, True, False)
    	
    	' Get instance from Properties of Contact Object
    	Set oContactProperties = CreateObject("MFilesAPI.PropertyValues")
    	Set oContactProperties = Vault.ObjectPropertyOperations.GetProperties(oObjectInfo.ObjVer)
    	
    	'Get ID from single select Lookup
    	intiPDMfilesUser = oContactProperties.SearchForProperty(pMFilesUser).TypedValue.GetValueAsLookup.Item
    	If intiPDMfilesUser = CurrentUserID Then IsValidCreate = 1
    Next
    
    ' Display error if the comparison finds the current user isn't a Project User on the selected Project
    If IsValidCreate = 0 Then
    	Err.Raise MFScriptCancel, "You are not listed as a Project User on the selected Project."
    End If

  • Why not change the permissions for the transition using the user from metadata functionality on that specific transition?

    This way the option to move it to the next state will be grayed out.

  • Ahhh... ah yes

    I do need to also validate that user is in correct user groups as well, but can also do that at the same time on transition...

    The real answer is, I started off by testing if the selected Approver for the workflow was valid based on metadata and user groups, which does need done in script.
    And then tried to extend the same thing for the CurrentUser, and carried on doing it in script.

    You're right though, I should just do it on the Transition. Instead of spending 6+ hours trying to get it done in script

    Thanks both