Best way to compare current workflow state/transition with specific state/transition

Hi M-Files community, 

As I often come to temptation/requirement to make specific business logic based on workflow state transition, I would like to start discussion with you about best practice to compare specific workflow state transitions with a current one. 

I have a concrete use case where I have a single "Reject" workflow state but multiple workflow transitions going into that workflow state (basically users can reject it from different workflow states). Based on previous workflow state or workflow state transition, I would need to apply different logic (in my case reset different comments properties in specific document).

What ever I try, I end up comparing names of workflow state transitions or comparing names of workflow states. 

Here some options what I can get:

// 1st Approach

// Get current "State transition" but only getting the name
// Here actually I do not get "State transition" at all, I suppose it is not attached to object, I get NULL
var propertyValue = env.PropertyValues.SearchForPropertyByAlias(env.Vault, "State transition", true);
var myCurrentStateTransition = propertyValue.TypedValue.DisplayValue;

// Getting workflow state transition ID by alias (we tend to use alias as they are more stable than IDs)
var mySpecificStateTransitionId = env.Vault.WorkflowOperations.GetWorkflowStateTransitionIDByAlias("<MY SPECIFIC WORKFLOW STATE TRANSITION>");

// Then compare 
// PROBLEM: One one side I have name on the other side Id!?

// 2nd Approach

// Comparing workflow states
// Get all workflow states in descending order
var objVersions = env.Vault.ObjectOperations.GetHistoryEx(env.ObjVer.ObjID, MFHistoryRetrievalMode.MFHistoryRetrievalDescending);   
// Get workflow state from previous version objVersions[2].ObjVer
var previousWorkflowState = env.Vault.ObjectPropertyOperations.GetProperty(objVersions[2].ObjVer, Constants.WORKFLOW_STATE);
            
// Compare 
// PROBLEM: I can get workflow state id by alias, guid but I can not get the current name
// Actually I would like to compare aliases from previous state and specific state
if(previousWorkflowState.TypedValue.DisplayName == "<some hardcoded workflow state>")

2nd Approach works generally but has a problem that I need to search through history for previous workflow state (still ok I guess) but I need to hard-coded name of workflow state I would expect. It would be very good if I could compare alias from previous state with alias from specific state. Names and Ids can easily change, also very often we tend to use the same names (also additional problem with duplicated names but ok this is our problem) where this logic would not work. Also names can be easily change without thinking about consequences (some user wish another name, we change it and suddenly the business logic does not work any more).

This is why I think comparing aliases would be the best solution as they are usually treated more serious as another values.

I am wondering why PropertyValue does not have alias info:

PropertyValue

-> PropertyDef

-> TypedValue

-> Alias (currently missing)

I think this could really help with these kind of comparisons and would make them more stable and secure.

What do you think about that idea?

Do you use different technique for this kind of comparisons? I would be glad to hear about your experiences.

Thanks,

Dejan

  • Hi,

    Would you have a specific use case ? Instead of comparing aliases and names you can get the Property Value maybe,
    I did write some VBScript in order to change properties for a huge amount of documents but they would keep the same state as before. Would that help you maybe ?

    Regards
    Claudio

  • You can assign a workflow state an alias, and then use that in your VAF code.

    In your configuration class you can have a property or field like this:

    /// <summary> Refers to a workflow state in the vault. </summary>
    // This refers to a state by the alias in quotes.
    [MFState(Required = true)]
    public MFIdentifier ApprovedWorkflowState = "MFiles.WorkflowState.Approval.Approved";
    

    You can then compare the value from the history with the ID of the state from your configuration:

    // Comparing workflow states
    // Get all workflow states in descending order
    var objVersions = env.Vault.ObjectOperations.GetHistoryEx(env.ObjVer.ObjID, MFHistoryRetrievalMode.MFHistoryRetrievalDescending);   
    
    // Get workflow state from previous version objVersions[2].ObjVer.
    // NOTE: I don't especially like assuming that it'll always be number two...!
    var previousWorkflowState = env.Vault.ObjectPropertyOperations.GetProperty(objVersions[2].ObjVer, (int)MFBuiltInPropertyDef.MFBuiltInPropertyDefState);
    
    // Compare
    // NOTE: The lookup ID of the state property is the ID to match against.
    if(previousWorkflowState.TypedValue.GetLookupID() == this.Configuration.ApprovedWorkflowState.ID)
    {
      // I came from the approved state.
    }
    else if(...)
    {
    }

    Regards,

    Craig.