Custom WorkFlow ex: Leave Submit & Approval in D365 fo


 

1. create a new base enum(YajLeaveWorkflowStatus or use the existing base enum 

Add elements to Enum
i. Not Submitted
ii. Submitted
iii. Approved

2. Create a new table (Ex: YajLeaveTable)
Add 2 or more Fields.
  i. LeaveId (String)
  ii. LeaveWorkFlowStatus (Drag and Drop Above created BaseEnum (YajLeaveWorkflowStatus)
  iii. Create field group (LeaveGroup) and add two fields
  iv. Create Two methods as follows
a. canSubmitToWorkflow
b. updateWorkflowStatus

public boolean canSubmitToWorkflow(str _workflowType = '')

   {
       boolean ret;
       ret = super(_workflowType);
       if (this.LeaveWorkflowStatus == YajLeaveWorkflowStatus::NotSubmitted)
       {
           ret = boolean::true;
       }
       else
       {
           ret = boolean::false;
       }
       return ret;
   }

static void updateWorkflowStatus(RefRecId _recId, YajLeaveWorkflowStatus _status)
   {
       YajLeaveTable yajLeaveTable;
       select forupdate yajLeaveTable
          where yajLeaveTable.RecId == _recId;
       ttsBegin;
       yajLeaveTable.LeaveWorkflowStatus = _status;
       yajLeaveTable.update();
       ttsCommit;
   }


3. Create a new Query (Ex: YajLeaveQuery) Add above table(YajLeaveTable) and datasource and its Table property Dynamics fields: Yes




4. Create a new WorkflowCategory(Ex: YajLeaveWorkflowCategory) 

Set the following properties for the “Work flow category”
Label – Give the label.
Module – Select the module (in which module this workflow category belongs Ex: HumanResource).



5. Create a new Form (YajLeaveForm)  and Add Datasource as Table(YajLeaveTable).

i. Form pattern: Simplelist
* Action Pane
* Group with Quick filter control
* Grid

6. Create a new Display menu Item for above Form (YajLeaveDisplayMenuItem) 


Add the properties options as Below image
Object type : Form
Object : YajLeaveForm
Enum Type Parameter : YajLeaveWorkflowStatus  (Base Enum created in Point no .1)
Enum Parameter : Submitted


7. Create a new Workflow Type((YajLeaveWorkflowType) 

Choose Category: (Point no. 4)
Query : (Point no. 3)
Menu item : (Point no. 6)


 It Create Multiple objects like Workflow type EventHandler & workflow type submit manager etc.


update following code to YajLeaveWorkflowTypeEventHandler 

public class  YajLeaveWorkflowTypeEventHandler implements WorkflowCanceledEventHandler,  

       WorkflowCompletedEventHandler,
       WorkflowStartedEventHandler
{
   public void started(WorkflowEventArgs _workflowEventArgs)
       {
               // TODO:  Write code to execute once the workflow is started.
       YajLeaveTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),YajLeaveWorkflowStatus::Submitted);
       }
 
   public void canceled(WorkflowEventArgs _workflowEventArgs)
       {
               // TODO:  Write code to execute once the workflow is canceled.
       YajLeaveTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),YajLeaveWorkflowStatus::Submitted);
       }
 
   public void completed(WorkflowEventArgs _workflowEventArgs)
       {
               // TODO:  Write code to execute once the workflow is completed.
       YajLeaveTable::updateWorkflowStatus(_workflowEventArgs.parmWorkflowContext().parmRecId(),YajLeaveWorkflowStatus::Submitted);
       }
}


update following code to YajLeaveWorkflowTypeSubmitManager

