Sunteți pe pagina 1din 24

Chapter 6: Workflow

CHAPTER 6: WORKFLOW
Objectives
The objectives are:

• Configure how the workflow engine is executed on a server.


• Specify which application module a workflow is applicable to using
a workflow category.
• Link tables to workflows using a query.
• Create a new workflow type.
• Apply a workflow to a form.
• Define what happens when the workflow is approved or denied.
• Create Event Handlers and apply them to a workflow.
• Configure a workflow.

Introduction
Workflow is a system in Microsoft Dynamics® AX 2012 that allows business
processes to be automated. For example, a purchase requisition may need to be
approved by a number of different employees according to the requisition's total
amount. Each employee has to approve it before the next employee in the
approval route.

A Workflow in Microsoft Dynamics AX uses a combination of Application


Object Tree (AOT) elements created by IT and additional set up that a user can
control. This course introduces the development side of creating a workflow. To
create a workflow you will need to use skills developed from this class and the
Morph X development class.

6-1
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

Scenario
Isaac, the systems developer, has been asked to create a new workflow that will
be used to approve a new sales order for a customer that has reached their credit
limit. The requirement is that when a new sales order is entered which takes the
customer over their credit limit, the sales order is submitted to the Accounts
Receivable (AR) manager. They will either approve or deny the sales order. Until
it is approved, the sales order cannot be picked, packed or invoiced.

Workflow Configuration
There are three batch jobs that manage workflow processing. These jobs are all
run on the Application Object Server (AOS) using the Batch system. To set this
up, run System Administration > Setup > Workflow > Workflow
infrastructure configuration. Enter batch groups for each process. Batch groups
can be used to control which AOS instance each workflow batch job is run on.

Create a Workflow Category


A workflow category is used to determine the module in which the workflow will
be available. Modules are defined by the ModuleAxapta enum.

Demonstration: Creating a Workflow Category


Perform the following steps to create a category that allows the workflow to be
authored from the Sales and Marketing module.

1. Open the AOT.


2. Expand the Workflow node.
3. Right-click on the Workflow Categories node and select New
Workflow Category. A new workflow category called Workflow
Category1 will be created.
4. Right-click on the newly created workflow category and select
Properties.
5. Change the name property to SalesCategory.
6. Change the label property to Sales workflows.

6-2
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

7. Change the Module property to SalesOrder.


8. Right-click on the newly created workflow category and select Save.

FIGURE 6.1 WORKFLOW TYPE WIZARD

Create a Query
A workflow uses a query to define the data that is used by the workflow. It can
define one or more tables and all or selected fields on that table.

Demonstration: Creating a Query


A query defines what tables are used to determine that a workflow can be
initiated. Use a class to bind that query to the workflow type. Perform the
following steps to create a query.

1. Open the AOT.


2. Right-click on the Queries node and select New Query.
3. Rename the query to SalesCreditLimitApproval.
4. Expand the newly created query.
5. Open another AOT window.
6. Expand Data Dictionary > Tables.
7. Find the table SalesTable.
8. Drag the SalesTable table to the Data Sources node of the
SalesCreditLimitApproval query.
9. Expand the SalesTable_1 node
10. Right-click on the Fields node and select Properties.
11. Set the Dynamics property to Yes and close the property sheet.
12. Right click on the SalesCreditLimitApproval query and select
Save.

6-3
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

Create a Workflow Type


A workflow type defines the business document being acted upon, the supported
AOT elements, and event handlers and menu items for the workflow. Workflows
are created based on a type, and many workflows can be based on the same type.

Demonstration: Creating a Workflow Type


Perform the following steps to create a workflow type and bind it to the
workflow category created in the previous demonstration.

1. Open the AOT.


2. Expand the Workflow node.
3. Right-click on the Workflow Types node and select Add-ins >
Workflow type wizard.
4. Click Next.
5. Enter SalesCreditLimitAppr in the name.
6. Enter SalesCategory in the Category.
7. Enter SalesCreditLimitApproval in the query.
8. Enter SalesTable in the Document menu item.

FIGURE 6.2 WORKFLOW TYPE

9. Click Next.
10. Click Finish. A development project with a number of newly created
elements will be displayed.

6-4
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

Enable Workflow on a Form


Now that both the workflow type and approval element are defined, you can
specify which forms will use it.

Demonstration: Add a WorkflowState Field


Any form that uses the same table in the datasource as the query referenced in a
workflow document can be enabled for a workflow. This demonstration shows
how to enable workflow on the SalesTableListPage form.

