How to Create custom workflow in D365 finance & Operations Project
Workflows are designed to streamline business processes while giving clear instructions for documents to flow, and for the users who need to approve it. Since there aren’t any existing workflows available for service agreements, creating a custom workflow for your Microsoft Dynamics365 Finance and Operatons Project is your best bet here.
This guide will explain every step needed to create your custom workflow in Dynamics 365.
Create Enum
The base enumeration is used to define the status of the workflow. Here are the steps needed to define it:
– Create a new Enum for the workflow status
Create A New Field on the Table
– Here, we use the table SMAAgreementTable as an example. Create an extension of the table and drag Enum to table.
Create Methods on the Table
Next, you will be required to create methods on the table using the following steps:
- Create a new class and name it “devSMAAgreementTable_Extension”
- Extend the table “SMAAgreementTable”.
- Create methods with the following names
- canSubmitToWorkflow
- UpdateCustWorkflowState
- The “canSubmitToWorkflow” method will use a chain of command for the next keyword.
public boolean canSubmitToWorkflow(str _workflowType)
{
boolean ret = next canSubmitToWorkflow(_workflowType);
ret = this.RecId != 0 && this.workflowStatus == workflowstatus::Draft;
return ret;
}
Create a New Query
Creating a new query is easy:
- Here we use the SMAAgreementTableListPage query
Create Workflow Category
- Click on Add > New Item > Business Process and Workflow > Workflow Category
- Enter a proper name
- Set the properties as given below
- Label = “Service agreement workflow category”
- Module = Salesorder (as of now we will add workflow to AR. So, the workflow will list under the AR module)
Create Workflow Type
- Create a new workflow type with the following steps:
- Click on Add > New Item > Business Process and Workflow > Workflow Type
– Edit the devServiceAgreementWFTypeEventHandler class as given below
public void started(WorkflowEventArgs _workflowEventArgs)
{
SMAAgreementTable::UpdateCustWorkflowState(_workflowEventArgs.parmWorkflowContext().parmRecId(), workflowstatus::Pending);
}
public void canceled(WorkflowEventArgs _workflowEventArgs)
{
SMAAgreementTable::UpdateCustWorkflowState(_workflowEventArgs.parmWorkflowContext().parmRecId(), workflowstatus::Cancel);
}
public void completed(WorkflowEventArgs _workflowEventArgs)
{
SMAAgreementTable::UpdateCustWorkflowState(_workflowEventArgs.parmWorkflowContext().parmRecId(), workflowstatus::Completed);
}
– Write the following code on the submit manager class
public static void main(Args _args)
{
SMAAgreementTable ObjSMAAgreementTable;
devServiceAgreementWFTypeSubmitManager submitManger = new devServiceAgreementWFTypeSubmitManager();
recId _recId = _args.record().RecId;
WorkflowCorrelationId _workflowCorrelationId;
workflowTypeName _workflowTypeName = workFlowTypeStr("devServiceAgreementWFType");
WorkflowComment note = "";
WorkflowSubmitDialog workflowSubmitDialog;
//Opens the submit to workflow dialog.
workflowSubmitDialog = WorkflowSubmitDialog::construct(_args.caller().getActiveWorkflowConfiguration());
workflowSubmitDialog.run();
if (workflowSubmitDialog.parmIsClosedOK())
{
ObjSMAAgreementTable = _args.record();
// Get comments from the submit to workflow dialog.
note = workflowSubmitDialog.parmWorkflowComment();
try
{
ttsbegin;
// Activate the workflow.
_workflowCorrelationId = Workflow::activateFromWorkflowType(_workflowTypeName, ObjSMAAgreementTable.RecId, note, NoYes::No);
ObjSMAAgreementTable.workflowstatus = workflowstatus::Submitted;
ObjSMAAgreementTable.update();
ttscommit;
// Send an Infolog message.
info("Submitted to workflow.");
}
catch (Exception::Error)
{
error("Error on workflow activation.");
}
}
_args.caller().updateWorkFlowControls();
}
Create Workflow Approval
– To create a workflow approval, simply click on Add > New item > Business process and workflow > Workflow Approval
– Update the devServiceAgreementWFApprovalEventHandler eventhandler class
public void started(WorkflowElementEventArgs _workflowElementEventArgs)
{
SMAAgreementTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),workflowstatus::Submitted);
}
public void canceled(WorkflowElementEventArgs _workflowElementEventArgs)
{
SMAAgreementTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),workflowstatus::Cancel);
}
public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
{
SMAAgreementTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),workflowstatus::Approved);
}
public void denied(WorkflowElementEventArgs _workflowElementEventArgs)
{
SMAAgreementTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),workflowstatus::Rejected);
}
public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
{
SMAAgreementTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),workflowstatus::ChangeRequest);
}
Add Workflow Approval to Workflow Type
– Create an approval supported element on workflow type under devServiceAgreementWFType > Supported elements
– Enable Workflow on Form
– Create Workflow
Wrapping Up
We hope that this tutorial on Creating Custom Workflow Activity Dynamics 365 was helpful and easy to follow. If you still have a few questions about the process, or any other questions relating to your D365 project, please feel free to contact our team of DEV IT experts today. They will help you get the best out of your development process in D365.