Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
A workflow is a system or business process that defines how a document flows through a system by showing who must complete a task, make a decision, or approve a document. You can configure workflows in the applicable system or create them in a development environment.
In this unit, you learn how to create a flow and its elements from a development environment. Workflow elements include:
In Microsoft Visual Studio, you use a wizard to create a workflow type and subsequent workflow configurations. First, you must create several elements for which the workflow type wizard prompts you, including:
To create the workflow category, query, and document menu item, follow these steps:
In Solution Explorer, specify a project for the workflow development by right-clicking the solution to open the context menu and then selecting Add > New project.
Enter MyWorkflow as the name for the project.
Note
You should have previously created a solution for use in this step.
On the properties for the project, select True for Synchronize Database because you're adding a new table to the project.
Right-click the project to open the context menu, and then select Add > New Item.
Go to Dynamics 365 Items > Business Process and Workflow.
Select workflow category, and then enter MyWorkflowCategory in the Name field.
Open the created workflow category in Visual Studio Designer, and then in Properties, set the module to Inventory and enter My workflow category for the label.
Create a base enumerator to manage the workflow’s status by right-clicking the project to open the context menu and then selecting Add > New Item.
Select Data Types and then select Base Enum.
In the Name field, enter MyWorkflowStatus and then select Add. Right-click the new base enumerator to open the context menu and then select New Element.
Add the following seven new elements to the Base Enum (we recommend that you set the name and label):
Create a table that includes the workflow status by right-clicking the project to open the context menu, selecting Add > Table, and then entering MyWorkflowTable as the name.
Add the following extended data types to the Field node:
Select Save.
Right-click the project to open the context menu, select Add > Form, and then enter the name MyWorkflowTableForm. The created form is now in Visual Studio Designer.
Drag your workflow table (MyWorkflowTable) into the Data Sources node on the form.
Right-click the form in the design area to open the context menu, and then select Apply Pattern > Custom.
Right-click the form in the design area to open the context menu, and then select New > Action Pane.
Right click the form in the design area to open the context menu, and then select New > Grid.
Open the Data Sources node, select your data source (MyWorkflowTable), and then select Fields. Select and drag the Num, Description, and MyWorkflowStatus fields from the Fields node into the grid in the design area. Select Save.
Right-click the project to open the context menu, and then select Add > New Item. Go to the user interface and select Display Menu Item.
Enter a name, such as MyWorkflowTableForm. You use this display menu item for the Workflow type and to add the form to a menu.
On the properties for the new Display Menu Item, select an Object of MyWorkflowTableForm.
Enter a label for the menu item, such as My workflow, and then select Save.
In Application Explorer, search for the InventoryManagement menu under User Interface, and then right-click the menu and select Create Extension. (Your solution should reference ApplicationSuite.) The system creates a menu extension in your project.
Right-click the menu extension in Solution Explorer, and then select Open. The menu extension opens in Designer.
Go to the InquiriesAndReports node. Select and drag the display menu item from the project to the InquiriesAndReports node in the menu extension. Select Save.
Right-click the project to open the context menu, and then select Add > New Item. Go to Data Model and select Query. Enter the name MyWorkflowTableQuery.
Select and drag the table (MyWorkflowTable) from the project into the Data Source node on the query.
In the properties for the MyWorkflowTable data source, in the Dynamic Fields property, select Yes and then select Save.
You’ve now created all required elements for creating a workflow type.
After creating all required elements for creating a workflow type, you can create a workflow type.
Right-click the project to open the context menu, and then select Add > New Item. Go to Business Process and Workflow.
Select Workflow Type, and then enter the name MyWorkflowType. The Workflow Type wizard opens.
Enter your newly created workflow category, query, and display menu items in the fields of the same names, and then select Next:
Category - MyWorkflowCategory
Query - MyWorkflowTableQuery
Document menu item - MyWorkflowTableForm
The Preview window lists the workflow type artifacts (classes and menu items). Select Next to create the workflow type and artifacts. The Add New Items window opens again, and you can close it by selecting Cancel. Several new elements should now be added to your project.
Open the two new Action Menu items and change the following labels:
Select Save All.
Open the SubmitManager class (MyWorkflowSubmitManager), and then replace the existing code with the following code. A dialog opens when you run the code. When you close the dialog, the new table’s status should change to Submit.
/// <summary>
/// The MyWorkflowTypeSubmitManager menu item action event handler.
/// </summary>
public class MyWorkflowTypeSubmitManager
{
public static void main(Args args)
{
// TODO: Write code to execute once a work item is submitted.
MyWorkflowTable myWorkflowTable;
WorkflowComment note = "";
WorkflowSubmitDialog workflowSubmitDialog;
WorkflowCorrelationId workflowCorrelationId;
WorkflowTypeName workflowTypeName = workFlowTypeStr('MyWorkflowType');
// Use WorkflowSubmitDialog to create a dialog.
workflowSubmitDialog = WorkflowSubmitDialog::construct(args.caller().getActiveWorkflowConfiguration());
workflowSubmitDialog.run();
if (workflowSubmitDialog.parmIsClosedOK())
{
myWorkflowTable = args.record();
note = workflowSubmitDialog.parmWorkflowComment();
try
{
ttsbegin;
workflowCorrelationId = Workflow::activateFromWorkflowType(workflowTypeName, myWorkflowTable.RecId, note, NoYes::No);
myWorkflowTable.MyWorkflowStatus = MyWorkflowStatus::Submit;
myWorkflowTable.update();
ttscommit;
info("Workflow submitted.");
}
catch (Exception::Error)
{
error("Workflow error.");
}
}
args.caller().updateWorkFlowControls();
}
}
You can set each status in the workflow event handler class by using a method for updating the Status field on the table that you previously created.
Open the workflow table (MyWorkflowTable) in Visual Studio Designer, and then right-click it to open the context menu. To open the code editor, select View Code.
Create a new public
method for updating the status for a specific RecId, as follows:
public class MyWorkflowTable extends common
{
public static void updateWorkflowStatus(RefRecId _recId , MyWorkflowStatus _status)
{
MyWorkflowTable myWorkflowTable;
ttsbegin;
select forupdate myWorkflowTable
where myWorkflowTable.RecId == _recId;
if (myWorkflowTable)
{
myWorkflowTable.MyWorkflowStatus = _status;
myWorkflowTable.update();
}
ttscommit;
}
}
Go to the workflow table (MyWorkflowTable), and then right-click the Methods node to open the context menu.
Select Override, and then select the canSubmitToWorkflow
method. The method should return a status of True so that you can submit the workflow. The system should now allow you to submit all incomplete workflows.
/// <summary>
/// Can workflow be submitted.
/// </summary>
/// <param name = "_workflowType"></param>
/// <returns></returns>
public boolean canSubmitToWorkflow(str _workflowType = '')
{
boolean ret;
ret = super(_workflowType);
if (this.MyWorkflowStatus != MyWorkflowStatus::Complete)
{
ret = true;
}
else
{
ret = false;
}
return ret;
}
Select Save all and then build the project.
Open the event handler
class for the workflow type (MyWorkflowTypeEventHandler). The class has event methods for started, canceled, and completed. Call the method for updating the status on the table, as follows:
/// <summary>
/// The MyWorkflowTypeEventHandler workflow event handler.
/// </summary>
public class MyWorkflowTypeEventHandler implements WorkflowCanceledEventHandler,
WorkflowCompletedEventHandler,
WorkflowStartedEventHandler
{
public void started(WorkflowEventArgs _workflowEventArgs)
{
// TODO: Write code to execute once the workflow is started.
MyWorkflowTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::Submit);
}
public void canceled(WorkflowEventArgs _workflowEventArgs)
{
// TODO: Write code to execute once the workflow is canceled.
MyWorkflowTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::Canceled);
}
public void completed(WorkflowEventArgs _workflowEventArgs)
{
// TODO: Write code to execute once the workflow is completed.
MyWorkflowTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::Complete);
}
}
Select Save all and then build the project again.
The system creates the workflow type. To set workflow properties, open MyWorkflowTableForm.
In FormDesign properties, locate and fill out the following elements:
Select Save all and then build the project again with synchronization turned on.
Your workflow type is now created.
After you create the workflow type, you can create workflow approval. To do so, make sure that you have the following prerequisites:
To add the preview field group to the table, follow these steps:
Now, you can create the workflow approval.
To create the workflow approval, follow these steps:
Right-click the project to open the context menu, and then select Add New Item.
Go to Business Process and Workflow, select Workflow Approval, and then enter the name MyWorkflowApproval.
Enter the following settings in the Workflow Approval wizard that opens:
Select Next. The Workflow Approval page should appear.
Select Next to have the system create all classes and menu items or select Cancel to close the window. The system adds the classes and menu items to your project, but you must modify it.
Open your workflow approval event handler class from the project MyWorkflowEventHandler.
For each status, update your table. The following code example calls the updateWorkflowStatus
method for each event to update its status in the table.
public void started(WorkflowElementEventArgs _workflowElementEventArgs)
{
// TODO: Write code to execute once the workflow is started.
MyWorkflowTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::Started);
}
public void canceled(WorkflowElementEventArgs _workflowElementEventArgs)
{
// TODO: Write code to execute once the workflow is canceled.
MyWorkflowTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::Canceled);
}
public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
{
// TODO: Write code to execute once the workflow is completed.
MyWorkflowTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::Complete);
}
public void denied(WorkflowElementEventArgs _workflowElementEventArgs)
{
// TODO: Write code to execute once the workflow is denied.
MyWorkflowTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::Denied);
}
public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
{
// TODO: Write code to execute once change is requested for the workflow.
MyWorkflowTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::ChangeRequested);
}
public void returned(WorkflowElementEventArgs _workflowElementEventArgs)
{
// TODO: Write code to execute once the workflow is returned.
MyWorkflowTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),MyWorkflowStatus::Returned);
}
public void created(WorkflowWorkItemsEventArgs _workflowWorkItemsEventArgs)
{
// TODO: Write code to execute once work items are created.
}
Select Save all.
For the workflow approval action menu items, you must set labels:
Point the workflow type to workflow approval by opening the workflow type (MyWorkflowType) in Visual Studio Designer. Go to and right-click Supported Elements to open the context menu, and then select New Workflow Element Reference.
In the Properties window, enter your workflow approval name (MyWorkflowApproval) in the Element Name and Name fields.
Go to the top node for MyWorkflowType, and in the Properties window, change the label to My workflow type.
Select Save all and then build the project with synchronize.
You’ve now completed all workflow development.
To test a workflow in finance and operations apps, you must first activate it.
Your newly created workflow belongs to the Inventory module. In Inventory Management > Setup > Inventory management workflows, set up your newly developed workflow:
Your workflow is now in use in finance and operations apps.
Having an issue? We can help!
Please sign in to use this experience.
Sign in