Customization in New Dynamics Ax (Aka AX 7)- Part 6 Custom Workflow

https://www.tech.alirazazaidi.com/customization-in-new-dynamics-ax-aka-ax-7-part-6-custom-workflow/

Customization in New Dynamics Ax (Aka AX 7)- Part 6 Custom Workflow

Hi, every one, Let’s explore the custom workflows in new Dynamics Ax commonly known as AX 7.
This blog post is part of series.
In this post, I build a custom workflow based on table and form build in pervious posts.
Below links leads you to older posts.







As per Microsoft Documentation Custom workflow development is very similar to Dynamics Ax 2012.

Let’s do this in in following steps.
Custom Enum type.

We need Custom enum type for defining the States or status of Workflow. In Current Example I am using Al as naming convention for development.

Right click on Solution explorer and then add a new from ax artifacts. Name it AlDocumentWorkflowState.
new-enumtype
When new AlDocumentWorkflowState enum is added in Solution explorer then double click on it to open it in designer mode. And then Add following Elements in it.
Add following elements in Enum
2016-09-20_22-56-56

Now drag this enum to required table and it will create a new field other wise you have to create a new field in table and set extended data type / base enum. For current example I used the AlEarningCodeGroup table which I created in my previous post.
workflowdrag


Table methods:
We need two methods in Table, first method is CanSubmitToWorkFlow.  This table help us to enable workflow based on specific condition, For example if table row has workflow state draft or not submitted, then workflow will processing will enable.
canworkflow

A new class is added in and update with logic, The condition on which Workflow enabled in class
public boolean canSubmitToWorkflow(str _workflowType = '')

{

boolean ret;

ret = super(_workflowType);

if (this.AlDocumentWorkflowState  == AlDocumentWorkflowState::NotSubmitted)

{

ret = boolean::true;

}

else

{

ret = boolean::false;

}

return ret;

}


Now add one more method in class We pass recId of current row and update its workflow state.
Public static void UpdateWorkFlow(RefRecId _RecId,AlDocumentWorkflowState _Status)
{
AlEarningCodeGroup _Group;
select forupdate * _Group where _Group.RecId == _RecId;
ttsbegin;
_Group.AlDocumentWorkflowState = _Status;
_Group.update();
ttscommit;

}

Custom Query:
Workflow uses the query to define the tables and fields that will be used by the workflow to define the data set available.
Again right click on solution explorer and add new Item and then select Query.
Now create a Query so we can build Document on that Query.
newquery


Name it AlEarningCodeGroupQuery .
Double click on Query and open it into Design mode and drag table in Data source. Save
querydetail
Now right click on Table and press Now F4 or open property window and set dynamics Field
fields
You find fields details.
query-building

Workflow Category:
A workflow category is used to determine the module in which the workflow will be available
Right click on project in solution explorer and add new Item.  From dialog window select workflow category.
workflowcategorydailog


Double click on workflow category created in previous step and open in designer view, press F4   to property window. set Module Property to HumanResource and Label to “AlEarningcodeGroupWFCategory” and save it.
categoryproperty



Workflow type.
Now to go to Solution explorer and add new item and from new Item dialog select
earncodetypewf

Name it AlEarningGrpWFType. A wizard will start and there you have to select Query , Workflow category and the link from which required form will be open. We build all these three things in previous steps.
ddde
When Wizard finish, number of classes, Workflow type and classes will be generated and added in Visual studio, you can find them in solution explorer.
menuee


Update label of workflow type open them in designer window and set its label.
earning-code-type
Similarly update labels of menu items to something good.
cancel

Similarly update the label of submit button.

Now expand the Submit Manager Class “AlEarnGrpWFTypeSubmitManager”  and update event handlers as follow.

/// <summary>
/// The AlEarnGrpWFTypeSubmitManager menu item action event handler.
/// </summary>
public class AlEarnGrpWFTypeSubmitManager
{
public static void main(Args _args)
{
AlEarningCodeGroup                           EarningCodeGroup;
AlEarnGrpWFTypeSubmitManager     submitManger;
recId _recId =                      _args.record().RecId;
WorkflowCorrelationId               _workflowCorrelationId;


workflowTypeName                    _workflowTypeName = workFlowTypeStr(“AlEarnGrpWFType”);
WorkflowComment                     note = “”;
WorkflowSubmitDialog                workflowSubmitDialog;
submitManger =                      new AlEarnGrpWFTypeSubmitManager();





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


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


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


EarningCodeGroup.AlDocumentWorkflowState = AlDocumentWorkflowState::Submitted;
EarningCodeGroup.update();
ttscommit;


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


_args.caller().updateWorkFlowControls();
}

}

