web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

is it Possible to Know How Many Approvals are Needed for The Workflow Through x++ Code?

(0) ShareShare
ReportReport
Posted on by 1,054

Greetings dears!

I'm trying to find how many approvals are required for a workflow to be completed.

I want to display the workflow required approvals steps in a custom screen.

So for example we have a workflow that has 2 approval steps, 1st approval is from Department head and 2nd approval is from the HR manager.

I want to display in a screen that this type of request requires the following:

1- Approval of Department head

2- Approval of HR Manager

Is it possible through x++ code to check the workflow how many approvals are there?

Also considering the workflow version (Because the approval steps might differ between an old version or a new version of the workflow).

Thanks in advance!

  • Suggested answer
    Momochi Profile Picture
    1,054 on at

    I was able to identify how many steps in the workflow based on the workflow version too using the following query:

    1- This is if you wanted to list the steps of the enabled version of the workflow:

    SysWorkflowTable            _SysWorkflowTable;
    WorkflowTable               _WorkflowTable;
    WorkflowVersionTable        _WorkflowVersionTable;
    WorkflowElementLinkTable    _WorkflowElementLinkTable;
    WorkflowElementTable        _WorkflowElementTable;
    WorkflowStepTable           _WorkflowStepTable;
    
    while select _WorkflowTable
    	where _WorkflowTable.DOCUMENTTABLENAME == "TableName"
    	join _WorkflowVersionTable where _WorkflowVersionTable.Workflowtable == _WorkflowTable.recId
    	&& _WorkflowVersionTable.Enabled = 1 //Add this condition if you wanted to list the enabled version steps only
    	join _WorkflowElementLinkTable where _WorkflowElementLinkTable.WORKFLOWVERSIONTABLE == _WorkflowVersionTable.RECID
    	join _WorkflowElementTable where _WorkflowElementTable.RECID == _WorkflowElementLinkTable.TARGETID
    	join _WorkflowStepTable where _WorkflowStepTable.ELEMENTID == _WorkflowElementTable.ELEMENTID
    {
    
    }

    2- And this is if you wanted to list the steps based on an already submitted record:

    WorkflowTable               _WorkflowTable;
    WorkflowVersionTable        _WorkflowVersionTable;
    WorkflowElementLinkTable    _WorkflowElementLinkTable;
    WorkflowElementTable        _WorkflowElementTable;
    WorkflowStepTable           _WorkflowStepTable;
    
    while select _SysWorkflowTable
    	where _SysWorkflowTable.CONTEXTRECID == DataSource.RecId
    	&& _SysWorkflowTable.CONTEXTTABLEID == DataSource.TableId
    	join _WorkflowVersionTable where _SysWorkflowTable.CONFIGURATIONID == _WorkflowVersionTable.CONFIGURATIONID
    	join _WorkflowElementLinkTable where _WorkflowElementLinkTable.WORKFLOWVERSIONTABLE == _WorkflowVersionTable.RECID
    	join _WorkflowElementTable where _WorkflowElementTable.RECID == _WorkflowElementLinkTable.TARGETID
    	join _WorkflowStepTable where _WorkflowStepTable.ELEMENTID == _WorkflowElementTable.ELEMENTID
    {
    
    }

    Hope it helps others 

  • André Arnaud de Calavon Profile Picture
    307,097 Super User 2026 Season 2 on at

    Hi Momochi,

    Thanks for providing your own solution. Did you fully manage to get your requirement implemented or do you still need help on a specific topic?

  • Suggested answer
    Michal Krejza Profile Picture
    Microsoft Employee on at

    Hi Momochi,

    Iti is very hard to achieve something like this due to complexity of workflow processes. You would need to go through whole worfklow design and evaluate all options. It is possible to define

    • Activating conditions
    • Automatic and manual decisions,
    • Conditions - based on approval limit / approved amounts / currency / dimension
    • Hiearchy structure
    • Required number of approves.

    Your requirement would work only in a simple scenario where you won't use points listed above. And still you would need to simulate sequence of steps within workflow and record number of all steps. In my opinion it is very difficult job.

  • Momochi Profile Picture
    1,054 on at

    Hi Andre,

    I modified the code and removed the SysWorkFlowTable from the query, since I found that this table is only showing records for active workflows.

    Once the workflow is completed, it will remove the records related to that workflow run.

    Also using WorkflowTrackingStatusTable I can be able to know the latest submitted workflow record using "order by createdDateTime DESC":

    WorkflowTable               _WorkflowTable;
    WorkflowVersionTable        _WorkflowVersionTable;
    WorkflowElementLinkTable    _WorkflowElementLinkTable;
    WorkflowElementTable        _WorkflowElementTable;
    WorkflowStepTable           _WorkflowStepTable;
    WorkflowTrackingStatusTable _WorkflowTrackingStatusTable;
    
    select _WorkflowTrackingStatusTable order by createdDateTime DESC //This is to get the latest submitted version incase the workflow was returned and then re-submitted
    	Where _WorkflowTrackingStatusTable.ContextRecId == DataSource.RecId
    	&& _WorkflowTrackingStatusTable.ContextTableId == DataSource.TableId
    
    while select _WorkflowVersionTable where _WorkflowTrackingStatusTable.WorkflowVersionTable == _WorkflowVersionTable.RecId
    	join _WorkflowElementLinkTable where _WorkflowElementLinkTable.WORKFLOWVERSIONTABLE == _WorkflowVersionTable.RECID
    	join _WorkflowElementTable where _WorkflowElementTable.RECID == _WorkflowElementLinkTable.TARGETID
    	join _WorkflowStepTable where _WorkflowStepTable.ELEMENTID == _WorkflowElementTable.ELEMENTID
    {
    
    }

    But now I'm facing an issue where I'm not able to identify how many work items are there for a workflow.

    With the code above you will be able to know how many steps are there in a workflow.

    But for work items, I'm not able to find a way to get them. Because the issue is, a step could contain multiple work items.

    There are work item tables like: "WorkflowWorkItemTable" but unfortunately, this table will only show records for active workflows. so based on the workflow progress after submitting, it will insert the records. and once completed it will be removed.

    I'm trying to identify how many work items are there for a request before submitting. so the user knows how many levels are there before he submits.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#2
CU10121822-0 Profile Picture

CU10121822-0 366

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 360 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans