Getting Workflow Approvers In Dynamics AX (AX 2012)

Getting Workflow Approvers In Dynamics AX (AX 2012)

UNDERSTANDING THE LOGIC ABOUT WORKFLOW APPROVALS
To start with, you need to understand how the workflow approvals are processed in Dynamics AX.
There are three tables that we will use
WorkflowTrackingStatusTable
WorkflowTrackingTable
WorkflowTrackingCommentTable

This three tables can be used to get the approvers names, Approval Dates and Approval Comment,

HOW THE TABLES RELATE TO EACH OTHER
WorkflowTrackingStatusTable relates to using the field WorkflowTrackingStatusTable
in WorkflowTrackingTable and RecId in WorkflowTrackingStatusTable

Simple X++ select Statement
WorkflowTrackingStatusTable _status;
WorkflowTrackingTable _tracking;
select * from _tracking where _tracking.WorkflowTrackingStatusTable == _status.RecId;

WorkflowTrackingStatusTable relates to using the field WorkflowTrackingStatusTable
in WorkflowTrackingTable and RecId in WorkflowTrackingStatusTable

Simple X++ select statement
WorkflowTrackingTable _tracking;
WorkflowTrackingCommentTable _comment;
select * from _comment where _comment.WorkflowTrackingTable == _tracking.RecId;


RELATION ABOUT WORKFLOW TABLES AND OTHER TABLES
This is the most important Bit, All tables are passed to workflow using the respective fields RECID and TABLEID to the table WorkflowTrackingStatusTable using fields CONTEXTRECID and CONTEXTTABLEID

Simple X++ Example
Assuming you want to get the purchase Order entry to the table WorkflowTrackingStatusTable for a purchase Order with a RecId 5637161092
WorkflowTrackingStatusTable _status;
RecId _recidmain;
;
_recidmain = 5637161092;

select * from _status where _status.ContextRecId == _recidmain && _status.ContextTableId == tableNum(PurchTable)

NB: You may find more than one entry in this table. This may imply that the Purchase Order had bein Recalled and resubmitted again or a change request had been done and the process started again. So to make sure you get the correct line in workflow pick the last line. You can simply use a while select so as to loop to the last line

X++ JOB TO COMBINE THE ABOVE
We will do a simple test to get the Approvers for a given Purchase Order
To start with Create a table that you will store the Approval data in my case I created a table called PurchaceorderapprovalTmp

The fields will be
Approver_name — Datatype-String; Character Length-25
Approval_date — Datatype-String; Character Length-25
Purchtable — Datatype-String; Character Length-25


Then paste the following code to a job

public void approver()
{
// Method to get the workflow approvers
// By JOSEPH KAMAU
//………………………………………
WorkflowTrackingStatusTable _status;
WorkflowTrackingTable _tracking;
WorkflowTrackingCommentTable _comment;
PurchaceorderapprovalTmp _tmptable;
UserInfo _user;
RecId _recid,_recidmain;
;
_recidmain = 5637161092;     // Put the RECID of the Purchase ORDER you want to test

while select * from _status where _status.ContextRecId == _recidmain && _status.ContextTableId == tableNum(PurchTable) //Loop to get the last item
{
_recid = _status.RecId;                                                           //Pass the recid to a variable
}
while select * from _tracking where _tracking.WorkflowTrackingStatusTable == _recid
{
select * from _comment where _comment.WorkflowTrackingTable == _tracking.RecId;   //To get the comment and Creation Dates
select * from _user where _user.id == _tracking.user;                            // To get the User name

if ((subStr(_comment.TrackingMessage,0,13) == ‘Submitted by:’)
|| (subStr(_comment.TrackingMessage,0,24) ==’Approve action taken by:’))   //To filter the records and get only the Person submitting and people approving
{
ttsBegin;
_tmptable.Approver_name = _user.name;                                     //To store the entries to a table
_tmptable.Approval_date = datetime2str(_comment.createdDateTime,123);
_tmptable.Purchtable = int642str(_recid);
_tmptable.insert();
ttsCommit;
}
}
while select _tmptable
{
info (strFmt(‘user name %1, Approval date %2′,_tmptable.Approver_name),datetime2str(_comment.createdDateTime,123)); //To see the saved data
}
}

Comments

Popular posts from this blog

Creating a numbersequence in form level in ax 2012

AX 2012 R3 Line by line invoicing the sales order using X++ code