The M-Files Community will be updated on Tuesday, April 2, 2024 at 10:00 AM EST / 2:00 PM GMT and the update is expected to last for several hours. The site will be unavailable during this time.

VBScript Event Handler to set PropertyDef of related Object

I'm currently working on an event handler, BeforeCheckInChangesFinalized, that will use a PropertyValue of the current object to look up an associated object, and then update a PropertyValue on the associated object. Our data model has a 'Member' object and 'Document' object where a Documents are children to a Member. This relationship is created with the 'Member' property which is a look up to our list of members and is present on all of our Document classes. Our Document Classes are all medical documents and one of our business units has requested that we display the Document Date (Essentially the effective date of the document) of business critical documents on an associated member's metadata card. For example if I have a document class 'LCED Initial' with a Document Date of 6/2/2023 and that is related to the member Murphy, Zachary the unit would like 6/2/2023 to populate into the 'LCED Date' field to be viewable on the Member's metadata card. I've used some previous posts on this forum as a jumping off point and I have a script that *seems* to run, but doesn't actually do anything:

Option Explicit

' get property values of the owner object
Dim oDocPropVals : Set oDocPropVals = Vault.ObjectPropertyOperations.GetProperties (ObjVer,False)

'
DIM oComments, szDocumentDate, oMember, iMember

' Get DisplayID of 'Member' and 'Document Date' fields of current Document																					  
iOwner = oPropVals.SearchForPropertyByAlias(vault,"M-Files.Property.Member",True).TypedValue.GetValueAsLookups.Item(0).Item
szDocumentDate = oPropVals.SearchForPropertyByAlias(vault,"MF.PD.DocumentDate",True).TypedValue.GetValueAsUnLocalizedText

' Get most recent version of the iMember object
Set oMember = Vault.ObjectOperations.GetLatestObjVer(iMember, True, false)

'Checkout the object you want to update
Dim checkedOutObjectVersion : Set checkedOutObjectVersion = vault.ObjectOperations.CheckOut(oMember.ObjVer.objID)

'update properties of checked out object
Dim oMemberPropVals : Set oMemberPropVals = Vault.ObjectPropertyOperations.GetProperties(checkedOutObjectVersion, False)

' create Property Value object, assign szDocumentDate to the 'Value' property and then update the checkedOutObjectVersion
Dim oPropValLCEDNotes : Set oPropValLCEDNotes = CreateObject("MFilesAPI.PropertyValue")
oPropValLCEDNotes.PropertyDef = oMemberPropVals.SearchForPropertyByAlias(vault,"MF.PD.LcedNotes",True).PropertyDef
oPropValLCEDNotes.Value.SetValue MFDatatypeText, szDocumentDate
Vault.ObjectPropertyOperations.SetProperty checkedOutObjectVersion, oPropValLCEDNotes 

'check in the checked out object
vault.ObjectOperations.CheckIn checkedOutObjectVersion.ObjVer
 

Now, I'm admittedly a self taught Python Developer and I'm relatively new to M-Files (I've been working with M-Files for almost a year now) so please excuse my poor coding skills and notion as I'm new to VBS. Also there might be a better way to do this so if that's the case I'm open to suggestions. However, as a caveat to this the Document Classes aren't unique to an individual member. So a member might have more than one 'LCED Initial' document. My plan is to add some additional checks to always use the greatest date value for the associated field, but I'm struggling to get this initial code running. Any input is greatly appreciated! 

Parents Reply
  • You will have to add the code also for the case a new document is created.

    I would use 

    AfterCreateNewObjectFinalize 

    AfterCheckInChanges 

    also make sure the code is executed just for your LCED Initial class:

    Option Explicit
    
    Dim iClass: iclass = PropertyValues.SearchForProperty(100).TypedValue.GetValueAsLookup.Item
    
    on error resume next	
    if iclass = 114 then  '' change it to Id of LCED Initial class you can get it also via class alias
    on error goto 0  
       ''' your code
       
    end if

Children