Saturday, 7 March 2020

Custom Workflow Example

Below are the steps to create a custom workflow for the custom form in D365.
1. Basing on the requirement create a new base enum or use the existing base enum
2. Add table level methods
-          canSubmitToWorkflow
-          updateWorkflowstatus
Below is the code
public boolean canSubmitToWorkflow(str _workflowType = '')
   {
        boolean ret;
ret = super(_workflowType);
if (this.WorkflowStatus == WorkflowStatus::NotSubmitted)
{
ret = boolean::true;
}
else
{
ret = boolean::false;
}

return ret;
   }

static void updateWorkflowStatus(RefRecId _recId, TradeWorkflowState _status)
    {
       LeaseTable leaseTable;

       select forupdate leaseTable
           where leaseTable.RecId == _recId;

       ttsBegin;
        leaseTable.WorkflowState = _status;
        leaseTable.update();
        ttsCommit;
    }
3.  Create query for the custom table, below are the screen shots
 
4. Create workflow category
 
Set the following properties for the “Work flow category”
  • Label – Give the label.
  • Module – Select the module (in which module this workflow category belongs).
5. Create work flow type
 
 Set the following while creating the workflow types.
  • Category – Name of the Workflow category Created in Step 4
  • Query – Name of the query created in step 3.
  • Document menu item – Name of display menu item for the form to enable Workflow on.
The following artifacts will be created:
  • Workflow Type
  • Classes
          i. Document class which extends WorkflowDocument.
          ii.EventHandler class which gives implementation to handle different workflow events.
          iii. SubmitManager class.
  • Action menu items:
           I. SubmitMenuItem pointing to SubmitManager class.
           II. CancelMenuItem pointing to WorkflowCancelManager class.
6. Add the following code "Work flow type" event handler
public class LeaseTableWFTypeEventHandler implements WorkflowCanceledEventHandler,
WorkflowCompletedEventHandler,
WorkflowStartedEventHandler
{
public void started(WorkflowEventArgs _workflowEventArgs)
{
LeaseTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),WorkflowStatus::Submitted);
}
public void canceled(WorkflowEventArgs _workflowEventArgs)
{
LeaseTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),WorkflowStatus::Submitted);
}
public void completed(WorkflowEventArgs _workflowEventArgs)
{
LeaseTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),WorkflowStatus::Submitted);
}
}
7. Enable Workflow on the custom form “LeaseTable” by setting Design node properties as follows:
  • Workflow Enabled – Yes (shown in the below shot).
  • WorkflowDatasource – Name of the form data source (shown in the below shot).
  • Workflow Type – Name of the custom Workflow Type created in step 5
   
 8. Add the below logic to submit manager class
/// <summary>
/// The TenancyAgreeWFTSubmitManager menu item action event handler.
/// </summary>
public class TenancyAgreeWFTSubmitManager
{
    
    public static void main(Args _args)
    {
         LeaseTable                       leaseTable;
        WorkflowComment          note = "";
        WorkflowSubmitDialog    workflowSubmitDialog;
        WorkflowCorrelationId     workflowCorrelationId;
         WorkflowTypeName        workflowTypeName = workFlowTypeStr("TenancyAgreementWFType");

        //Opens the submit to workflow dialog.
        workflowSubmitDialog = WorkflowSubmitDialog::construct(
        _args.caller().getActiveWorkflowConfiguration());
        workflowSubmitDialog.run();

        if (workflowSubmitDialog.parmIsClosedOK())
        {
            leaseTable = _args.record();
            // Get comments from the submit to workflow dialog.
            note = workflowSubmitDialog.parmWorkflowComment();

            try
            {
                       ttsbegin;
 workflowCorrelationId = Workflow::activateFromWorkflowType(workflowTypeName, leaseTable.RecId, note, NoYes::No);
                     leaseTable.WorkflowState = TradeWorkflowState::Submitted;
                     leaseTable.update();
                      ttscommit;

                // Send an Infolog message.
                info("Submitted to workflow.");
            }
            catch (Exception::Error)
            {
                error("Error on workflow activation.");
            }
        }

        _args.caller().updateWorkFlowControls();
    }

}

9. Create a Workflow Approval 
 
 
9. Add the following code to Work flow approval event handler
public final class LeaseTableWFApprEventHandler implements WorkflowElementCanceledEventHandler,
WorkflowElemChangeRequestedEventHandler,
WorkflowElementCompletedEventHandler,
WorkflowElementReturnedEventHandler,
WorkflowElementStartedEventHandler,
WorkflowElementDeniedEventHandler,
WorkflowWorkItemsCreatedEventHandler
{
public void started(WorkflowElementEventArgs _workflowElementEventArgs)
{
// TODO: Write code to execute once the workflow is started.
}
public void canceled(WorkflowElementEventArgs _workflowElementEventArgs)
{
LeaseTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), WorkflowStatus::Rejected);
}
public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
{
LeaseTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), WorkflowStatus::Rejected);
}
public void denied(WorkflowElementEventArgs _workflowElementEventArgs)
{
LeaseTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), WorkflowStatus::Rejected);
}
public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
{
// TODO: Write code to execute once change is requested for the workflow.
}
public void returned(WorkflowElementEventArgs _workflowElementEventArgs)
{
LeaseTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), WorkflowStatus::Rejected);
}
public void created(WorkflowWorkItemsEventArgs _workflowWorkItemsEventArgs)
{
// TODO: Write code to execute once work items are created.
}
}
 11. Drag the workflow approval created in step 8(TenancyAgreeAppr) to the supported elements node of the Workflow type created in the step 5(TenancyAgreeWFT).
12. Set the following properties for the menu item(i.e. AgreementALL) for which we trying to enable the workflow.
Set the following properties:
- EnumTypeParameter to ModuleAxapta.
- EnumParameter to “HousingMgnt”.
13. Set the following property for form Under the Main menu
 Display in content area - Yes

No comments:

Post a Comment

POSTMAN D365

  Postman is useful to test the behavior of different OData class from/to D365FO. In this post we will see the steps to setup Postman with D...