Alfasith AX

Alfasith AX
اللَّهُمَّ انْفَعْنِي بِمَا عَلَّمْتَنِي، وَعَلِّمْنِي مَا يَنْفَعُنِي، وَزِدْنِي عِلْمًا

Thursday, July 23, 2026

WorflowWorkitemActionManager- Customizations. - Validation on workflow approval actions in D365 Fno / AX 2012 R3

D365FO Workflow Validation Before Action Execution

Workflow Validation Before Executing a Workflow Action in Dynamics 365 Finance & Operations

In workflow-enabled applications, it is often necessary to validate business rules before allowing a workflow action such as Approve, Reject, or Complete. This example demonstrates how to perform a validation before executing a workflow action.

Main Method

The main() method retrieves the active workflow work item, gets the associated business record, validates it, and executes the workflow action only if the validation succeeds.

public static void main(Args _args)
{
    WorkflowWorkItemTable workItem;
    Common common;
    boolean ret = true;
    PRTDieselRequestTaskWFActionManager actionManager =
        new PRTDieselRequestTaskWFActionManager();

    workItem = _args.caller().getActiveWorkflowWorkItem();
    common = workItem.getRecord();

    ret = PRTDieselRequestTaskWFActionManager::validate(common);

    if (ret)
    {
        actionManager.run(_args);
    }
    else
    {
        throw error("Task must be valid to perform the action");
    }
}

Validation Method

The validation method verifies whether at least one diesel request line has been approved. If no approved line exists, the workflow action is blocked.

public static boolean validate(Common _common)
{
    boolean ret = true;

    if (_common.TableId == tableNum(PRTDieselRequestTable))
    {
        PRTDieselRequestTable dieselRequestTable =
            PRTDieselRequestTable::findRec(_common.RecId);

        PRTDieselRequestLine dieselRequestLine;

        select firstonly Approved
            from dieselRequestLine
            where dieselRequestLine.DieselRequestId ==
                    dieselRequestTable.DieselRequestId
               && dieselRequestLine.Approved;

        ret = dieselRequestLine.Approved
            ? dieselRequestLine.Approved
            : false;
    }

    return ret;
}

How the Code Works

  1. Retrieves the active workflow work item.
  2. Obtains the underlying business record.
  3. Calls the custom validation method.
  4. Checks whether at least one diesel request line is approved.
  5. If validation succeeds, the workflow action executes.
  6. If validation fails, an error is displayed and the workflow action is cancelled.
Validation Error:
Task must be valid to perform the action.

Business Scenario

Consider a Diesel Request workflow where users submit multiple request lines for approval. Before the workflow task can be approved, at least one line must already be marked as approved. This validation prevents users from approving an empty or invalid request, ensuring compliance with business rules.

Conclusion

Adding validation before workflow execution is a best practice in Dynamics 365 Finance & Operations. It helps enforce business rules, improves data integrity, and prevents invalid workflow transitions.

Hi,


/// </summary>

internal class PRTOilRequestTaskWFActionManager

{

    public void run(Args _args)

    {

        WorkflowWorkItemActionManager::main(_args);

    }

    public static void main(Args _args)

    {

        WorkflowWorkItemTable         workItem;

        Common                        common;

        boolean                       ret = true;

        PRTOilRequestTaskWFActionManager     actionManager = new PRTOilRequestTaskWFActionManager();

        

        workItem = _args.caller().getActiveWorkflowWorkItem();

        common = workItem.getRecord();

        ret = PRTOilRequestTaskWFActionManager::validate(common);

        if (ret)

        {

            actionManager.run(_args);

        }

        else

        {

            throw error('Task must be valid to perform the action');

        }

    }

    public static boolean validate(Common _common)

    {

        boolean                     ret = true;

        

        if (_common.TableId == tableNum(PRTOilRequestTable))

        {

            PRTOilRequestTable   OilRequestTable  = PRTOilRequestTable::findRec(_common.RecId);

            PRTOilRequestLine    OilRequestLine;

            select firstonly1 Approved from OilRequestLine where

                OilRequestLine.OilRequestId == OilRequestTable.OilRequestId

                && OilRequestLine.Approved;

            ret = OilRequestLine.Approved ? OilRequestLine.Approved : false;

            

        }

        return ret;

    }

}


Thanks,

Alfasith

No comments:

Post a Comment

WorflowWorkitemActionManager- Customizations. - Validation on workflow approval actions in D365 Fno / AX 2012 R3

D365FO Workflow Validation Before Action Execution Workflow Validation Before Executing a Workflow Action in Dynam...