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 to auto complete outstanding assignments under a document when it' done

Former Member
Former Member
Hi,

I am configuring/testing a new document workflow in M-Files: when a new document is created, a few separate assignments are created to trace the tasks user need to perform related to the document and send reminer email when due date is reached.

Here the my problem:
When the document reaches the final DONE state, all the assignments created earlier should be have been completed (the state transition is a proof that all the assignments are completed) . Instead of the user having to manual select each assignemnt and click: 'Mark Complete', I would like it to happen automatically. I belive the right way to implement this is: Workflow->(Done) State->Actions->"Run script". However I don't have enough VB expereince to code the script myself, or can I find a good example to begin with. I would appreciate very much if someone can share me some samples or give me a head start. Thank you in advance!

This is how my VB script should look like:
[list type=decimal]
  • Retrieve the list of assignments assigned to the current document;

  • For each assignment, if it is not completed, mark it as completed.

[/list]
Parents
  • Dear Kevin Lin,

    This is not trivial scripting task that one can solve without any scripting knowledge.
    Your best bet is to consult m-files paid support to get this done.
    After you get this solved I hope you share your solution with community

    Here is my (failed) attempt to approach to this in workflow action script.
    Maybe it gives you idea of general approach:

    Option Explicit

    'search for assignments that are related to current object
    dim RelatedAssignments: set RelatedAssignments = SearchRelatedAssignments( ObjVer )

    'There are related assignments
    if RelatedAssignments.count > 0 then

    'Get search results as ObjVers collection
    DIM RelatedAssignmentsObjVers: SET RelatedAssignmentsObjVers = RelatedAssignments.GetAsObjectVersions().GetAsObjVers()

    'Get PropertyValues for all search results in PropertyValuesOfMultipleObjects collection
    DIM PropertyValuesOfAssignments
    SET PropertyValuesOfAssignments = Vault.ObjectPropertyOperations.GetPropertiesOfMultipleObjects(RelatedAssignmentsObjVers)

    dim CurrentAssignmentPropertyValues
    dim CurrentAssignmentObjVer

    'Iterate assignments
    dim assignment_index
    For assignment_index = 1 To RelatedAssignmentsObjVers.Count

    set CurrentAssignmentPropertyValues = PropertyValuesOfAssignments.item(assignment_index)
    set CurrentAssignmentObjVer = RelatedAssignmentsObjVers.item(assignment_index)

    'Get Assigned To PropertyValue
    dim AssignedToPv: set AssignedToPv = CurrentAssignmentPropertyValues.SearchForProperty( MFBuiltInPropertyDefAssignedTo)

    'Something is selected in Assigned To
    if not AssignedToPv.Value.IsNull() then

    'Check Out Assignment
    dim CheckedOutAssignmentVersion: set CheckedOutAssignmentVersion = Vault.ObjectOperations.CheckOut(CurrentAssignmentObjVer.ObjID)

    'Get What is selected as lookups
    dim AssignedToLookups: set AssignedToLookups = AssignedToPv.Value.GetValueAsLookups()

    dim currentLookup
    'For every user who is assigned to this assignment

    dim latestVersionAndProps
    for each currentLookup in AssignedToLookups

    'Mark assignment as complete
    Vault.ObjectPropertyOperations.MarkAssignmentCompleteByUser CheckedOutAssignmentVersion.ObjVer, currentLookup.Item
    next

    Vault.ObjectOperations.CheckIn(CheckedOutAssignmentVersion.ObjVer)
    end if
    Next
    end if


    'Search for assignments that are related to provided ObjVer
    Function SearchRelatedAssignments( RelatedObjVer )

    Dim SearchConditions: Set SearchConditions = CreateObject("MFilesAPI.SearchConditions")

    'Where Object is not deleted
    dim NotDeletedSearchCondition: set NotDeletedSearchCondition = CreateObject("MFilesAPI.SearchCondition")
    NotDeletedSearchCondition.Expression.DataStatusValueType = MFStatusTypeDeleted
    NotDeletedSearchCondition.ConditionType = MFConditionTypeEqual
    NotDeletedSearchCondition.TypedValue.SetValue MFDatatypeBoolean, False
    SearchConditions.Add -1, NotDeletedSearchCondition

    'Where Object Type is Assignment
    dim ObjTypeSearchCondition: set ObjTypeSearchCondition = CreateObject("MFilesAPI.SearchCondition")
    ObjTypeSearchCondition.Expression.DataStatusValueType = MFStatusTypeObjectTypeID
    ObjTypeSearchCondition.ConditionType = MFConditionTypeEqual
    ObjTypeSearchCondition.TypedValue.SetValue MFDatatypeLookup, MFBuiltInObjectTypeAssignment
    SearchConditions.Add -1, ObjTypeSearchCondition

    'Where Current object type (Any Property) is pointing to current object instance. You may want to use some other condition here (maybe Assignment default property).
    dim RelatedToThisObjectSearchCondition: set RelatedToThisObjectSearchCondition = CreateObject("MFilesAPI.SearchCondition")
    RelatedToThisObjectSearchCondition.Expression.SetTypedValueExpression MFDatatypeMultiSelectLookup, RelatedObjVer.Type, MFParentChildBehaviorNone, Nothing
    RelatedToThisObjectSearchCondition.ConditionType = MFConditionTypeEqual
    RelatedToThisObjectSearchCondition.TypedValue.SetValue MFDatatypeMultiSelectLookup, RelatedObjVer.ID
    SearchConditions.Add -1, RelatedToThisObjectSearchCondition

    Set SearchRelatedAssignments = Vault.ObjectSearchOperations.SearchForObjectsByConditions(SearchConditions, MFSearchFlagNone, false)

    end function


    While this seems to work, when "Assigned To" is manually selected.
    It does not work on Assignment that were created by "Create Separate Assignments" feature of workflow.
    It does seem to remove, whatever is selected in "Assigned To" property on assignment.
    there is a forum thread about marking assignments complete: community.m-files.com/index.php
    Maybe I'm doing something incorrectly. Maybe It is a bug of M-Files.
    Either way I am not using assignments in my environment and I don't have personal interest in getting this resolved. So I stopped trying.

