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 CustAprWorkflowTypeSubmitManag er class on workflow type project
public static void main(Args _args)
{
// Variable declaration.
CustTable CustTable;
recId _recId = _args. record().RecId;
WorkflowCorrelationId _ workflowCorrelationId;
workflowTypeName _ workflowTypeName = workFlowTypeStr(" CustAprWorkflowType");
WorkflowComment note = "";
WorkflowSubmitDialog workflowSubmitDialog;
submitManger = new CustAprWorkflowTypeSubmitManag er();
//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 CustApprWorkflowApprEventHandl er 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
- CustApprWorkflowApprRequestCha
nge 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.
No comments:
Post a Comment