BeforeCreateNewObjectFinalize and External Objects

We have an event handler script that explicitly excludes an externally maintained Project object:

' Get the user name
Dim userAccount
Set userAccount = Vault.UserOperations.GetUserAccount(CurrentUserID)
Dim vloginName
vloginName = userAccount.LoginName

'Get instance of Property values
Dim oPropVals: Set oPropVals = CreateObject("MFilesApi.PropertyValues")
Set oPropVals = Vault.ObjectPropertyOperations.GetProperties(ObjVer)

currentTitle = oPropVals.SearchForProperty(MFBuiltInPropertyDefNameOrTitle).TypedValue.Value

currentClass = oPropVals.SearchForProperty(MFBuiltInPropertyDefClass).TypedValue.DisplayValue


If ( currentClass <> "Project") Then
If ( ( (InStr(1, currentTitle, "!") > 0) _
or (InStr(1, currentTitle, "~") > 0) _
or (InStr(1, currentTitle, "@") > 0) _
or (InStr(1, currentTitle, "#") > 0) _
or (InStr(1, currentTitle, "%") > 0) _
or (InStr(1, currentTitle, "^") > 0) _
or (InStr(1, currentTitle, "&") > 0) _
or (InStr(1, currentTitle, "*") > 0) _
or (InStr(1, currentTitle, "?") > 0) ) _
and (InStr(1, vloginName, "mf_service") = 0) ) _
Then
Err.Raise MFScriptCancel, "Please remove the special characters in " + currentTitle
End If
End If

Yet, full refreshes of the Project object fail to complete.  Watching the refreshing status, I can see that objects are not updated or inserted.

  • When I deactivate the event handler, the refreshes work.

  • And instead of checking for the presence of special characters and reacting with an error, why don't you just remove them: 

    Replace(currentTitle,"!","")

  • I can try that and see if Refreshes work again.  Thanks.

  • Alas, my Project object refresh still failed to insert new objects and update existing ones when the event handler was active.  Only when I deactivated it did it go through.  Here is the code:

    ' Get the user name
    Dim userAccount
    Set userAccount = Vault.UserOperations.GetUserAccount(CurrentUserID)
    Dim vloginName
    vloginName = userAccount.LoginName

    'Get instance of Property values
    Dim oPropVals: Set oPropVals = CreateObject("MFilesApi.PropertyValues")
    Set oPropVals = Vault.ObjectPropertyOperations.GetProperties(ObjVer)

    Dim currentProperty
    Set currentProperty = oPropVals.SearchForProperty(MFBuiltInPropertyDefNameOrTitle)
    currentTitle = currentProperty.TypedValue.Value

    currentClass = oPropVals.SearchForProperty(MFBuiltInPropertyDefClass).TypedValue.DisplayValue


    If ( currentClass <> "Project") Then

    If ( ( (InStr(1, currentTitle, "!") > 0) _

    or (InStr(1, currentTitle, "~") > 0) _
    or (InStr(1, currentTitle, "$") > 0) _
    or (InStr(1, currentTitle, "@") > 0) _
    or (InStr(1, currentTitle, "#") > 0) _
    or (InStr(1, currentTitle, "%") > 0) _
    or (InStr(1, currentTitle, "^") > 0) _
    or (InStr(1, currentTitle, "&") > 0) _
    or (InStr(1, currentTitle, "*") > 0) _
    or (InStr(1, currentTitle, "?") > 0) ) _
    and (InStr(1, vloginName, "mf_service") = 0) ) _
    Then
    currentTitle = Replace ( _
    Replace ( _
    Replace ( _
    Replace ( _
    Replace ( _
    Replace ( _
    Replace ( _
    Replace ( _
    Replace ( _
    Replace(currentTitle,"`","_") , _
    "~","_") , _
    "@","_") , _
    "#","_") , _
    "$","_" ), _
    "%","_") , _
    "^","_"), _
    "&","_") , _
    "*","_") , _
    "!","_")


    currentProperty.TypedValue.SetValue 1, currentTitle
    Vault.ObjectPropertyOperations.SetProperty ObjVer, currentProperty
    ' Err.Raise MFScriptCancel, "Replacing special character with " + currentTitle


    End If

    End If

  • I would shorten it to

    'Get instance of Property values
    Dim oPropVals: Set oPropVals = CreateObject("MFilesApi.PropertyValues")
    Set oPropVals = Vault.ObjectPropertyOperations.GetProperties(ObjVer)
    
    Dim currentProperty
    Set currentProperty = oPropVals.SearchForProperty(MFBuiltInPropertyDefNameOrTitle)
    currentTitle = currentProperty.TypedValue.Value
    
    currentClass = oPropVals.SearchForProperty(MFBuiltInPropertyDefClass).TypedValue.DisplayValue
    
    If ( currentClass <> "Project") Then
    
    	currentTitle = Replace(currentTitle,"`","_")
    	currentTitle = Replace(currentTitle,"~","_")
    	currentTitle = Replace(currentTitle,"@","_")
    	currentTitle = Replace(currentTitle,"#","_")
    	currentTitle = Replace(currentTitle,"$","_")
    	currentTitle = Replace(currentTitle,"%","_")
    	currentTitle = Replace(currentTitle,"^","_")
    	currentTitle = Replace(currentTitle,"&","_")
    	currentTitle = Replace(currentTitle,"*","_")
    	currentTitle = Replace(currentTitle,"!","_")
    
    	currentProperty.TypedValue.SetValue 1, currentTitle
    	Vault.ObjectPropertyOperations.SetProperty ObjVer, currentProperty
    	' Err.Raise MFScriptCancel, "Replacing special character with " + currentTitle
    
    End If

    taking the current user is redundant because it is performed by the system here

  • As a general comment, you should avoid using display names in your logic as the script will break if someone goes and renames the class. The best practice is to find the structure elements by their aliases, which should not change once set.

  • If your event handler changes a property that is used in synchronization with the external source, then you need to make sure the synchronization includes correct Update and Insert statements including the properties being changed and, of course, that the external database offers permission to change those properties to the user account used for the synchronization.

    Have you checked the event log to see which errors are generated?

  • Thanks for your advice.  My script explicitly excludes the Project class from this validation of Name or Title.  Maybe our use of Project as a property of most of these Documents affects the Project refreshes.  I don't know. 

    The event log did not show anything about the refresh.

  • Thanks.  Good to know that it's the system itself that is finalizing the creation of the document.

  • Once I replaced my code with this code, the Project object refreshes worked again.  Not sure why yet.

    Thanks for everyone's input.Grinning