You can specify conditions under which a workflow is eligible for submission.
One of these conditions must be that it has not already been submitted. To test
this condition, use a new field on the SalesTable table.

Perform the following steps to add a workflow state field.

1. Open the AOT.


2. Expand Data Dictionary.
3. Right-click on Base Enums and select New Base Enum.
4. Rename the new enum to SalesCreditLimitApprovalStatus
5. Add four elements to the Enum called NotSubmitted, Submitted,
Approved, Rejected.
6. Expand Tables > SalesTable.
7. Right-click on Fields and select New > Enum.
8. Right-click on the newly created field and select Properties.
9. Change the Name property to CreditLimitApprovalStatus.
10. Change the EnumType property to
SalesCreditLimitApprovalStatus.
11. Right-click on the SalesTable node and select Save.

Demonstration: Enable Workflow on the Form


Workflow on the form is enabled using properties on the design node, and by
overriding a form method. Perform the following steps to enable workflow on a
form.

1. Open the AOT.


2. Expand Tables > SalesTable.
3. Create a new method and add the method in the following code.
4. Save the changes made to the table.
5. Expand Forms > SalesTableListPage > Designs.
6. Right-click on the design node and select Properties.
7. Change the WorkflowEnabled property to Yes.

6-5
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

8. Change the WorkflowDatasource property to SalesTable.


9. Change the WorkflowType property to SalesCreditLimitAppr
10. Save your changes to the form.

boolean canSubmitToWorkflow(str _workflowType = '')


{
amountMST creditBalance;
custTable custTable;
;

if (!this.CreditLimitApprovalStatus ==
SalesCreditLimitApprovalStatus::NotSubmitted)
return false;

custTable = this.custTable_InvoiceAccount();

if (!custTable.CreditMax)
return false;

creditBalance = custTable.CreditMax -
custTable.balanceMST();

if (this.amountRemainSalesFinancial() +
this.amountRemainSalesPhysical() < creditBalance)
return false;

return true;
}

Demonstration: Create a Submit to Workflow Class


To submit a document for workflow processing, call standard code to prompt the
user for a comment and to process the submission. Perform the following steps to
create a submit to workflow class.

1. Press Ctrl+Shift+P to open the development projects window.


2. Expand Private.
3. Double-click on SalesCreditLimitApprWFType developement
project.
4. In the development project find the class
SalesCreditLimitApprSubmitManager.
5. Create a new method called submit, and copy the following code for
this method.
6. Modify the code for the main method as shown in the following
code.
7. Press F8 to save and compile the code.
8. Find the menu item SalesCreditLimitApprSubmitMenuItem.

6-6
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

9. Change the Label property to Submit.


10. Right-click on the SalesCreditLimitApprSubmitMenuItem menu
item and select Save.

void submit(Args args)


{
// Variable declaration.
recId recId = args.record().RecId;
WorkflowCorrelationId workflowCorrelationId;

// Hardcoded type name


WorkflowTypeName workflowTypeName =
workflowtypestr(SalesCreditLimitAppr);

// Initial note is the information that users enter


when they
// submit the document for workflow.
WorkflowComment note ="";
WorkflowSubmitDialog workflowSubmitDialog;
SalesTable SalesTable;

// Opens the submit to workflow dialog.


workflowSubmitDialog =
WorkflowSubmitDialog::construct(args.caller().getActiveWork
flowConfiguration());
workflowSubmitDialog.run();

if (workflowSubmitDialog.parmIsClosedOK())
{
recId = args.record().RecId;
SalesTable = args.record();

// Get comments from the submit to workflow dialog.


note = workflowSubmitDialog.parmWorkflowComment();

try
{
ttsbegin;

workflowCorrelationId =
Workflow::activateFromWorkflowType(workflowTypeName,

recId,

note,

NoYes::No);

SalesTable.CreditLimitApprovalStatus =
SalesCreditLimitApprovalStatus::Submitted;

// Send an Infolog message.


info("Submitted to workflow.");

6-7
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

ttscommit;
}
catch(exception::Error)
{
info("Error on workflow activation.");
}
}
args.caller().updateWorkFlowControls();
}

Create a Workflow Approval


A workflow element may contain a number of outcomes. The workflow may be
approved, rejected, returned or a change may be requested. The Workflow
Approval element determines which of these outcomes is allowed and what
happens in the event of each outcome.

