workflow
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
- BaseEnum:
Create “CustWFApprovalStatus” base enum for the workflow approval status
- Not Submitted
- Submitted
- Pending Approval
- Change Request
- Approved
- Reject
- Create new Field on CustTable:
Create Enum type field ‘WorkflowApprovalStatus ‘ on CustTable and assign CustWFApprovalStatus to enumtype
- 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;
}
- Query:
Here we use CustTableListPage query. Hence no need to create new query. Add WorkflowApprovalStatus field on query to display on customer list page.
- Workflow Category:
Here we use CustCategory as it has customer module attached with it.
- Workflow Type:
- In the AOT, expand the Workflow node.
- 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.
- Click Next.
- 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();
}
- Workflow Approval
- Open the AOT.
- Expand the Workflow node.
- Right-click on Approvals and select Add-ins > Approval wizard.
- 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
- Drag workflow approval to workflow type
Drag workflow approval to workflow type under CustAprWorkflowType > Supported elements
- 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.
for workflow on Dynamics 365 F&O click here
This helped you..!! comment it below to encourage for more posts like this..:)
Hello! I want to say thank you for such a useful and informative tutorial and clear explanations, you explained everything in detail step by step and it helped me to understand the mechanism itself. It turns out that everything is easy and simple to do :)
ReplyDeleteIt's really wonderful explanation.Thank you so much
Deleteshow!
ReplyDeleteImpressive indeed
ReplyDeletethe workflow doesn't show in the form :(
ReplyDeleteHave you override canSubmitToWorkflow method on the table as mentioned here in step 3?
DeleteThank you very much. It's works. Love it
ReplyDelete