Reply
  • Dear Kevin Lin,

    This is not trivial scripting task that one can solve without any scripting knowledge.
    Your best bet is to consult m-files paid support to get this done.
    After you get this solved I hope you share your solution with community

    Here is my (failed) attempt to approach to this in workflow action script.
    Maybe it gives you idea of general approach:

    Option Explicit

    'search for assignments that are related to current object
    dim RelatedAssignments: set RelatedAssignments = SearchRelatedAssignments( ObjVer )

    'There are related assignments
    if RelatedAssignments.count > 0 then

    'Get search results as ObjVers collection
    DIM RelatedAssignmentsObjVers: SET RelatedAssignmentsObjVers = RelatedAssignments.GetAsObjectVersions().GetAsObjVers()

    'Get PropertyValues for all search results in PropertyValuesOfMultipleObjects collection
    DIM PropertyValuesOfAssignments
    SET PropertyValuesOfAssignments = Vault.ObjectPropertyOperations.GetPropertiesOfMultipleObjects(RelatedAssignmentsObjVers)

    dim CurrentAssignmentPropertyValues
    dim CurrentAssignmentObjVer

    'Iterate assignments
    dim assignment_index
    For assignment_index = 1 To RelatedAssignmentsObjVers.Count

    set CurrentAssignmentPropertyValues = PropertyValuesOfAssignments.item(assignment_index)
    set CurrentAssignmentObjVer = RelatedAssignmentsObjVers.item(assignment_index)

    'Get Assigned To PropertyValue
    dim AssignedToPv: set AssignedToPv = CurrentAssignmentPropertyValues.SearchForProperty( MFBuiltInPropertyDefAssignedTo)

    'Something is selected in Assigned To
    if not AssignedToPv.Value.IsNull() then

    'Check Out Assignment
    dim CheckedOutAssignmentVersion: set CheckedOutAssignmentVersion = Vault.ObjectOperations.CheckOut(CurrentAssignmentObjVer.ObjID)

    'Get What is selected as lookups
    dim AssignedToLookups: set AssignedToLookups = AssignedToPv.Value.GetValueAsLookups()

    dim currentLookup
    'For every user who is assigned to this assignment

    dim latestVersionAndProps
    for each currentLookup in AssignedToLookups

    'Mark assignment as complete
    Vault.ObjectPropertyOperations.MarkAssignmentCompleteByUser CheckedOutAssignmentVersion.ObjVer, currentLookup.Item
    next

    Vault.ObjectOperations.CheckIn(CheckedOutAssignmentVersion.ObjVer)
    end if
    Next
    end if


    'Search for assignments that are related to provided ObjVer
    Function SearchRelatedAssignments( RelatedObjVer )

    Dim SearchConditions: Set SearchConditions = CreateObject("MFilesAPI.SearchConditions")

    'Where Object is not deleted
    dim NotDeletedSearchCondition: set NotDeletedSearchCondition = CreateObject("MFilesAPI.SearchCondition")
    NotDeletedSearchCondition.Expression.DataStatusValueType = MFStatusTypeDeleted
    NotDeletedSearchCondition.ConditionType = MFConditionTypeEqual
    NotDeletedSearchCondition.TypedValue.SetValue MFDatatypeBoolean, False
    SearchConditions.Add -1, NotDeletedSearchCondition

    'Where Object Type is Assignment
    dim ObjTypeSearchCondition: set ObjTypeSearchCondition = CreateObject("MFilesAPI.SearchCondition")
    ObjTypeSearchCondition.Expression.DataStatusValueType = MFStatusTypeObjectTypeID
    ObjTypeSearchCondition.ConditionType = MFConditionTypeEqual
    ObjTypeSearchCondition.TypedValue.SetValue MFDatatypeLookup, MFBuiltInObjectTypeAssignment
    SearchConditions.Add -1, ObjTypeSearchCondition

    'Where Current object type (Any Property) is pointing to current object instance. You may want to use some other condition here (maybe Assignment default property).
    dim RelatedToThisObjectSearchCondition: set RelatedToThisObjectSearchCondition = CreateObject("MFilesAPI.SearchCondition")
    RelatedToThisObjectSearchCondition.Expression.SetTypedValueExpression MFDatatypeMultiSelectLookup, RelatedObjVer.Type, MFParentChildBehaviorNone, Nothing
    RelatedToThisObjectSearchCondition.ConditionType = MFConditionTypeEqual
    RelatedToThisObjectSearchCondition.TypedValue.SetValue MFDatatypeMultiSelectLookup, RelatedObjVer.ID
    SearchConditions.Add -1, RelatedToThisObjectSearchCondition

    Set SearchRelatedAssignments = Vault.ObjectSearchOperations.SearchForObjectsByConditions(SearchConditions, MFSearchFlagNone, false)

    end function


    While this seems to work, when "Assigned To" is manually selected.
    It does not work on Assignment that were created by "Create Separate Assignments" feature of workflow.
    It does seem to remove, whatever is selected in "Assigned To" property on assignment.
    there is a forum thread about marking assignments complete: community.m-files.com/index.php
    Maybe I'm doing something incorrectly. Maybe It is a bug of M-Files.
    Either way I am not using assignments in my environment and I don't have personal interest in getting this resolved. So I stopped trying.

Children
No Data