Each outcome can trigger the execution of business logic by specifying a menu
item for each item.

Demonstration: Creating a Workflow Approval


Perform the following steps to create a workflow approval element and set key
properties.

1. Open the AOT.


2. Expand the Workflow node.
3. Right-click on Approvals and select Add-ins > Approval wizard.
4. Click Next.
5. Enter SalesCLApproval in Name.
6. Enter SalesCreditLimitApprDocument in Workflow document.

6-8
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

7. Enter Overview in Document preview field group.


8. Enter SalesTableListPage in Document menu item.

FIGURE 6.3 WORKFLOW APPROVAL

9. Click Next.
10. Click Finish. A development project with a number of newly created
elements is displayed.
11. Drag SalesCLApproval approval to the Supported elements node
on the SalesCreditLimitAppr workflow type.
12. Save your changes to the SalesCreditLimitAppr workflow type

Create Event Handlers


Event handlers are used to execute business logic at specific points in the life of a
workflow. They can be implemented at the workflow level, for example when the
workflow is started or completed, or at an element level, for example when
anyone approves or rejects a step in the approval.

Event handlers are implemented by creating a class that implements one or more
of the EventHandler interfaces. The interfaces at the workflow level are as
follows:

Event Description
WorkflowStarted This event occurs when the workflow instance starts.
EventHandler
WorkflowCompleted This event occurs when the workflow instance ends
EventHandler after it is completed.

6-9
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

Event Description
WorkflowCanceled This event occurs when the workflow instance ends
EventHandler after it is canceled. Use this event handler to perform
any clean up operations needed.
WorkflowConfig This event occurs when a workflow changes. Use this
DataChangeEvent event handler to identify when a workflow has
Handler changed. For example, if you create an association
between the application data and a workflow, this
event would be raised if the workflow was deleted or
updated so that application code could be invoked or
respond in some way.

Demonstration: Write code for Event Handler


The wizards run in the previous demonstrations create event handler classes that
need to be completed. This can include basic steps to set the status of the
approval, or include more complex business logic. The following code needs to
be added to the completed method of the SalesCreditLimitApprEventHandler
class.

public void completed(WorkflowEventArgs _workflowEventArgs)


{
SalesTable SalesTable;

select forupdate SalesTable where SalesTable.RecId ==


_workflowEventArgs.parmWorkflowContext().parmRecId();
if(SalesTable.RecId)
{
SalesTable.CreditLimitApprovalStatus =
SalesCreditLimitApprovalStatus::Approved;
SalesTable.write();
}
}

Element Level Event Handlers


The event handler interfaces at the workflow element level are as follows:

Event Description
WorkflowElement This event occurs when the task or approval starts.
StartedEventHandler For approvals, you can use this event to transition
the workflow document state from Submitted to
PendingApproval.
WorkflowElement This event occurs when the task or approval is
CanceledEventHandler canceled.
For approvals, you can use this event to transition
the workflow document state from the current state
to Canceled.

6-10
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

Event Description
WorkflowElement This event occurs when the task or approval is
CompletedEvent completed.
Handler For approvals, you can use this event to transition
the workflow document state from
PendingApproval to Approved.
WorkflowElement This event occurs when the task or approval is
ReturnedEventHandler returned to the originator.
For approvals, you can use this event to transition
the workflow document state from the current state
to RequestChange.
WorkflowElemChange This event occurs when an approver requests a
RequestedEvent change to the task or approval.
Handler For approvals, you can use this event to transition
the workflow document state from
PendingApproval to RequestChange.

Demonstration: Add Element Level Event Handlers


Perform the following steps to add element or outcome level event handlers.

1. In the AOT locate the class SalesCLApprovalEventHandler. This


class was created by the Approval element wizard.
2. Add the following code in the returned method

public void returned(WorkflowElementEventArgs


_workflowElementEventArgs)
{
SalesTable SalesTable;

ttsbegin;

select forupdate SalesTable


where SalesTable.RecId ==
_workflowElementEventArgs.parmWorkflowContext().parmRecId()
;

SalesTable.CreditLimitApprovalStatus =
SalesCreditLimitApprovalStatus::Rejected;
SalesTable.update();

ttscommit;
}

3. Save your changes to the class.


4. In the AOT, right-click on the AOT node and select Incremental
CIL generation from X++

6-11
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

NOTE: Code run in batch jobs must first be compiled to the Common
Intermediate Language (CIL) of the .NET framework. This increases the
performance of the batch job.

