Create custom workflow D365 finance & Operations
Enum
add field on the table
query
workflow category
workflow type
workflow Approval
add workflow approval to workflow type
Enable workflow on form
Create workflow
Create Enum
Create new enum for the workflow status
Create a new field on the table.
Here we use a table SMAAgreementTable. Create extension of table and Drag enum to table.
Create methods on table
Create a new class. Name it “devSMAAgreementTable_Extension”
Extend for table “SMAAgreementTable”.
Create methods
canSubmitToWorkflow
UpdateCustWorkflowState
“canSubmitToWorkflow” method will be using a chain of command. (use of next keyword)
public boolean canSubmitToWorkflow(str _workflowType)
{
boolean ret = next canSubmitToWorkflow(_workflowType);
ret = this.RecId != 0 && this.workflowStatus == workflowstatus::Draft;
return ret;
}
Create new query
Here we use SMAAgreementTableListPage query
Create workflow category
Click on Add > New item > Business process and workflow > Workflow category
Enter proper name
Set property as below.
Label = “Service agreement workflow category”
Module = Salesorder (as of now we add workflow to AR. So workflow will list under AR module)
Create workflow type
Create new workflow type as below:
Click on Add > New item > Business process and workflow > Workflow type
Edit devServiceAgreementWFTypeEventHandler class as 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 code on 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
Click on Add > New item > Business process and workflow > Workflow Approval
Update 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 approval supported element on workflow type under devServiceAgreementWFType > Supported elements
Enable workflow on form
Create workflow
Create workflow from AR > AR workflow.
Create service agreement and verify