Thursday, May 31, 2018

D365/AX7 – How to apply workflow hierarchy type at runtime

The default workflow hierarchy providers as provided by Microsoft Dynamics AX, allow us to use managerial or configurable hierarchy as setup for the workflow. However, when comes a requirement where the hierarchy type of the workflow should be decided at run time, we need to do some customizations with workflow hierarchy providers.


Problem


To select workflow hierarchy type for each record at run time. 







Here, we need the workflow to be approved by the Task hierarchy of the workflow originator (say Aaliyah). The Task hierarchy of the worker Aaliyah is as follows.




Solution


To achieve above requirement, follow the steps below to create custom workflow hierarchy provider for any specified hierarchy type.

Step#1 In order to add a custom workflow hierarchy provider, refer the blog How to add custom workflow hierarchy assignment provider

Step#2 Add a method to get the position hierarchy type from the record the workflow is executing for.

public HcmPositionHierarchyTypeRecId getPositionHierarchyType(
WorkflowContext _workflowContext)
{
MY_TaskTable                 taskTable;

if(_workflowContext.parmTableId() == tableNum(MY_TaskTable))
{
taskTable = MY_TaskTable::find(_workflowContext.parmRecId());
}

return taskTable.PositionHierarchyType;

}

Step#3 Modify the method getNextNode as below

public WorkflowHierarchyProviderNode getNextNode(anytype _nodeId,
WorkflowHierarchyLevel _level,
WorkflowContext _workflowContext)
{
HcmPositionHierarchyTypeRecId positionHierarchyTypeRecId = this.getPositionHierarchyType(_workflowContext);
       
if (positionHierarchyTypeRecId == 0)
{
throw error("Position hierarchy not defined for the workflow");
}

return helper.getNextNode(_nodeId, _level, _workflowContext, dataSource, positionHierarchyTypeRecId);
}

Step#4 Build the relevant model.

Execution


Setup the workflow using task workflow hierarchy provider.


According to the task hierarchy of the worker Aaliya (workflow originator), and the workflow design as above, the task will be first assigned to Adam and then to Julia for approval. 


No comments:

Post a Comment