Sunday, 25 September 2016

WORKFLOW From Scratch

Here we work on how to create custom workflow using x++ dynamics ax 2012. It’s simple and easy to do with the following steps in dynamics ax 2012.


The following steps shows to create custom workflow for new created customer approval. Below are the objects needs to be created:
  • Base Enum
  • WorkflowApprovalStatus field
  • Method on CustTable
  • Query
  • Workflow category
  • Workflow type
  • Workflow approval
  • Drag workflow approval to workflow type
  • Enable workflow on form


  1. BaseEnum:
Create “CustWFApprovalStatus” base enum for the workflow approval status
  • Not Submitted
  • Submitted
  • Pending Approval
  • Change Request
  • Approved
  • Reject




  1. Create new Field on CustTable:
Create Enum type field ‘WorkflowApprovalStatus ‘ on CustTable and assign CustWFApprovalStatus to enumtype


  1. Override Method on CustTable:
Override canSubmitToWorkflow method on CustTable
public boolean canSubmitToWorkflow(str _workflowType = '')
{    
   boolean ret;


   ret = this.RecId != 0 && this.WorkflowApprovalStatus == CustWFApprovalStatus::NotSubmitted;


   return ret;
}


New Method on CustTable:
Create new method UpdateCustWorkflowState on CustTable to update status


public static void UpdateCustWorkflowState(RefRecId _recId, CustWFApprovalStatus _state)
{
   CustTable custTable = CustTable::findRecId(_recId, true);


   ttsBegin;


   custTable.WorkflowApprovalStatus = _state;
   custTable.update();


   ttsCommit;
}


  1. Query:
Here we use CustTableListPage query. Hence no need to create new query. Add WorkflowApprovalStatus field on query to display on customer list page.


  1. Workflow Category:
Here we use CustCategory as it has customer module attached with it.


  1. Workflow Type:

  1. In the AOT, expand the Workflow node.
  2. Right-click the Workflow Types node, and then click Add-Ins > Workflow type wizard. The Workflow wizard is displayed. This wizard will help you create a new workflow type.
  3. Click Next.
  4. Set the following values for the wizard.


This will create a private project as below:


After the workflow type is created, you will add code for the workflow events.
Add below code to the CustAprWorkflowTypeSubmitManager class on workflow type project


public static void main(Args _args)
{
    // Variable declaration.
   CustTable                           CustTable;
   CustAprWorkflowTypeSubmitManager    submitManger;   
   recId _recId =                      _args.record().RecId;
   WorkflowCorrelationId               _workflowCorrelationId;


   workflowTypeName                    _workflowTypeName = workFlowTypeStr("CustAprWorkflowType");
   WorkflowComment                     note = "";
   WorkflowSubmitDialog                workflowSubmitDialog;
   submitManger =                      new CustAprWorkflowTypeSubmitManager();
   
   
   


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


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


       try
       {
           ttsbegin;
           // Activate the workflow.
           _workflowCorrelationId = Workflow::activateFromWorkflowType(_workflowTypeName, CustTable.RecId, note, NoYes::No);


           CustTable.WorkflowApprovalStatus = CustWFApprovalStatus::Submitted;
           CustTable.update();
           ttscommit;


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


   _args.caller().updateWorkFlowControls();
   
}


  1. Workflow Approval

    1. Open the AOT.
    2. Expand the Workflow node.
    3. Right-click on Approvals and select Add-ins > Approval wizard.
    4. Click Next.




This wizard will create the private project as below:




Now, Update code on CustApprWorkflowApprEventHandler for different events
public void started(WorkflowElementEventArgs _workflowElementEventArgs)
{
   CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),CustWFApprovalStatus::Submitted);
}


public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
{
   CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), CustWFApprovalStatus::Approved);
}


public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
{
   CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),  CustWFApprovalStatus::ChangeRequest);
}


public void canceled(WorkflowElementEventArgs _workflowElementEventArgs)
{
   CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),  CustWFApprovalStatus::Rejected);
}


Update Lables on Menuitems


  • CustApprWorkflowApprApprove  set label as Approve
  • CustApprWorkflowApprReject set label as Reject
  • CustApprWorkflowApprRequestChange set label as Request change


  1. Drag workflow approval to workflow type


Drag workflow approval to workflow type under CustAprWorkflowType > Supported elements


  1. Enable workflow on form


  • Go to AOT > Forms > CustTableListPage
  • Go to Designs > Design
  • Right click > Properties






That’s it now everything is setup and ready to go. But before start do incremental CIL and this is must. Every time you change the code, CIL is required. 

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...