How to Auto Submit the Workflow Through X++ Code?

Introduction: Workflows in Dynamics 365 Finance and Operations streamline approval processes. However, in some scenarios, manually submitting a workflow can be time-consuming and prone to delays, especially in high-volume environments or when dealing with recurring processes. To address this, automating workflow submission becomes a game-changer. 

In this blog, a practical scenario of automating custom Project Estimation workflow submission is used as an example, complete with technical steps and sample code to guide users through implementation. 

Scenario: When workers create project estimates in D365FO, they often need to manually submit them for managerial approval. Automating this process ensures that estimates meeting predefined conditions are automatically submitted, saving time and reducing delays. 

High level resolution steps 

Detailed resolution steps 

Step 1: Create a new workflow and configure a workflow using your custom table to define the approval process.  

Note: Use the link below to create workflow. 

Step 2: Implement X++ code in the write method of the form’s data source to automatically submit the workflow when conditions are met. 

///  

/// Auto submit workflow 

///  

public void write() 

{ 

    super(); 

 

    Workflow::activateFromWorkflowType(workFlowTypeStr(SCSS_EmployeeProjectWorkflowType), 

 

                                        SCSS_EmployeeProjectTable.RecId, 

 

                                        "Automatic workflows submit", 

 

                                        false, 

 

                                        curUserid()); 

 

    //Update workflow status to submit 

 

    SCSS_EmployeeProjectTable::updateWorkflowStatus(SCSS_EmployeeProjectTable.RecId, SCSS_WorkflowStatus::Submitted); 

 

    SCSS_EmployeeProjectTable_ds.research(true); 

} 

Code Explanation: 

Output 

When a new project estimate is created from the UI, it remains in draft state, and as shown in the screenshot, the record is not yet saved, so the workflow is not visible in the action pane.  

Auto Submit the Workflow Through X++ Code?

Once the record is saved, the workflow appears in the action pane, marked as “Submitted,” with the “Approve” option only visible, and no “Submit” option available. 

Leave a Reply

Your email address will not be published. Required fields are marked *