Now double click on event class “AlEarnGrpWFTypeEventHandler”  in solution explorer edit its events with table method call, We create this static method in one of above step.
/// <summary>
/// The AlEarnGrpWFTypeEventHandler workflow event handler.
/// </summary>
public class  AlEarnGrpWFTypeEventHandler implements WorkflowCanceledEventHandler,
WorkflowCompletedEventHandler,
WorkflowStartedEventHandler
{
public void started(WorkflowEventArgs _workflowEventArgs)
{
AlEarningCodeGroup::UpdateWorkFlow(_workflowEventArgs.parmWorkflowContext().parmRecId(),AlDocumentWorkflowState::Submitted);
}

public void canceled(WorkflowEventArgs _workflowEventArgs)
{
AlEarningCodeGroup::UpdateWorkFlow(_workflowEventArgs.parmWorkflowContext().parmRecId(),AlDocumentWorkflowState::PendingConcellation);
}

public void completed(WorkflowEventArgs _workflowEventArgs)
{
AlEarningCodeGroup::UpdateWorkFlow(_workflowEventArgs.parmWorkflowContext().parmRecId(),AlDocumentWorkflowState::Completed);
}

}

Workflow Approval:


Add new item and select Workflow Approval like in previous steps
approval

In wizard select Workflow document this will be generated by Workflow element step. Also select the field group from table on which we are building workflow. Also select form menu item which will open when user click on notification.

earaninggroupcodeapprovalwizard

earninggroupapprovalwizarddetails




In Solution explorer new menu items and classes are added.
new-items


Now double click and open the approval class and update it as AlEarningGroupWFApprEventHandler
And update workflow dates

/// <summary>
/// The AlEarningGroupWFApprEventHandler workflow outcome event handler.
/// </summary>
public final class AlEarningGroupWFApprEventHandler implements WorkflowElementCanceledEventHandler,
WorkflowElemChangeRequestedEventHandler,
WorkflowElementCompletedEventHandler,
WorkflowElementReturnedEventHandler,
WorkflowElementStartedEventHandler,
WorkflowElementDeniedEventHandler,
WorkflowWorkItemsCreatedEventHandler
{
public void started(WorkflowElementEventArgs _workflowElementEventArgs)
{

}

public void canceled(WorkflowElementEventArgs _workflowElementEventArgs)
{
AlEarningCodeGroup::UpdateWorkFlow(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), AlDocumentWorkflowState::PendingConcellation);
}

public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
{
AlEarningCodeGroup::UpdateWorkFlow(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), AlDocumentWorkflowState::Approved);
}

public void denied(WorkflowElementEventArgs _workflowElementEventArgs)
{
AlEarningCodeGroup::UpdateWorkFlow(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), AlDocumentWorkflowState::Reject);
}

public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
{
AlEarningCodeGroup::UpdateWorkFlow(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), AlDocumentWorkflowState::ChangeRequest);
}

public void returned(WorkflowElementEventArgs _workflowElementEventArgs)
{
AlEarningCodeGroup::UpdateWorkFlow(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), AlDocumentWorkflowState::Returned);
}

public void created(WorkflowWorkItemsEventArgs _workflowWorkItemsEventArgs)
{
// TODO:  Write code to execute once work items are created.
}

}

Now double click on workflow type and open in designer
workflow-type

workflowapproval

Update it workflow approval with name
approva
Form Properties to enable Workflow.

Now expand the form, double click on designer and set following properties.

Workflow data source, enable for workflow and workflow Type.
worktypedetial
Now save Compile And Run with Ctrl F5 to run without debugging to open Dynamics Ax web client
workflow-setup

Now we have to configure workflow.
We set workflow category is Human Resource Management, and you will find Human resource workflows
setupworkflow

Create a new workflow and select the workflow type we created in above step.
login

Enter credentials.



From designer window select and configure workflow.
setup

Save and activate.

Now when you open the client in browser. You will find workflow menu item you can run the workflow from here.

workflow-setss

Comments

Popular posts from this blog

AX7/D365/Operations: Enable/Disable form control in X++

AX 2012: Multi-Select Lookup for SSRS Report Dialog

Credit limit check on Sales Quotation based on the customer credit limit in D365 FO of AX7