Custom Workflow Development for Existing Forms


                canSubmitToWorkflow is not working at all in D365FO extensions, but we can achieve this by creating custom workflow with new table which we will use sort of extension and associate the table with standard form extension.

Following are the steps to create custom work flow and associate with existing form.
Here I am created workflow for service order form this is located in service management module.
Step-1 – Create custom Enum or you can use existing.
      
 

Step-2 – Create custom table (Table name: SMJ_ServiceOrderWorkflow) and
add two fields Main field id and Status field extend both fields with appropriate Enum or EDTs.
1)      Main field is ServiceOrderID
2)      Status field is WorkflowStatus



Step-3 – Create relation on parent table and our newly created table on the basis of Main field id.















Step-4 – Create a Custom Query (Query name: SMJ_ServiceOrderWF) and

    Add the newly created table (Table name: SMJ_ServiceOrderWorkflow). 

     
Step-5 – Create workflow Type with the necessary datas
Step-6 – Create workflow Approval with the necessary datas.
Step-7 – Add the workflow approval to the Workflow type’s supported elements.
Step-8 – Override the canSubmitToWorkflow  on newly created table.
    /// <summary>
    ///
    /// </summary>
    /// <param name = "_workflowType"></param>
    /// <returns></returns>
    public boolean canSubmitToWorkflow(str _workflowType = '')
    {
        boolean ret;
        SMAServiceOrderLine ServiceOrderLine;
        select ServiceOrderLine where ServiceOrderLine.ServiceOrderId == this.ServiceOrderId;
        ret = this.RecId != 0 && this.WorkflowState == PurchReqWorkflowState::NotSubmitted && ServiceOrderLine;
        //ret = super(_workflowType);
        return ret;
    }

Step-9 – Create method in the table to update status on workflow events/approval.
/// <summary>
    ///
    /// </summary>
    public static void UpdateWorkflowState(RecId _recId, PurchReqWorkflowState _state)
    {
        SMJ_ServiceOrderWorkflow    ServiceOrderWorkflowStateUpdate; //= SMJ_PurchaseOrder::findByRecID(_recId,       true,ConcurrencyModel::Auto);
        select  forupdate ServiceOrderWorkflowStateUpdate where ServiceOrderWorkflowStateUpdate.RecId == _recId;
       
        if(ServiceOrderWorkflowStateUpdate)
        {
            ttsBegin;
            ServiceOrderWorkflowStateUpdate.WorkflowState = _state;
            ServiceOrderWorkflowStateUpdate.update();
            ttsCommit;
        }
    }

Step-10 –  Add proper labels on workflow types, approval and menu items.

Step-11 – Build the model and perform the database sync.

Step-12 – Go to your module and create a new workflow. (service management)

Step-13 – Configure workflow as per your requirement and activate.

Step-14 – Create extension of the form on which you want to associate workflow.
         (extension of the form name is: SMAServiceOrderTable.WFExtension)

Step-15 – Add data source table and join with parent data source.


Step-17 –  
  • Added Workflow data-source ,
  • enable the workflow, and 
  • add workflow type.

 

Step-18 – Now override the parent table Method OnWritten and insert the record on our newly added data-source (SMJ_ServiceOrderWorkflow).as well. Whenever user create or update parent table.

[FormDataSourceEventHandler(formDataSourceStr(SMAServiceOrderTable, SMAServiceOrderTable), FormDataSourceEventType::Written)]
    public static void SMAServiceOrderTable_OnWritten(FormDataSource sender, FormDataSourceEventArgs e)
    {
        SMAServiceOrderTable sMAServiceOrderTable = sender.cursor();
        FormDataSource fds = sender.formRun().dataSource(formDataSourceStr(SMAServiceOrderTable, SMJ_ServiceOrderWorkflow));
        fds.object(fieldnum(SMJ_ServiceOrderWorkflow, ServiceOrderId)).setValue(sMAServiceOrderTable.ServiceOrderId);
        fds.object(fieldnum(SMJ_ServiceOrderWorkflow, WorkflowState)).setValue(PurchReqWorkflowState::NotSubmitted);
        fds.write();
    }
Step-19 – Buid the model and you can verify with the new record.


Comments

Post a Comment

Popular posts from this blog

SSRS Report using Controller , Contract and RDP classes in D365

Exporting data to Excel through X++ code

COC for Form level method

How to pass the parameter from one form to another in Dynamic365

Multi Select Lookup in SSRS Report in D365