HOW TO CREATE A DOCUMENT HISTORY FOR STATUS CHANGES?

HOW TO CREATE A DOCUMENT HISTORY FOR STATUS CHANGES?


  1. Register two functions in IWorkflowActionProvider. 
    1.1. The first function (WriteTransitionHistory) is for making a list.* 
    1.2. The second function (UpdateTransitionHistory) is for adding a current action into the table of changes history.
  2. Add these functions into “Implemtation” and “Pre-Execute Implementation” units for each Activity that defines the document status.
  3. Subscribe to ProcessStatusChanged event in WorkflowRuntime. In the command handler, do the following: 
    3.1. Delete empty history fields (if exists). 
    3.2. Call PreExecuteFromCurrentActivity method in WorkflowRuntime.

*Use the process Instance.IdentityIds to form a list of potential approvers.

Example of method for PreExcute Implementation:

public static void WriteTransitionHistory(ProcessInstance processInstance, string parameter)
{
    if (processInstance.IdentityIds == null)
        return;

    var currentstate = WorkflowInit.Runtime.GetLocalizedStateName(processInstance.ProcessId, processInstance.CurrentState);

    var nextState = WorkflowInit.Runtime.GetLocalizedStateName(processInstance.ProcessId, processInstance.ExecutedActivityState);

    var command = WorkflowInit.Runtime.GetLocalizedCommandName(processInstance.ProcessId, processInstance.CurrentCommand);

    using (var context = new DataModelDataContext())
    {
        GetEmployeesString(processInstance.IdentityIds, context);

        var historyItem = new DocumentTransitionHistory
                              {
                                  Id = Guid.NewGuid(),
                                  AllowedToEmployeeNames = GetEmployeesString(processInstance.IdentityIds, context),
                                  DestinationState = nextState,
                                  DocumentId = processInstance.ProcessId,
                                  InitialState = currentstate,
                                  Command = command
                              };
        context.DocumentTransitionHistories.InsertOnSubmit(historyItem);
        context.SubmitChanges();
    }
}

Example of method for Implementation:

public static void UpdateTransitionHistory(ProcessInstance processInstance, string parameter)
{
    var currentstate = WorkflowInit.Runtime.GetLocalizedStateName(processInstance.ProcessId, processInstance.CurrentState);

    var nextState = WorkflowInit.Runtime.GetLocalizedStateName(processInstance.ProcessId, processInstance.ExecutedActivityState);

    var command = WorkflowInit.Runtime.GetLocalizedCommandName(processInstance.ProcessId, processInstance.CurrentCommand);

    using (var context = new DataModelDataContext())
    {
        var historyItem =
            context.DocumentTransitionHistories.FirstOrDefault(
                h => h.DocumentId == processInstance.ProcessId && !h.TransitionTime.HasValue &&
                     h.InitialState == currentstate && h.DestinationState == nextState);

        if (historyItem == null)
        {
            historyItem = new DocumentTransitionHistory
            {
                Id = Guid.NewGuid(),
                AllowedToEmployeeNames = string.Empty,
                DestinationState = nextState,
                DocumentId = processInstance.ProcessId,
                InitialState = currentstate
            };

            context.DocumentTransitionHistories.InsertOnSubmit(historyItem);

        }

        historyItem.Command = command;
        historyItem.TransitionTime = DateTime.Now;

        if (string.IsNullOrWhiteSpace(processInstance.IdentityId))
            historyItem.EmployeeId = null;
        else
            historyItem.EmployeeId = new Guid(processInstance.IdentityId);

        context.SubmitChanges();
    }
}