Showing posts with label workflow ax2012. Show all posts
Showing posts with label workflow ax2012. Show all posts

Tuesday, 26 April 2016

Creating Custom Workflow from scratch in Dynamics Ax 2012

 Suppose we are working in HR module and client wants that worker will add their expense and send for Approval.

For this requirement, first we create a custom table and create its relation with Worker table. And then develop a custom workflow on it.
So this post contains tasks
  • Table creation and relation with worker
  • Create a simple entry form and list page.
  • Workflow



So first we create a new table and generate its relation with Worker ID.

If we open HCMworker table we will find that its primary key is “PersonnelNumber”.

We add new enum type which has expected values, medical, Certification, Food expense and others. We use Tst as extension for our example’s artifacts.
Expense

Now create a new table set its Name as tstWorkerExpenseTable.
Expand table node and right click on relation node. Create a new and set table as HCMWorker.

Rightlick on it new => foreignKey=>Primarykey.
HcmWorker
You will find new and new field added, you can rename it, if you check the properties you will the  which will int64 bit.

Now drag and drop enum created in previous step
Rename it to Expense type.


Now add one more field name It Amount of real type and set its label as Amount
Save It. Also create a field group with overview and drag all fields in that group.
Table final structure will be as follow.
Table Final stage


Form create simple form with Name tstWorkerExpense and set its data source as
Now create a simple form and set its datasource as TstWorkerExpense. And set data source properties  “Edit” and “insert if empty” to no.

Add grid on form and drag and drop fields from data source to grid. Save it.  Form structure will be look like.
WorkerListPage

Run the form you will find something like. You can improve from. It is enough for our current example
Also insert command buttons on action table for new, Edit and delete.
CreateForm


You can improve but for current example it is enough.

Now create a new enum  for which will used as approval status for table.
WorkFlowStatus
Drag and drop in fields of tstWorkerExpense. Save table, compile and synchronize, so this will be reflect at SQL Server level.

Now Its time to write some code that will work for state change in workflow steps.
Right click on Method node on tstWorkerExpense and click on CanSubmitToWorkFlow
CanWorkflow




Update it as follow.

publicboolean canSubmitToWorkflow(str _workflowType = ”)
{
boolean ret;

if (this.ExpenseStatus ==tstExpenseStatus::NotSubmit)
{

ret = boolean::true;
}
else
{
ret =boolean::false;
}

return ret;
}

New add a new static method with Name “updateworkFlow” and update it as follow

publicstaticvoid updateWorkFlowState(RefRecId _id, tstExpenseStatus _status)
{
tstWorkerExpense _Expense;

selectforUpdate _Expense where _Expense.RecId ==_id;
if (_Expense !=null)
{
ttsBegin;
_Expense.ExpenseStatus = _status;
_Expense.update();
ttsCommit;
}
}

 

Now create a Query and This query will later used when we build Workflow type on it.
Query
Add TstWorkerExpense table in it and set field  dynamic to yes.

WorkflowQuery
Now expand AOT and expand workflow right click on new Workflow Category
Custom Cateogry
Set its name tstWorkFlowCategory and set Module as HumanResource.
CategoryDetail
Save it. Now add a new me display menu Item With name “tstExpenseTable” and set its form tstWorkerExpenseTable.
Menu Item



Now expand workflow and then expand workflow Type right click and run the wizard.
WorkFlowType Wizard
Wizard1


From next window set following properties all based on artifacts we create in pervious steps.
ExpenseWizard
Ie.  Query, workflow category and menu item.  AS we test this workflow only on Ax client so check on rich client
TypeWizardFinished
Click on next.


In result a new Ax Project is created
TypeProject
  • Workflow Type
  • Classes
    • Document class which extends WorkflowDocument.
    • EventHandler class which gives implementation to handle different workflow events.
    • SubmitManager class.
  • Action menu items:
    • SubmitMenuItem pointing to SubmitManager class.
    • CancelMenuItem pointing to WorkflowCancelManager class.


Now expand the form (We created this in one above step) and expand its design, and set following properties  to it will workflow enable
WorkflowEnabled =yes
WorkflowDataSorce =tstWorkFlowExpense
WorkflowType = tstWorkerEpense (We created this in pervious step).
WorkFlowSettings

Now expand submit manager class in workflow type project and update logic as follow.
And Create a new method with following logic.

