D365FO: Get last workflow approver for purchase orders in X++

 

Purpose:

In this post we’re going to see how can we get last workflow approver for purchase orders in X++.

Application:

Dynamics 365 for Finance and Operations.

Business requirement:

Get the last purchase order approver in X++.

Solution:

Please use the code below to get the last purchase order approver.

Tip:

Take a note that workflow tables are cross-company tables. That’s why it is important to change company when making joins with them, else you will get empty table buffers!

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <summary>
/// Gets workflow last approver for purchase orders.
/// </summary>
public void getPurchWorkflowApprovers()
{
    WorkflowTrackingStatusTable workflowTrackingStatusTable;
    WorkflowTrackingTable workflowTrackingTable;
    PurchTable purchTable;
    PurchTable purchTableWTT;
 
    while select crosscompany purchTable
    {
        changecompany(purchTable.DataAreaId)
        {
            select firstonly crosscompany workflowTrackingTable
                order by workflowTrackingTable.RecId desc
                join workflowTrackingStatusTable
                    where workflowTrackingTable.WorkflowTrackingStatusTable == workflowTrackingStatusTable.RecId
                join purchTableWTT
                    where workflowTrackingStatusTable.ContextTableId == tableNum(PurchTable)
                        && workflowTrackingStatusTable.ContextCompanyId == purchTableWTT.DataAreaId
                        && workflowTrackingStatusTable.ContextRecId == purchTableWTT.RecId
                        && workflowTrackingTable.TrackingContext == workflowtrackingcontext::WorkItem
                        && workflowTrackingTable.TrackingType == workflowtrackingtype::Approval
                        && purchTableWTT.RecId == purchTable.RecId;
             
            info(strFmt("%1, %2", purchTable.PurchId, workflowTrackingTable.User));
        }
    }
}

Comments