Author a Workflow
Now that you have created a workflow type and enabled it on a form, you can
configure it for use.

Demonstration: Authoring a Workflow


Perform the following steps to configure a workflow from the main menu.

1. The workflow category we created in the first procedure needs to be


added to the main menu. Create a new display menu item called
WorkflowConfigurationSales.
2. Set the label to Sales workflows.
3. Set the object to WorkflowTableListPage.
4. Set the EnumTypeParameter to ModuleAxapta.
5. Set the EnumParameter to SalesOrder.
6. Add the new menu item to SalesAndMarketting > Setup menu.
7. In the property sheet for the new node in the menu, set the property
IsDisplayedInContentArea to Yes.
8. Save your changes to the menu.
9. Open the main menu and select Sales and Marketting > Setup >
Sales workflows.
10. Click New.
11. Select SalesCreditLimitApprType and click Create workflow.
The workflow editor window opens.
12. Drag SalesCLApprovalApproval approval from the Workflow
elements window to the Workflow window.

6-12
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

13. Drag the bottom node of the Start box to the top node of the
Approval box.
14. Drag the bottom node of the Approval box to the top node of the
End box.

FIGURE 6.4 WORKFLOW EDITOR

15. Double click on the Approval box.


16. Click Step 1
17. Click Assignment.
18. On the Assignment type tab, select User
19. On the User tab, double click on Sammy
20. Click on Basic Settings and then enter a subject and instructions for
the approval
21. Click on Close
22. Click on Save and Close
23. Enter some notes for the new workflow if you wish.
24. Select to Activate the new version
25. Click OK.

Demonstration: Test the Workflow


Perform the following steps to test the workflow by setting a credit limit and
creating a sales order.

1. In the application workspace, navigate to Accounts receivable >


Common > Customers > All customers.
2. Select a customer and click Edit.

6-13
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

3. In the Credit and collections fasttab, set a credit limit.


4. Close the Customers form.
5. Go to Sales and marketting > Common > Sales orders > All sales
orders.
6. Click New sales order. Enter the customer account modified that
you modified the credit limit for and click ok.
7. Enter items and quantities in the sales lines so that the balance of the
customer plus the total amount on the lines is greater than the credit
limit.
8. The workflow Submit button and dialog should appear.
9. Click the Submit button and enter a comment.
10. Wait for the batch job to process the workflow request. This should
take one to two minutes.
11. Select Actions > History. You will see that the document is waiting
for approval by the person you assigned to approve it.
12. Logon to windows as the Sammy using the Switch User option on
the Start menu.
13. Start the Microsoft Dynamics AX client.
14. Open the Sales order form.
15. Click the workflow Actions button and select Approve.
16. Wait one to two minutes for the workflow engine to process the
approval.
17. The workflow has now been approved.

6-14
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

Lab 6.1 - Add Another Condition to the Submit Action


Scenario

Isaac has been asked to ensure that, once a credit limit has been reached, the sales
order cannot be posted until the workflow has been approved.

Challenge Yourself!
Add conditions to the posting functions on the sales order form that will prevent
posting to picking, packing or invoicing until the workflow has been approved. If
the credit limit has not been reached, then the postings should be allowed.

Step by Step

1. Add the following method CanPostCreditLimit to the salesTable


table.
2. Add the following code to the methods canPickingListBeUpdate(),
canPackingSlipBeUpdated() and canInvoiceBeUpdated() in the
salesTableType class.

boolean canPostCreditLimit()
{
amountMST creditBalance;
custTable custTable;
;

if (this.CreditLimitApprovalStatus ==
SalesCreditLimitApprovalStatus::Approved)
return true;

if (this.CreditLimitApprovalStatus ==
SalesCreditLimitApprovalStatus::Rejected
|| this.CreditLimitApprovalStatus ==
SalesCreditLimitApprovalStatus::Submitted)
return false;

custTable = this.custTable_InvoiceAccount();

if (!custTable.CreditMax)
return true;

creditBalance = custTable.CreditMax -
custTable.balanceMST();

if (this.amountRemainSalesFinancial() +
this.amountRemainSalesPhysical() < creditBalance)
return true;

return false;
}

6-15
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

