How to create document history for status changes
- Register two functions in IWorkflowActionProvider:
- The first function (WriteTransitionHistory) is for making a list. Use the process Instance.IdentityIds to form a list of potential approvers.
- The second function (UpdateTransitionHistory) is for adding current action into the table of history changes.
- Add these functions into "Implementation" and "Pre-Execute Implementation" units for each Activity that defines the document status.
- Subscribe to ProcessStatusChanged event in WorkflowRuntime. In the command handler, do the following:
- Delete empty history fields (if exists).
- Call PreExecuteFromCurrentActivity method in WorkflowRuntime.
Example of method for PreExecute 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();
}
}