Change a value in an Employee object in VBS

I know this should be a very simple operation but I cannot figure it out. I am afraid I cannot grasp the info in the API documentation although trying hard. And the samples I find do not help me much. Possibly someone has a script and could share.

So, I have a workflow where you apply for leave. There I have a multi select property "Replacement Employee" selected from the employee object. So this property is filled by the user well before leaving. The workflow remains active until the employee returns from leave and ends the workflow. A day before the departure date a notification is automatically sent to the user to remind him/her to manually select his/her Replacement in the Employee object. Likewise a new notification to remove it after return. This is often forgotten of course. The Replacement Employee is then used in all other workflows to assign also the replacement during the leave period. Built-in substitute user has its limitations as permissions do not follow.

So, if instead of the notifications, if I could set that property value automatically in the employee object things would be much easier.

I understand I need to search for the employee object. Then set the property value in that Employee object property.

Any help highly appreciated.

  • So I am using the code from a post by Tore Furuhatt

    Trying to add a new value to a list and also get the value to a property

    with some comments from Craig. Thank you. This code will add a lookup item to a value list. Seems Tore wanted to do exactly what I want but Craig's comments were never answered and no new code posted.

    The code below with my questions/comments. I did not change any variable names, just my id.s.

    Appreciate any help

    Option Explicit
    
    'For debugging
    Dim fso, objOutFile
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set objOutFile = fso.CreateTextFile("c:\Temp\AddReplacement.txt",True)
    
    
    
    CONST PROP_ID_DOC_COMP = 1322 ' Field to update (MultiSelect Lookup)
    CONST iValuelistID = 156 ' List to find value
    Dim iCompID : iCompID = PropertyValues.SearchForProperty(1310).TypedValue.DisplayValue 'Field to find value for update (MultiSelectLookup)
    
    AddUpdateProperty iCompID, PROP_ID_DOC_COMP, iValuelistID
    
    Function AddUpdateProperty(sValue, PROP_ID, ListID)
    
      Dim oProperty : Set oProperty = CreateObject("MFilesAPI.PropertyValue")
      oProperty.PropertyDef = PROP_ID
      Dim iDataType : iDataType = Vault.PropertyDefOperations.GetPropertyDef( oProperty.PropertyDef ).DataType
      Dim oOneValueList : Set oOneValueList = vault.ValueListItemOperations.GetValueListItems(ListID)
      Dim oLookups : Set oLookups = CreateObject("MFilesAPI.Lookups")
    
      'I ONLY HAVE ONE SUBVALUE, THESE LINES CAN BE MODIFIED BUT WORKS NOW
      Dim sSubValues : sSubValues = Split(sValue, ",")
      
      Dim sSubValue, i
      For i=0 to ubound(sSubValues)
       sSubValue=Trim(sSubValues(i))
    
       ' Create the lookup
       Dim oLookup : Set oLookup = CreateObject("MFilesAPI.Lookup")
    
    
       ' Get the ID (it'll add it if it doesn't exist)
       oLookup.Item = GetValueListItemByName(ListID, oOneValueList, sSubValue)
       
       ' Add it to the lookups
       oLookups.Add -1, oLookup
    
    
     Next
    
    
    ' Set the property typed value to the lookups
      oProperty.TypedValue.SetValueToMultiSelectLookup oLookups
    
    
    
    ' Here the property 1322 is updated with the propertyvalue from 1310 (which is another multiselect lookup)in the CURRENT WORKFLOW CLASS
      ' Update the object's property
      Vault.ObjectPropertyOperations.SetProperty ObjVer, oProperty
    
    
    
    ' But I want it to update the same property 1322 in the below Employee object 156-1108 (Later I will use the Item.Id = ext. ID)
    
    Dim TargetObjID: Set TargetObjID = CreateObject("MFilesAPI.ObjID")
    TargetObjID.SetIDs 156, 1108 'Item.id 1108 Employee Object = 156, Employee 1108 external ID for me (Display ID is 8708)
    
    Dim oTargetProps
    set oTargetProps = Vault.ObjectOperations.GetLatestObjectVersionAndProperties(TargetObjID,true)
    
    'here somehow clone or set the first lookup value (or all lookup values, I have only one anyway) in 1322 in the 156-1108 object
    'I guess I have the value in oProperty
    
    
    
    End Function
    
    Function GetValueListItemByName(iValueListId, ValueListItems, sNameToFind)
    
     Dim item
     For Each item in ValueListItems
      ' Does this need to deal with string casing? YESSSS
      If item.Name = sNameToFind Then
       GetValueListItemByName = item.id
       objOutFile.WriteLine item.id
       Exit Function
      End If
     Next
    
    End Function

  • You should be able to set property 1322 in the employee object the same way you set it in your other object. 

    Try this:

    Vault.ObjectPropertyOperations.SetProperty oTargetProps.ObjVer, oProperty

  • Thanks a million for pointing me in the right direction. It works!!! Very happy about this. Below the code mainly from Tore Furuhatt. Still some tidying up but it works. Maybe someone could profit.

    Found some comments about checking out. Seems you need to read the properties in order to be able to check out. Then read the properties again as the checkout creates a new version. Not really evident at first glance.

    Thanks again for support.

    Option Explicit
    
    CONST PROP_ID_DOC_COMP = 1322 ' Field to update Replacement employee (MultiSelect Lookup)
    CONST iValuelistID = 156 ' List to find value = Employee Object
    Dim iCompID : iCompID = PropertyValues.SearchForProperty(1310).TypedValue.DisplayValue 'Requester. Field to find value for update (Lookup)
    
    
    'For debugging
    Dim fso, objOutFile
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set objOutFile = fso.CreateTextFile("c:\Temp\AddReplacement.txt",True)
    
    AddUpdateProperty iCompID, PROP_ID_DOC_COMP, iValuelistID
    
    Function AddUpdateProperty(sValue, PROP_ID, ListID)
    
      Dim oProperty : Set oProperty = CreateObject("MFilesAPI.PropertyValue")
      oProperty.PropertyDef = PROP_ID
      Dim iDataType : iDataType = Vault.PropertyDefOperations.GetPropertyDef( oProperty.PropertyDef ).DataType
      Dim oOneValueList : Set oOneValueList = vault.ValueListItemOperations.GetValueListItems(ListID)
      Dim oLookups : Set oLookups = CreateObject("MFilesAPI.Lookups")
    
    
       ' Create the lookup
       Dim oLookup : Set oLookup = CreateObject("MFilesAPI.Lookup")
    
    Dim EmpID
       ' The external ID of the employee
       oLookup.Item = GetValueListItemByName(ListID, oOneValueList, sValue, EmpID)
    
    
    Dim TargetObjID: Set TargetObjID = CreateObject("MFilesAPI.ObjID")
    TargetObjID.SetIDs 156, EmpID ' Employee Object = 156, Employee 1108 external ID for me (Display ID is 8708)
    
    
    Dim oObjVer
    Set oObjVer = Vault.ObjectOperations.GetLatestObjVer( TargetObjID, True, True)
    
    
    Vault.ObjectOperations.CheckOut(oObjVer.objid) 
    Set oObjVer = Vault.ObjectOperations.GetLatestObjVer( TargetObjID, True, True)'obviously need this for the new version created by checkout
    Vault.ObjectPropertyOperations.SetProperty oobjver, oProperty
    Vault.ObjectOperations.CheckIn(oObjVer)
    
    End Function
    
    
    
    Function GetValueListItemByName(iValueListId, ValueListItems, sNameToFind, EmpID)
    
     Dim item
     
     For Each item in ValueListItems
      ' Does this need to deal with string casing? YESSSS case sensitive
      If item.Name = sNameToFind Then
       GetValueListItemByName = item.id
       objOutFile.WriteLine item.id
       EmpID = item.id
       Exit Function
      End If
     Next
    
    End Function