boolean canPickingListBeUpdated()
{
......

ok = ok && salesTable.canPostCreditLimit();

return ok;

boolean canPackingslipBeUpdated()
{
......

ok = ok && salesTable.canPostCreditLimit();

return ok;

boolean canInvoiceBeUpdated()
{
......

ok = ok && salesTable.canPostCreditLimit();

return ok;

6-16
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

Lab 6.2 - Enable Resubmit


Scenario

Isaac is required to ensure the workflow can be resubmitted after it has been
rejected.

Challenge Yourself!
When a workflow is rejected, it can be resubmitted. Modify the Submit to
Workflow class so that it can resubmit the workflow after a rejection

Use the PurchReqWorkflow class as model.

Step by Step

1. Find the class SalesCLApprovalResubmitActionMgr.


2. Modify the Main method and add a new method Resubmit as shown
in the following code:

public static void main(Args _args)


{

SalesCLApprovalResubmitActionMgr
SalesCLApprovalResubmitActionMgr = new
SalesCLApprovalResubmitActionMgr();

SalesCLApprovalResubmitActionMgr.resubmit(_args);

private void resubmit(Args _args)


{
// Variable declaration.
recId _recId = _args.record().RecId;
WorkflowCorrelationId _workflowCorrelationId;
// Hardcoded type name
WorkflowTypeName _workflowTypeName =
workflowtypestr(SalesCreditLimitAppr);
// Initial note is the information that users enter
when they
// submit the document for workflow.
WorkflowComment _initialNote ="";
WorkflowWorkItemActionDialog
WorkflowWorkItemActionDialog;
SalesTable SalesTable;
;

6-17
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

// Opens the submit to workflow dialog.


workflowWorkItemActionDialog =
WorkflowWorkItemActionDialog::construct(
_args.caller().getActiveWorkflowWorkItem(),

WorkflowWorkItemActionType::Resubmit,

new MenuFunction(menuitemactionstr(PurchReqReSubmit),
MenuItemType::Action));
workflowWorkItemActionDialog.run();

if (WorkflowWorkItemActionDialog.parmIsClosedOK())
{
_recId = _args.record().RecId;
SalesTable = _args.record();
// Get comments from the submit to workflow dialog.
_initialNote =
workflowWorkItemActionDialog.parmWorkflowComment();

try
{
ttsbegin;

WorkflowWorkItemActionManager::dispatchWorkItemAction(
_args.caller().getActiveWorkflowWorkItem(),

_initialNote,

curUserId(),

WorkflowWorkItemActionType::Resubmit,

_args.menuItemName(),

false);

SalesTable.CreditLimitApprovalStatus =
SalesCreditLimitApprovalStatus::Submitted;

// Send an Infolog message.


info("Resubmitted to workflow.");

6-18
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

ttscommit;
}

catch(exception::Error)
{
info("Error on workflow activation.");
}
}

_args.caller().updateWorkFlowControls();

6-19
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

Summary
The workflow system is highly configurable and flexible. By using Morph X and
some standard code patterns, it can be enabled for virtually any part of the
Microsoft Dynamics AX application.

This lesson explores some of the possibilities the workflow system offers, and
explores some of the different ways it can be used to cover most workflow
requirements.

6-20
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

Test Your Knowledge


Test your knowledge with the following questions.

1. What application element is used to define to which module a workflow is


applicable?
( ) Workflow type
( ) Workflow category
( ) A field in the workflow configuration
( ) SalesTable

2. What type of AOT element needs to be created to specify which tables will
be processed by a workflow?
( ) Extended data type
( ) Class
( ) Form
( ) Query

3. Which three properties on a form design need to be modified to allow the


form to use a workflow?
( ) WorkflowType
( ) WorkflowEnabled
( ) WorkflowDocument
( ) WorkflowDatasource

6-21
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

Quick Interaction: Lessons Learned


Take a moment and write down three key points you have learned from this
chapter

1.

2.

3.

6-22
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Chapter 6: Workflow

Solutions
Test Your Knowledge
1. What application element is used to define to which module a workflow is
applicable?
( ) Workflow type
(•) Workflow category
( ) A field in the workflow configuration
( ) SalesTable

2. What type of AOT element needs to be created to specify which tables will
be processed by a workflow?
( ) Extended data type
( ) Class
( ) Form
(•) Query

3. Which three properties on a form design need to be modified to allow the


form to use a workflow?
(√) WorkflowType
(√) WorkflowEnabled
( ) WorkflowDocument
(√) WorkflowDatasource

6-23
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement
Development III in Microsoft Dynamics® AX 2012

6-24
Microsoft Official Training Materials for Microsoft Dynamics®
Your use of this content is subject to your current services agreement

S-ar putea să vă placă și