public void submit(Args _args)
{
tstWorkerExpense        workerExpense;
WorkflowComment         note = “”;
WorkflowSubmitDialog    workflowSubmitDialog;

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

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

try
{
ttsbegin;
workerExpense.ExpenseStatus  = tstExpenseStatus::Submit;
ttscommit;

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

_args.caller().updateWorkFlowControls();
}

public static void main(Args args)
{
tstWorkerExpenseSubmitManager submitManager = new tstWorkerExpenseSubmitManager();
submitManager.submit(args);

}

Now Create a we create workflow approval.
Expand Workflow node in AOT and then again expand Workflow approval.
Approval


Now you have to use the following artifacts we created in previous steps.

Document, which we created through workflow type wizard and tables field group. And
ApprovalWithDocument


Again new project created.
New Approval Project is created

Now expand TsWorkerExpenseAppEventHandler and update following methods.
public void returned(WorkflowElementEventArgs _workflowElementEventArgs)
{
tstWorkerExpense::updateWorkFlowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), tstExpenseStatus::ChangeRequest);
}


public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
{
tstWorkerExpense::updateWorkFlowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), tstExpenseStatus::ChangeRequest);
}

public void denied(WorkflowElementEventArgs _workflowElementEventArgs)
{
tstWorkerExpense::updateWorkFlowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), tstExpenseStatus::Reject);
}


public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
{
tstWorkerExpense::updateWorkFlowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), tstExpenseStatus::Approve);
}

public void canceled(WorkflowElementEventArgs _workflowElementEventArgs)
{
tstWorkerExpense::updateWorkFlowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), tstExpenseStatus::Cancel);
}

public void started(WorkflowElementEventArgs _workflowElementEventArgs)
{
tstWorkerExpense::updateWorkFlowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), tstExpenseStatus::Submit);
}


Now expand the workflow and in sub node Supported Elements, drag and drop approval artifact.
Support



Drag
WorkFlowElement



Now create a new menu item and set its form and set its following properties.
WorkFlowMenuItem
  • Form WorkflowTableListPage
  • EnumTypeParameter to ModuleAxapta
  • EnumParameter to Basic.


Now generate increment CIL. It is necessary step.


Click on this and open the workflow design.


Navigate to the menu item to open WorkflowTableListPage to design the workflow.
Click on new button.
WorkFlowScreen

Drag and drop approval on designer screen and. Link the start to shape and from shape to end .

WorkFlow
 

Labels

#veryusefulcode (1) AIF (8) AOT Maps (1) Args (1) Ax 2009 Reports (2) AX 2012 navigation (1) Ax 2012 Interview Questions (1) AX 7 (2) AX Architecture (1) Ax Backup (1) AX Workflow (2) AX2012 (1) AX2012 R2 (1) Ax2012R3 (1) AX2012R3 Dynamics Connector Step by Step Installation and Configuration (1) AX2012R3 EP Step by Step Installation and Configuration EP R3 (1) AX2012R3 HelpServer Step by Step Installation and Configuration (1) AX2012R3 Rapid Start Connector Step by Step Installation and Configuration (1) AX2012R3 Report Server and Analysis Server Step by Step Installation and Configuration (1) AX7 (1) Best practices (1) Blocking user to enter (1) Collection Classes (1) Container (1) D365FO (3) Data Migration Frame Work ax 2012R3 (1) Deleting duplicate Record from Table – Ax2012 (1) Delivery due date notification workflow in Ax 2012 (1) Development Steps EP (1) Dimensions (1) DIXF (1) DMF in Ax 2012 R3 (1) Dynamics Ax 2012 Interview Questions (1) DYNAMICS AX 2012 INTERVIEW QUESTIONS PART 2 (1) DYNAMICS AX 7 (1) EDT relation Migration Tool (1) EP AX 2012 (1) Ep Lookup (1) Error (1) Event Handler (1) F5 (1) File Handling (4) Filter on AX2012 Listpage (1) filtering (2) financial dimensions in AX 2012 (3) form (1) images (1) Installation and Configration (4) Installation and Configuration (11) Installation of Management Reporter 2012 for AX 2012 (1) Interaction class in ax 2012 (1) Interview Question (1) Interview Questions For Ax 2012 (1) Invent DIm (1) Jobs (2) license (1) List page and form menuitem enable code (1) Methods (1) microsoft Dynamics AX 365FO (1) Number Sequence Generation – AX 2012 (5) Number Sequence2012 (1) OLTP-OLAP (1) Passing Args (1) Passing form caller and menu item caller in ax 2012 (1) Passing Multiple Records Args (1) Posting in Ax 2012 (1) POSTING PURCHASE ORDER (1) Query (1) Query Filter Form (2) Query Ranges in X++ (1) Question and Answer (1) Report (1) Reports Controller class (1) RLS in ax 2009 (1) SALES ORDER IMPORT/EXPORT FRAMEWORK BY DMF (1) Security (1) security roles (1) Security Sysqueryrangeutil (1) Sharepoint 2016 (1) SQL SERVER (1) SSRS (2) SSRS Reports Controller class (2) Table collections & Virtual company (1) Time (1) TIPS AND TRICKS (1) Web service AIF (3) Web Services on IIS (AIF) Step by Step Installation and Configuration (1) workflow ax2012 (1) Workflow installation (1) Workflow Method (3) X++ (1)