Wednesday, May 30, 2018

D365/AX7 – How to add expression field for workflow hierarchy assignment


We have several scenarios when we need to have option to select additional field for hierarchy stopping condition and expression to exclude users when setting up the workflow design. Let’s have a look at how we can add field for hierarchy assignment expression for workflow.


Problem

To add field for hierarchy assignment conditions i.e. stopping condition and the condition to exclude users.
Consider a scenario where we need to assign workflow to an employee’s line managerial hierarchy. We need to add condition to exclude the workers (users) from the hierarchy, who does not belong to the department 022.

Solution

Following steps need to be performed to achieve the above requirement.
Step#1 Add new class to put all the relevant event handlers

class TaskWorkflowHierarchyProviderHandler
{
    const str Workflow_DepartmentId = 'DepartmentId';       

}

Step#2 Copy following event handlers from the class WorkflowHierarchyProviderHelper to your event handler class
1.       addAdditionalFieldsToDataSourceDelegate
2.       addDataSourceFieldsDelegate

Step#3 Add the below code to the method addAdditionalFieldsToDataSourceDelegate


/// <summary>
/// Add fields to data source
/// </summary>
/// <param name="_datasource">Expression data source</param>
[SubscribesTo(classStr(WorkflowHierarchyProviderHelper), delegateStr(WorkflowHierarchyProviderHelper, addAdditionalFieldsToDataSourceDelegate))]
public static void addAdditionalFieldsToDataSourceDelegate(ExpressionDataSource _datasource)
{
      _datasource.addField(ExpressionField::newFieldDefinition(Workflow_DepartmentId, extendedTypeStr(OMOperatingUnitNumber), "Department Id"));

}

Step#4 Add the below code to the method addDataSourceFieldsDelegate

/// <summary>
/// Add fields with values to the node data source
/// </summary>
/// <param name="_nodeDataSource">Node data source</param>
/// <param name="_workflowContext">Workflow context</param>
/// <param name="_hcmWorker">Worker at node</param>
/// <param name="_positionRecId">Positon of worker </param>
[SubscribesTo(classStr(WorkflowHierarchyProviderHelper), delegateStr(WorkflowHierarchyProviderHelper, addDataSourceFieldsDelegate))]
public static void addDataSourceFieldsDelegate(ExpressionDataSource _nodeDataSource,
WorkflowContext _workflowContext,
HcmWorker _hcmWorker,
HcmPositionRecId _positionRecId)
{
        HcmPositionDetail   postionDetail = HcmPositionDetail::findByPosition(_positionRecId);
        OMOperatingUnit     department = OMOperatingUnit::find(postionDetail.Department, OMOperatingUnitType::OMDepartment);
       
        _nodeDataSource.addField(ExpressionField::newFieldValue(Workflow_DepartmentId, extendedTypeStr(OMOperatingUnitNumber), department.OMOperatingUnitNumber));

}

This method sets the value for department Id for each worker in hierarchy.

Step#5 Build the respective model

Execution

Workflow will be assigned to the managerial hierarchy of supervisor Aaliyah. Set manager level to 2, so we have the hierarchy including Adam and Julia.


Add condition to exclude users who does not belong to department 022

Submit the workflow to verify the workflow assignment. The workflow assignment exclude the worker Adam, as he does not belong to the department 022, and thus directly assigns to Julia.

No comments:

Post a Comment