public class YajLeaveWorkflowTypeSubmitManager
{
   public static void main(Args args)
       {
               //  TODO:  Write code to execute once a work item is submitted.
       YajLeaveTable           yajLeaveTable;
       WorkflowComment         note = "";
       WorkflowSubmitDialog    workflowSubmitDialog;
       WorkflowCorrelationId   workflowCorrelationId;
       WorkflowTypeName        workflowTypeName = workFlowTypeStr("YajLeaveWorkflowType");
       //Opens the submit to workflow dialog.
       workflowSubmitDialog = WorkflowSubmitDialog::construct(args.caller().getActiveWorkflowConfiguration());
       workflowSubmitDialog.run();
       if (workflowSubmitDialog.parmIsClosedOK())
       {
           YajLeaveTable = args.record();
           // Get comments from the submit to workflow dialog.
           note = workflowSubmitDialog.parmWorkflowComment();
           try
           {
               ttsbegin;
               workflowCorrelationId = Workflow::activateFromWorkflowType(workflowTypeName, YajLeaveTable.RecId, note, NoYes::No);
               YajLeaveTable.LeaveWorkflowStatus = YajLeaveWorkflowStatus::Submitted;
               YajLeaveTable.update();
               ttscommit;
               // Send an Infolog message.
               info("Submitted to workflow.");
           }
           catch (Exception::Error)
           {
               error("Error on workflow activation.");
           }
       }
       args.caller().updateWorkFlowControls();
       }
}


Note:  Build the project now.

8. Create workflow Approval (YajLeaveWorkflowApproval) 



Choose Workflow Document: (Created during Workflowtype creation Point no. 7)
Field Group (YajLeaveTable field group: (Point no. 2)
Menu item : (Point no. 6)


It creates multiple Objects like WorkflowApprovalEventHandler etc.


Change the Label for all menu items created here as Submitted, Approved, Reject etc. to appear at Workflow MenuItem in D365fo frontEnd.

update following code to YajLeaveWorkflowApprovalEventHandler

public final class YajLeaveWorkflowApprovalEventHandler 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)
       {
               // TODO:  Write code to execute once the workflow is canceled.
       YajLeaveTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), YajLeaveWorkflowStatus::Submitted);
       }
 
   public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
       {
               // TODO:  Write code to execute once the workflow is completed.
       YajLeaveTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), YajLeaveWorkflowStatus::Submitted);
       }
 
   public void denied(WorkflowElementEventArgs _workflowElementEventArgs)
       {
               // TODO:  Write code to execute once the workflow is denied.
       YajLeaveTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), YajLeaveWorkflowStatus::Submitted);
       }
 
   public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
       {
               // TODO:  Write code to execute once change is requested for the workflow.
       }
 
   public void returned(WorkflowElementEventArgs _workflowElementEventArgs)
       {
               // TODO:  Write code to execute once the workflow is returned.
       YajLeaveTable::updateWorkflowStatus(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), YajLeaveWorkflowStatus::Submitted);
       }
 
   public void created(WorkflowWorkItemsEventArgs _workflowWorkItemsEventArgs)
       {
               // TODO:  Write code to execute once work items are created.
       }
}


9. Now Goto Workflow Type (YajLeaveWorkflowType)

Create New Workflow Element Reference and Give Element Name as Workflow Approval(YajLeaveWorkflowApproval Created in Point no. 8)


10. Extend and add to project HRM(Human Resource Menu)  for Add Display menu Item(YajLeave DisplayMenuItem Point no. 6) 
       in Workers Group or somewhere Drag and Drop Menu item from project
Note: If HRM CreateExtension not enable Update PersonnelManagement to current Model)

And change the Properties as above screen

----------------------------------------------------------------------------------------------------------------------------

in D365FO FrontEnd : choose Module : Human resource  --> Human Resource Workflow 





Note: If you can't see Workflow Approval(YajLeaveWorkflowApproval) in Approvals at left side  (Check Point No. 9)










-------------------------------------------------------------


Now goto HumanResource(HRM) module --> Workers Group --> Click Menu Item (leave form we created to open form) 


Create some records as with Leave status as Not submitted.





Refresh the page and wait a while for net step.

choose the record which showing Submitted and make it approve from workflow menu.







Table Browser in Dynamics 365FO

Copy D365FO Table Browser URL /?cmp=usmf&mi=SysTableBrowser&tablename= URL :  https://usnconeboxax1aos.cloud.onebox.dynamics.com...