Skip to main content
X++ Development · · 2 min read

Approve Purchase Requisition Workflow

Approve Purchase Requisition workflows in D365 F&O using X++ for workflow approval processing and business logic customization.

Ahmad Hassan

Ahmad Hassan

Dynamics 365 F&O Consultant

Share
Approve Purchase Requisition Workflow

This customization handles the Approved button click on the PurchReqTableListPage and processes the selected purchase requisition workflow. It identifies the pending workflow work item assigned to the current user and completes the workflow using WorkflowWorkItemActionManager. If no pending work item is found, a warning is displayed.

X++ Code


    public static void Approved_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormDataSource purchReqTableDS = sender.formRun().dataSource(formDataSourceStr(PurchReqTableListPage, PurchReqTable));
        PurchReqTable purchReqTable = purchReqTableDS.cursor();
        WorkflowWorkItemTable workItem;
        UserId currentUser = curUserId();

        select firstOnly forUpdate workItem
        where workItem.RefTableId == tableNum(PurchReqTable)
&& workItem.RefRecId   == purchReqTable.RecId
&& workItem.Status     == WorkflowWorkItemStatus::Pending
&& workItem.UserId     == currentUser;

        if (workItem.RecId)
        {
            try
            {
                ttsbegin;

                WorkflowWorkItemActionManager::dispatchWorkItemAction(
                workItem,
                '', // Optional comment
                currentUser,
                WorkflowWorkItemActionType::Complete,
                menuItemActionStr(PurchReqApprovalApprove),
                ''
            );

                ttscommit;
                info("Workflow approved successfully.");
            }
            catch
            {
                ttsabort;
                error("Error occurred while approving workflow.");
            }
        }
        else
        {
            warning("No pending workflow item found for this requisition.");
        }
    }

Process Flow

The Approved button click event retrieves the selected PurchReqTable record and the current user.

It searches for a pending WorkflowWorkItemTable record associated with the purchase requisition and current user.

If a work item is found, the workflow action is completed through WorkflowWorkItemActionManager inside a transaction.

If no pending work item is found or an exception occurs, the corresponding warning or error message is displayed.

d67c2c31-daf7-4b6e-bc57-884a76d83d37.png

Figure: Flow of approving a purchase requisition workflow for the current user.

Found this useful?

Share it with your network

Ahmad Hassan

Written by

Ahmad Hassan

Microsoft Dynamics 365 & Power Platform blogger.

More about me

Related Posts

Comments

Leave a comment