Chapter 8. Workflow in Microsoft Dynamics AX

In this chapter

Introduction
Microsoft Dynamics AX 2012 workflow infrastructure
Windows Workflow Foundation
Key workflow concepts
Workflow architecture
Workflow life cycle
Implementing workflows

Introduction

Few people would deny the importance or significance of the processes that drive the businesses and organizations that we work for and interact with on a daily basis. Business processes represent the key activities that, when carried out, are intended to achieve a specific goal of value for the business or organization. For example:

  • A manufacturing operation in which business process activities include design, development, quality assurance testing, and delivery of a saleable (and hopefully profitable) range of goods

  • Sales process activities for manufactured items, including marketing, locating prospects, providing quotes, converting quotes to orders and prospects to customers, shipping the product, invoicing, and obtaining payment

  • Supporting activities that contribute to the business or organization in tangible ways, such as hiring new employees and managing employee expenses

Viewing activities in terms of the business processes that encompass them affords businesses and organizations the opportunity to systematically define, design, execute, evaluate, and then improve the way that these activities are performed. This systematic approach is extremely valuable, even critical, given that today’s businesses and organizations have to react to an increasingly rapid rate of change and the ever-expanding influence of globalization.

Enterprise resource planning (ERP) suites, such as Microsoft Dynamics AX, exist to automate business processes and to provide the capability to adapt these processes to the needs of businesses and organizations over time. Before Microsoft Dynamics AX 2009, no standard workflow infrastructure existed in the product, and each company had to write specific business logic to implement everyday activities such as approvals. The Microsoft Dynamics AX 2009 release included a built-in workflow infrastructure to make it easier for businesses and organizations to automate and manage business processes. This infrastructure has been enhanced further in Microsoft Dynamics AX 2012.

The main difference between business processes and workflows (these terms are often used interchangeably) is their scope, level of abstraction, and purpose. Business processes represent the broad set of activities that a business or organization needs to carry out, along with the interrelationships among the activities. Business processes are implementation-independent and can combine manual and automated activities. Workflows represent the automated parts of a business process that coordinate various human or system (or both) activities to achieve a particular outcome, and they are implementation-specific. Therefore, workflows are used to implement parts of a business process.

Microsoft Dynamics AX 2012 workflow infrastructure

Fundamentally, a workflow consists of one or more activities that represent the items of work to be completed. In addition, the concept of workflows that connect the activities and govern the sequence of execution (referred to as the structure of a workflow) is key. The behavior of a workflow is determined by its type. Figure 8-1 illustrates the major types of workflows and identifies where the emphasis of the workflow infrastructure is located in Microsoft Dynamics AX 2012.

Major types of workflows.

Figure 8-1. Major types of workflows.

A major distinction exists between human workflows and system workflows. (For more information, see the Types of workflows sidebar on the next page.) Workflows in Microsoft Dynamics AX 2012 are primarily designed to support structured human workflows. Almost 60 structured human workflows are included with the product, spanning accounts payable, accounts receivable, budgeting, fixed assets, general ledger, organization administration, procurement and sourcing, system administration, time and attendance, and travel and expense. Whereas the built-in workflows tend to focus on structured human workflows that obtain approvals, you can also create workflows that contain tasks for humans to complete or a mixture of structured and unstructured tasks along with approvals and automated tasks. Customers, partners, and independent software vendors (ISVs) can create additional workflows to supplement those in the product.

Because existing Microsoft Dynamics AX modules use approvals extensively, the workflow infrastructure in Microsoft Dynamics AX 2012 is primarily intended to support structured human workflows. Focusing on this type of workflow lays the groundwork to help businesses and organizations more easily automate, analyze, and improve high-volume workflows across their ERP systems.

Each structured human workflow in Microsoft Dynamics AX 2012 acts on a single document type because data is the key constituent of an ERP system. (Think of the broad categories of data that exist in an ERP system: master data, transaction data, and reference data. Processes that operate within those systems are largely data-driven.)

Here are some of the key tasks that you can perform with structured human workflows in Microsoft Dynamics AX 2012:

  • Define the activities that must take place, based on the business process that is being automated.

  • Define the sequence in which tasks, approvals, subworkflows, and the new workflow elements in Microsoft Dynamics AX 2012 (manual decisions, automated decisions, parallel activities and branches, automated tasks, and line-item workflows) execute to reflect the order in which activities must be completed in a business or an organization.

  • Set up a condition to determine which workflow to use in a given situation.

  • Decide how to assign an activity to users.

  • Specify the text that is displayed in the user interface for the various activities to help users understand what they need to do.

  • Define a set of outcomes for an activity that users can select from.

  • Select which notifications to send, which email template to use, when to send the notifications, and who should receive the notifications.

  • Establish how a workflow should be escalated if there is no timely response to an activity.

Four types of users interact with the workflow infrastructure in Microsoft Dynamics AX 2012: business process owners, developers, system administrators, and end users (called “users” in this book).

Business process owners and developers are primarily responsible for defining, designing, and developing workflows, whereas system administrators and users interact with workflows that are executing.

  • Business process owners understand the objectives of the business or organization within which they operate to the degree that they can envision how best to structure the activities within their areas of responsibility. Business process owners therefore configure workflows that have already been implemented and work with functional consultants or developers to enable other modules or create new workflow types in existing modules.

  • Developers work with business process owners to design and implement any underlying business logic that is required to support workflows that are being developed.

  • System administrators set up and maintain the development and production environments, ensure that the workflow infrastructure is configured correctly, monitor workflows as they execute, and take actions to resolve issues with workflows that are executing.

  • Users interact with workflows when necessary; for example, by submitting a business document record, taking a particular action (such as approving or rejecting a document), entering comments, viewing workflow history, and so on.

Windows Workflow Foundation

The workflow infrastructure in Microsoft Dynamics AX 2012 is related to Windows Workflow Foundation (WF), which is part of the Microsoft .NET Framework 4. WF provides many fundamental capabilities that are used by the workflow infrastructure in Microsoft Dynamics AX 2012. As a low-level infrastructure component, however, WF has no direct awareness of or integration with Microsoft Dynamics AX 2012. In Figure 8-2, the workflow infrastructure (A) is an abstraction layer that sits above WF (B) and allows workflows that are specific to Microsoft Dynamics AX to be designed, implemented, and configured in Microsoft Dynamics AX 2012 and then executed by using WF.

Relationship between the Microsoft Dynamics AX 2012 workflow infrastructure and WF.

Figure 8-2. Relationship between the Microsoft Dynamics AX 2012 workflow infrastructure and WF.

In the following list, each numbered item refers to the corresponding part of Figure 8-2:

  1. The developer designs and implements workflow elements and business logic in the Application Object Tree (AOT).

  2. The business process owner models workflows using the new graphical workflow editor in the Microsoft Dynamics AX 2012 client, which is based on the WF Designer.

  3. The workflow runtime bridges both the Microsoft Dynamics AX 2012 workflow infrastructure and WF; it instantiates and then executes workflows. (The system administrator manages the runtime environments.)

  4. Users interact with workflow user interface (UI) controls both in the Microsoft Dynamics AX 2012 Windows client and in the Enterprise Portal web client.

Key workflow concepts

As a Microsoft Dynamics AX developer, workflow is something that you work with to help the users in a business or organization improve their efficiency. The ultimate goal for workflow in Microsoft Dynamics AX 2012 is to make it as easy as possible for business process owners to configure workflows fully themselves, freeing developers to work on other activities. Currently, developers and business process owners work together to create and customize workflows.

You need to understand a number of key concepts to help business process owners implement workflows successfully.

Workflow document and workflow document class

The workflow document, sometimes referred to as the business document, is the focal point for workflows in Microsoft Dynamics AX 2012. Every workflow type and every workflow element must reference a workflow document because it provides the data context for the workflow. A workflow document is an AOT query supplemented by a class in the AOT (referred to as the workflow document class). The term workflow document is used instead of query because it more accurately portrays what the workflow operates on. The query used by a workflow document can reference multiple data sources and isn’t constrained to a single table. In fact, a query can reference data sources hierarchically. However, if there are multiple data sources within a query, the first data source is considered the primary or root data source.

Tip

The workflow document and workflow document class are located in the AOT in the Microsoft Dynamics AX 2012 client.

Workflow in Microsoft Dynamics AX 2012 incorporates an expression builder that you can use to define conditions that control the behavior of an executing workflow. The expression builder uses the workflow document to enumerate the fields that can be referenced in conditions. To make derived data available within conditions, you add parm methods to the workflow document class, and then add X++ code to the parm methods to produce the derived data. The workflow document then returns the fields from the underlying query plus the data generated by the parm methods.

Workflow categories

Workflow categories determine the association a workflow type has to a specific module. (Without these categories, you would see all workflows in the context of every module in Microsoft Dynamics AX 2012.) For example, a workflow category named ExpenseManagement, which is mapped to the Travel and Expense module, comes with Microsoft Dynamics AX 2012. All workflows associated with this module are visible in the Microsoft Dynamics AX 2012 client within the Travel and Expense module. If you add a new module to Microsoft Dynamics AX 2012, you must create a new module and a new workflow category that references that module.

Tip

Workflow categories are located in the AOT in the Microsoft Dynamics AX 2012 client.

Workflow types

The workflow type (called a “template” in Microsoft Dynamics AX 2009) is the primary building block that developers use to create workflows. You generate the workflow type by using the new Workflow wizard, shown in Figure 8-3. The wizard automates the creation of the metadata required for a workflow type; all you need to do is specify the name, workflow category, query, and menu items.

The Create Workflow Type page in the Workflow wizard

Figure 8-3. The Create Workflow Type page in the Workflow wizard

The resulting metadata is created under the AOTWorkflowWorkflow Types node. The business process owner later references this workflow type when creating an actual workflow.

Tip

Workflow types are located in the AOT in the Microsoft Dynamics AX 2012 client.

For more information about the Workflow wizard, see “How to: Create a New Workflow Type” at http://msdn.microsoft.com/en-us/library/cc594095.aspx.

Event handlers

Event handlers are well-defined integration points that developers use to trigger application-specific business logic during workflow execution. Workflow events are exposed at the workflow level and the workflow element level. For more information about event handlers, including where they are used, see “Workflow Events Overview” at http://msdn.microsoft.com/en-us/library/cc588240.aspx.

Tip

Event handlers are located in the AOT in the Microsoft Dynamics AX 2012 client.

Menu items

Workflow in Microsoft Dynamics AX 2012 uses both display and action menu items. Display menu items are used to navigate to a form in the Microsoft Dynamics AX 2012 client that displays the details of the record being processed by workflow. Web menu items are used to navigate to the same type of webpage in Enterprise Portal. Action menu items are used for each possible action that a user can take in relation to a workflow. They also provide another integration point for you to integrate custom code. For more information about the menu items that are used in the workflow infrastructure, see “How to: Associate an Action Menu Item with a Workflow Task or Approval Outcome” (http://msdn.microsoft.com/en-us/library/cc602158.aspx) and “How to: Associate a Display Menu item with a Workflow Task or Approval” (http://msdn.microsoft.com/en-us/library/cc604521.aspx).

Tip

Menu items are located in the AOT in the Microsoft Dynamics AX 2012 client.

Workflow elements

The elements of a workflow represent the activities within the workflow. The business process owner models these elements. An element can be a task, an approval, a subworkflow, a manual decision, an automated decision, a parallel activity with multiple branches, a line-item workflow, or an automated task. Developers implement the task, approval, line-item workflow, and automated task elements. The rest are referred to as “configuration only” elements that business process owners can use in the graphical workflow editor. The following list describes each element:

  • Tasks are generic workflow elements that represent a single unit of work. The developer defines the possible outcomes for each task.

  • Approvals are specialized tasks that allow sequencing of multiple steps and use a fixed set of outcomes.

  • Subworkflows are workflows that are invoked from other workflows.

  • Manual decisions enable the workflow to follow one of two possible paths based on an action taken by a user.

  • Automated decisions enable the workflow to follow one of two possible paths based on a condition.

  • Parallel activities contain two or more branches that represent discrete workflows, and they are executed simultaneously.

  • Line-item workflows are modeled within a workflow that exists for a business document that represents the master in a master-detail relationship. They enable specific workflows to be instantiated on line items that are associated with the master business document; for example, expense lines on an expense report.

  • Automated tasks are non-interactive and invoke X++ business logic synchronously.

Manual and automated decisions, parallel activity, line-item workflows, and automated tasks are new in Microsoft Dynamics AX 2012. In addition workflow wizards have been added to make the creation of approval and task elements easier.

Tip

Workflow elements are located in the AOT in the Microsoft Dynamics AX 2012 client.

Queues

The ability to assign workflow work items to a queue is new in Microsoft Dynamics AX 2012. Queues offer an alternative to assigning workflow work items directly to users by providing support for teams that collaborative within a business process. With this approach, a work item is first assigned to a queue; then, the work item is claimed by a member of the queue so it can be worked on. The eventual work item owner can also return the work item to the original queue, put the work item in another queue, or assign the work item to another user.

To use work item queues, complete the following steps:

  1. Create one or more work item queues for a selected workflow document (for example, purchase requisition header) and assign one or more Microsoft Dynamics AX users to each queue. These assigned users can view and take action on work items assigned to the queue. Each queue also has an administrator, which by default is the user who created the queue.

  2. Create a work item group, a container for grouping one or more work item queues, and then add all of the work item queues for a given document type to the work item group.

  3. Set the status of the work item queues to Active so that workflows can assign work items to them.

  4. Create a workflow by using a workflow type based on the same business document as the queue. Model a task element within the workflow and configure it to be assigned to the appropriate work item queue.

    Note

    Only work items generated from task elements can be assigned to queues, because a task can be completed by only a single user. In this case, the queue provides a way for users to assign themselves to work items. Approvals differ from tasks in this respect because approvals are explicitly modeled around an approval pattern that consists of one or more discrete steps; each step has a specific type of user assignment and a completion policy that controls when an approval is complete.

  5. Submit a record to workflow. Any work items created for the task element are directed to the appropriate queue. Users can access work item forms in the Microsoft Dynamics AX 2012 client and review, accept, and take action on work items in their queue.

For more information about setting up work item queues, see “Configure work item queues” at http://msdn.microsoft.com/en-us/library/gg731875.aspx.

Providers

Workflow in Microsoft Dynamics AX 2012 uses the provider model as a flexible way of allowing application-specific code to be invoked for different purposes when a workflow is executing. There are four provider types within the workflow infrastructure: due date, participant, hierarchy, and queue. The way in which provider metadata is stored has changed in this release. In Microsoft Dynamics AX 2009, providers were developed as classes that implemented a provider interface and were registered on the workflow element as a property. Workflow providers in Microsoft Dynamics 2012 now have their own node in the AOT (AOT > Workflow > Providers), as shown in Figure 8-4.

The new Workflow Providers node in the AOT.

Figure 8-4. The new Workflow Providers node in the AOT.

In addition each provider now has properties that are used to define the following:

  • Their organization scope (AssociationType)

  • Whether the provider applies to all workflow types or specific types (Workflow Types subnode)

For more information about workflow providers, including where they are used, see “Workflow Providers Overview” at http://msdn.microsoft.com/en-us/library/cc519521.aspx.

Workflows

The business process owner creates workflows using the new graphical workflow editor (shown in Figure 8-5) in the Microsoft Dynamics AX 2012 client. The business process owner first selects a workflow type and then configures the approvals, tasks, and other elements that control the flow of activities through the workflow.

The new graphical workflow editor.

Figure 8-5. The new graphical workflow editor.

Workflows are located in the Microsoft Dynamics AX 2012 client. A list page containing the workflows for a given module is located on the area page for the module under Setup > [module name] workflows.

Workflow instances

A workflow instance is an activated workflow created by combining the workflow and the underlying AOT workflow elements on which the workflow is based (the workflow type, tasks, and approvals).

Workflow instances are located in the Microsoft Dynamics AX 2012 workflow runtime.

Work items

Work items are the actionable units of work that are created by the workflow instance at run time. When a user interacts with a workflow, he or she responds to a work item that has been generated from a task element, an approval element, or a manual decision. Work items are displayed in the Unified worklist web part and in the Microsoft Dynamics AX 2012 client.

Workflow architecture

Microsoft designed the workflow infrastructure based on a set of assumptions and goals related to the functionality it wanted to deliver. Two assumptions are the most significant:

  • Business logic (X++ code) invoked by workflow is always executed on the Application Object Server (AOS).

  • Workflow orchestration is managed by WF in the .NET Framework 4.0.

The first assumption reflects the fact that most business logic already resides and is executed on the AOS. The second assumption is based on the opportunity to use existing Microsoft technology for orchestrating workflows in Microsoft Dynamics AX 2012 instead of designing and implementing this functionality from scratch. In Microsoft Dynamics AX 2012, the WF framework was integrated into the AOS.

The following primary goals influenced the architecture:

  • Create an extensible, pluggable model for workflow integration (including events and providers), because the workflow infrastructure had to be flexible enough to address application-specific requirements as they pertain to workflow execution.

  • Build in scalability that accommodates the growth of workflow usage in Microsoft Dynamics AX 2012 over time and provides options for scale up and scale out.

  • Minimize the performance impact on transactional X++ business logic to invoke workflows. For example, if workflow activation is triggered from saving a document, no adverse performance side effects should result from doing this in the same physical transaction (ttsbegin/ttscommit) as the save operation.

The next section expands on the capabilities of the workflow runtime in Microsoft Dynamics AX 2012.

Workflow runtime

Figure 8-6 shows the components of the workflow runtime and their interaction.

Workflow runtime.

Figure 8-6. Workflow runtime.

The workflow runtime includes the following components:

  • Workflow API An application programming interface (API) that exposes the underlying workflow functionality to the rest of Microsoft Dynamics AX 2012.

  • Workflow instance storage Tables that store the serialized workflow instance data. Whenever the workflow goes idle waiting for a user or a system action, the workflow instance is serialized and saved to the database and removed from memory on the AOS.

  • Workflow tracking Tables that store the tracking information for a workflow instance. Tracking information is used to display historical information of completed and pending workflow instances.

  • Workflow message queue A table that stores the messages used for communication between the .NET Framework 4.0 workflow instance and the Microsoft Dynamics AX 2012 workflow runtime in X++. A message exchange is required for any scenario where transactional X++ application logic must execute as part of a workflow instance or when user action is required.

  • Application providers and event handlers Application code that is invoked by the workflow instance.

  • Workflow messaging batch job A server-bound batch job that is dedicated to processing messages from the workflow message queue. This batch job supports parallel processing of batch tasks to enable both scaling up and scaling out for workflow processing. The batch job runs in X++ compiled into .NET common intermediate language (CIL).

  • WF plus Microsoft Dynamics AX extensions The workflow framework provided by the .NET Framework 4.0 together with the Microsoft Dynamics AX custom workflow activities, custom providers, and custom workflow host.

Workflow runtime interaction

Figure 8-7 shows the logical control flow the workflow runtime uses to process a workflow activation message.

Logical workflow runtime control flow used to process a workflow activation message.

Figure 8-7. Logical workflow runtime control flow used to process a workflow activation message.

As an example of how these components interact at run time, the following sequence explains what happens when a user clicks Submit to activate a workflow on a record in Microsoft Dynamics AX 2012:

  1. The Submit action invokes the Workflow API to post a workflow activation message for the selected workflow. This causes a message to be posted into the message queue.

  2. The message is processed by the messaging batch job that calls the workflow runtime in the .NET Framework 4.0 to activate the workflow.

  3. The Microsoft Dynamics AX extensions to the .NET Framework 4.0 receive the request and first load the workflow model. The workflow model is the structural representation of the workflow along with all of the workflow and workflow element properties. This model was created using the Microsoft Dynamics AX 2012 graphical workflow editor.

  4. From the workflow model, the .NET Framework 4.0 workflow activity tree is built. These are the runtime workflow activities that orchestrate the workflow. These activities are a combination of custom Microsoft Dynamics AX 2012 activities and the primitive activities from .NET Framework 4.0.

  5. Once the workflow reaches the first point where application logic may need to run, a message is posted, the workflow instance state is serialized and saved, and the workflow tracking data is updated. The workflow started message is posted at this point.

  6. After the workflow instance goes idle and is saved to the database, it is removed from memory in the AOS to save physical computer resources. The workflow instance is brought back into memory after the workflow started message is processed and the acknowledge workflow started message is posted and begins to be processed.

Figure 8-8 shows the logical workflow runtime control flow to process a workflow started message.

Processing a workflow started message.

Figure 8-8. Processing a workflow started message.

The flow in Figure 8-8 builds on the flow in Figure 8-7. A workflow started message is currently posted to the message queue and is ready for processing as follows:

  1. The workflow messaging batch job reads the workflow started message from the message queue. This message was posted by the workflow instance to allow application logic that was registered for this event to be invoked.

  2. The application event handler that was registered for the workflow started event is invoked. This event handler runs the necessary X++ business logic to update the state of the underlying document.

  3. An acknowledge workflow started message is posted and the workflow started message is removed from the message queue. Workflow tracking information is also logged at this point.

  4. The workflow messaging batch job reads the acknowledge workflow started message and calls the workflow runtime in .NET Framework 4.0 to resume the workflow instance.

  5. Microsoft Dynamics AX extensions to .NET Framework 4.0 receive the request to resume and then load the serialized workflow instance state from the workflow instance storage. This action brings the workflow instance back into memory on the AOS.

  6. The workflow instance is placed back into the .NET Framework 4.0 workflow scheduler to be executed. The workflow then executes until the next point that X++ application logic needs to be invoked or until the workflow assigns work to users. Both of these represent points in the workflow instance where the instance must wait for either a system action (for example, processing the application event handler) or a human action (for example, a user approving an expense report).

Logical approval and task workflows

Another way to visualize how the key workflow concepts and architecture come together is to look at the interaction patterns of approval and task elements at run time. Four main types of interactions can occur: workflow events, acknowledgments (of events), provider callbacks, and infrastructure callbacks.

  • Workflow event The workflow instance posts a message, saves the workflow instance, inserts tracking information, and removes the originating message. The workflow instance then waits for the corresponding message to be processed. The workflow messaging batch job processes the message by invoking the event handler on the corresponding workflow type, task, approval, or automated task. Then the workflow messaging batch job posts the acknowledgment message.

  • Acknowledgment An acknowledgment message is the response to an event triggered from a workflow instance. Upon receiving the acknowledgment, the workflow instance is loaded from workflow instance storage back into memory and is resumed.

  • Provider callback A call from the workflow instance to an application-defined workflow provider (for example, to resolve users for assignment or to calculate a due date). Workflow providers are integration points for developers to inject custom code for resolving users, due dates, user hierarchies, or queues. A provider callback is a synchronous call from the workflow instance back into an X++ workflow provider.

  • Infrastructure callback A call from the workflow instance back into X++ to perform infrastructure-related activities. One example is to create work items for each user returned from a call to a participant provider.

Figure 8-9 shows the logical workflow interactions for approvals.

Logical approval workflow interactions.

Figure 8-9. Logical approval workflow interactions.

In Figure 8-9, the outermost box represents the workflow itself. Nested inside are the approval (element) and within that, a single step. (An approval can contain multiple steps.) The smaller rectangular boxes represent events or outcomes. The symbols in the legend represent the four interaction types, which are positioned in Figure 8-9 where that type of interaction occurs. When the workflow starts, an event and an acknowledgment occur. Acknowledgments confirm that the workflow runtime received and processed a preceding event. A similar event and acknowledgment occur for the start of the approval element. When a step starts, callbacks invoke the workflow providers to resolve the users for assignment and the due dates for the corresponding work items. The work items are then created through an infrastructure callback, and the workflow instance waits for the corresponding acknowledgments from the work items. Acknowledgments for work items are triggered when users take action on their assigned work items. After the step (or steps) complete, the outcome is determined based on the completion policies of the step, and the corresponding event is raised for that outcome. The workflow instance then waits for acknowledgment that the workflow runtime has processed the event that is associated with the outcome. Finally, the completion of the workflow itself raises an event.

Task interactions are similar to approvals, except that no steps and outcomes are unique for each task. Figure 8-10 shows the logical workflow interactions for tasks.

Logical task workflow interactions.

Figure 8-10. Logical task workflow interactions.

Workflow life cycle

This section describes the workflow process improvement life cycle, shown in Figure 8-11, and explains the implementation aspects of the life cycle in detail.

The workflow life cycle in Microsoft Dynamics AX 2012.

Figure 8-11. The workflow life cycle in Microsoft Dynamics AX 2012.

The workflow life cycle has four phases:

  • Design Business process owners use their understanding of the organization to decide which parts of a business process that traverses Microsoft Dynamics AX 2012 need to be automated and then design a workflow to achieve this automation. They can collaborate with developers in this phase, or they might just communicate the workflow requirements to the developers.

  • Implement and configure Developers implement workflow artifacts in Microsoft Dynamics AX 2012 based on the design of the business process. Business process owners then model the workflow using the graphical workflow editor. If this work is carried out on a test system, after successfully testing the workflow, the system administrator deploys the related artifacts and workflows to the live, or production, system.

  • Run Users interact with Microsoft Dynamics AX 2012 as part of their day-to-day work, and in the course of doing so, might submit workflow documents to the workflow for processing or interact with workflows that are already activated.

  • Analyze Business process owners evaluate the performance of the workflows that have been designed, implemented, and executed using the workflow analytical cube and performance reports introduced in Microsoft Dynamics AX 2012 to determine if any further changes are warranted.

This cycle is therefore repeated when a workflow that has been designed, implemented and configured, and deployed has to change in some way. Aside from performance, a change might result from a change in the business process or in the organization.

Implementing workflows

You can use the Microsoft Dynamics AX 2012 workflow infrastructure to automate aspects of a business process that are part of a larger business process automation effort. There is no single, correct approach to this undertaking. However, at a high level, you can follow the steps listed here to figure out and understand your existing business processes, determine how these business processes should function, and finally, to automate them by using workflow.

  1. Map out existing business processes. This effort is often referred to as developing the as-is model and may involve the use of a business process modeling tool.

  2. Analyze the as-is model to determine whether obvious improvements can be made to existing processes. These improvements are represented in another business process model, which is often referred to as the to-be model.

  3. Design the way in which you’re going to implement the to-be business process model—or the changes to the as-is model suggested by the to-be model. In this step, you might decide which parts of the to-be business process should be automated with workflow and which parts should remain manual.

  4. For the parts of the business process model in which workflow is going to be used—and for the parts you want to automate—define the workflow document and then design one or more workflows. This step centers on the workflow document that the workflow will act on.

  5. Implement the building blocks for the workflows, such as the business logic, and enabling workflow in the Microsoft Dynamics AX client, Enterprise Portal, or both.

  6. Configure and enable the workflows, causing workflow instances to be created when a record for the workflow document is submitted.

The major advantage of the workflow infrastructure in Microsoft Dynamics AX 2012 is that it provides a significant amount of functionality out of the box, meaning that you don’t have to write custom workflows. Businesses and organizations have more time to focus on improving their processes instead of writing and rewriting business logic.

Create workflow artifacts, dependent artifacts, and business logic

As a developer, once you understand the workflow requirements that the business process owner provides, you must create the corresponding workflow artifacts, dependent workflow artifacts, and business logic. You create these in the AOT by using the Microsoft Dynamics AX 2012 client. You write the business logic in X++.

Table 8-1 lists each workflow artifact and the steps you need to perform when creating it. The artifacts are listed in order of dependency.

Table 8-1. Workflow artifacts.

Artifact

Steps

Workflow category

  1. Define the module in which the workflow type is enabled.

For more information, see the section Key workflow concepts, earlier in this chapter, and “How to: Create a Workflow Category” at http://msdn.microsoft.com/en-us/library/cc589698.aspx.

Approval

  1. Define the approval workflow document.

  2. Define approval event handlers for Started and Canceled.

  3. Define approval menu items for Document, DocumentWeb, Resubmit, ResubmitWeb, Delegate, and DelegateWeb.

  4. Enable or disable approval outcomes.

  5. Define approval outcome menu items for Action and ActionWeb.

  6. Define an approval outcome event handler.

  7. Define the DocumentPreviewFieldGroup.

For more information, see “How to: Create a Workflow Approval” at http://msdn.microsoft.com/en-us/library/cc596847.aspx.

Task

  1. Define the task workflow document.

  2. Define task event handlers for Started, Canceled and WorkItemCreated.

  3. Define task menu items for Document, DocumentWeb, Resubmit, ResubmitWeb, Delegate, and DelegateWeb.

  4. Add or remove task outcomes.

  5. Define task outcome menu items for Action and ActionWeb.

  6. Define task outcome event handler.

  7. Define the DocumentPreviewFieldGroup.

For more information, see “How to: Create a Workflow Task” at http://msdn.microsoft.com/en-us/library/cc601939.aspx.

Automated Task

  1. Define the automated task workflow document.

  2. Define automated task event handlers for Execution and Canceled.

For more information, see “Walkthrough: Adding an Automated Task to a Workflow” at http://msdn.microsoft.com/en-us/library/gg862506.aspx.

Workflow type

  1. Define the workflow document.

  2. Define event handlers for workflow Started, Completed, ConfigDataChanged, and Canceled.

  3. Define menu items for SubmitToWorkflow, SubmitToWorkflowWeb, Cancel, and CancelWeb.

  4. Define the workflow category. (Select a category from the existing categories.)

  5. Define supported approvals, tasks, and automated tasks (these will then be displayed in the graphical workflow editor.)

  6. Enable or disable activation conditions for workflows based on the type.

For information about creating workflow types, see “Walkthrough: Creating a Workflow Type” at http://msdn.microsoft.com/en-us/library/cc641259.aspx.

Table 8-2 identifies the dependent workflow artifacts that are referenced in Table 8-1.

Table 8-2. Dependent workflow artifacts.

Dependent workflow artifact

Description

Workflow document query

This query defines the data in Microsoft Dynamics AX 2012 that a workflow acts on and exposes certain fields that the business process owner uses for constructing conditions in the graphical workflow editor. The query is defined under the Queries node in the AOT and it is required for all workflows.

Workflow document class

This X++ class references the workflow document query and any calculated fields to be made available when constructing conditions. This class is created under the AOTClasses node and extends the WorkflowDocument base class. This class is required because workflow types and elements must bind to a workflow document class.

For information about derived data, see the section Key workflow concepts, earlier in this chapter.

SubmitToWorkflow class

This X++ class is the menu item class for the SubmitToWorkflow menu item that displays the Submit To Workflow dialog box in the Microsoft Dynamics AX 2012 user interface. The Submit To Workflow dialog box allows the user to enter comments associated with the submission. A SubmitToWorkflow class then activates the workflow. If state is being managed in the record that has been submitted to workflow, this class can be used to update the state of the record.

This class is created under the Classes node of the AOT.

State model

A defined set of states and state transitions (supported changes from one state to another) used to track the status of workflow document records during their life cycle. For example, a document can have the following states: Not Submitted, Submitted, ChangeRequested, or Approved. There is currently no state model infrastructure in Microsoft Dynamics AX, so you must implement any state model that is required.

For more information, see the section State management, later in this chapter.

Event handlers

Event handler code consists of business logic that is written in X++ and then referenced in the workflow type, the approval element, approval outcomes, the task element, task outcomes, and the automated task element. If a workflow document has an associated state model, you must write event handler code to transact workflow document records through the state model when being processed by using workflow. Event handler X++ code is created under the AOTClasses node.

Action and display menu items

For information about menu items, see the section Key workflow concepts, earlier in this chapter.

Both types of menu item are created under the AOTMenu Items or AOTWebWeb Menu Items node.

For more information about the menu items used in the workflow infrastructure, see “How to: Associate an Action Menu Item with a Workflow Task or Approval Outcome” (http://msdn.microsoft.com/en-us/library/cc602158.aspx) and “How to: Associate a Display Menu item with a Workflow Task or Approval” (http://msdn.microsoft.com/en-us/library/cc604521.aspx).

Custom workflow providers

If the functionality of the workflow providers included with Microsoft Dynamics AX 2012 isn’t adequate for a given set of requirements, you can develop your own workflow provider. Custom workflow provider X++ classes are created under the AOTClasses node and then referenced in one or more providers (under the AOTWorkflowProviders node).

For more information about workflow providers, including where they are used, see “Workflow Providers Overview” at http://msdn.microsoft.com/en-us/library/cc519521.aspx.

canSubmitToWorkflow method

This method is required to inform the workflow common UI controls that the record in the form is ready to be submitted to the workflow. While in Microsoft Dynamics AX 2009, the form canSubmitToWorkflow method was overridden, in Microsoft Dynamics AX 2012, this logic can be implemented on the table’s canSubmitToWorkflow method instead.

State management

A state model defines a set of states and the transitions that are permitted between the states for a given record type, along with an initial state and a final state. State models exist to provide a prescriptive life cycle for the data they are associated with. The current state value is often stored in a field on a record. For example, the PurchReqTable table (the header for a purchase requisition) has a status field that is used to track the approval state of a purchase requisition. The business logic for purchase requisitions is coded to respect the meaning of each state and the supported state transitions so that a purchase requisition record can’t be converted into a purchase order before the state is approved.

The simplest way to add and manage the state on a record is to use a single field to store the current state, but you have to determine the approach that makes the most sense. You would then create a static X++ class that implements the business logic that governs the state transition. Conceptually, you can think of this class as a StateManager class. All existing business logic that performs the state transitions should be refactored to use this single, central class to perform the state transitions, in effect isolating the state transition logic into a single class. From a workflow perspective, state transitions always occur at either the beginning or the conclusion of a workflow element. This is why all workflow tasks and workflow approvals have EventHandlers that can be used to invoke a StateManager class. Figure 8-12 shows the dependency chain between an event handler and the workflow document state.

State management dependency chain.

Figure 8-12. State management dependency chain.

When you decide to enable a workflow for a table in Microsoft Dynamics AX 2012 and determine that the table has a state that must be managed, you must refactor all business logic to respect the state model that you define to avoid unpredictable results. Create operations should always create a record with the initial state (for the state model). Update operations must respect the current state and fail if the state isn’t as expected. For example, it shouldn’t be possible to change the business justification of a purchase requisition after it has been submitted for approval. Managing the state of the record during each update so that the current state is verified and the next logical state is updated is typically implemented in the update method on the table by calling the StateManager class. If it returns a value of true, perform the update. If not, throw an exception and cancel the operation. Figure 8-13 shows a simple state model for a record.

A simple state model for approvals.

Figure 8-13. A simple state model for approvals.

In Figure 8-13, the initial state is NotSubmitted. When a record is submitted to workflow, the state changes to Submitted. After the workflow is activated, the state becomes PendingApproval. If a workflow participant selects the Request Change action, the state changes to ChangeRequested. After all approvals are submitted, the final state is Approved.

Create a workflow category

You use workflow categories to associate a workflow type with a module. This association restricts the list of types that are shown when the business process owner edits a workflow for a particular module, preventing a list of all workflow types from being displayed. For example, if a user is in the Accounts Payable module, the user sees only the workflow types that are bound to Accounts Payable. The mechanism behind this grouping is a simple metadata property on the workflow type called Workflow category. This property allows you to select an element from the module enum (AOTData DictionaryBase enumsModuleAxapta).

With this mechanism, it is easy for ISVs and partners who create their own modules to extend the module enum and thus have workflow types that can be associated with that module. Note that a workflow category can be associated with only one module.

Create the workflow document class

The purpose of a workflow is to automate all or part of a business process. To do this, it must be possible to define various rules for the document that is being processed by workflow. In Microsoft Dynamics AX 2012, these rules are called conditions. A business process owner creates conditions when modeling the workflow. For example, conditions can be used to determine whether a purchase requisition is approved automatically (without any human intervention). Figure 8-14 shows a simple condition defined in the graphical workflow editor.

A simple condition defined in the graphical workflow editor.

Figure 8-14. A simple condition defined in the graphical workflow editor.

When a business process owner defines a condition by using the graphical workflow editor, he or she needs to make sure that users have a way to select the fields from the workflow documents they want to use. On the surface, this seems simple, but two requirements complicate the task. First, not all the fields in a table might make sense to the business process owner, and therefore only a subset of the fields should be exposed. Second, it must be possible to use calculated fields (also called derived data). The workflow document class meets these two requirements by functioning as a thin wrapper around an AOT query that defines the available fields and by providing a mechanism for defining calculated fields.

The AOT query enables developers to define a subset of fields from one or more related tables. By adding nested data sources in a query, you can model complex data structures. However, the most common usage is to model a header-line pattern. At design time, when the business process owner is editing a workflow, the AOT query is used by the condition editor to determine which fields to display to the business process owner.

The workflow infrastructure uses a prescriptive pattern to support calculated fields by using parm methods that are defined within the workflow document class. These methods must be prefixed with parm and must implement a signature of (CompanyId, TableId, RecId). The workflow infrastructure then, at run time, calls the parm method and uses the return value in the condition evaluation. This design enables developers to implement calculated fields in parm methods on the workflow document class.

Note

When the expression builder constructs the list of fields, it uses the labels for the table fields as the display names for the fields. The display name for a calculated field is defined by the extended data type label of the return types. For enums, this is defined by the enum element label.

Creating a workflow document class involves creating an X++ class that extends WorkflowDocument. You must override the getQueryName method to return the name of the workflow document query. Figure 8-15 shows a sample X++ class that extends WorkflowDocument.

A sample X++ class that extends from the workflow document.

Figure 8-15. A sample X++ class that extends from the workflow document.

Creating a parm method involves adding a method to the workflow document class and then adding X++ code to calculate or otherwise determine the value to be returned, as shown in Figure 8-16.

A parm method within a workflow document class that returns the approval amount (which is calculated).

Figure 8-16. A parm method within a workflow document class that returns the approval amount (which is calculated).

Add a workflow display menu item

Workflow display menu items enable users to navigate directly to the Microsoft Dynamics AX 2012 client form (or Enterprise Portal webpage) from which they can select one of the available workflow actions. A user is prompted to participate in a workflow when he or she receives a work item from the workflow at run time. When viewing the work item, the user can click Go To <Label>. This button is automatically mapped to the workflow display menu item, and the button text (<Label>) is the label of the root table of the workflow document query.

This design enables developers to create task-based forms that are focused on the particular task at hand, rather than having to create monolithic forms that assume the user knows where in the process he or she is acting and which fields and buttons to use.

Activate the workflow

Workflows in Microsoft Dynamics AX 2012 are always explicitly activated; either a user does something in the Microsoft Dynamics AX 2012 client or in Enterprise Portal that causes workflow processing to start, or the execution of business logic starts a workflow. (Once you understand how users activate a workflow, you can use this knowledge to activate workflows through business logic.)

For the first activation approach to work, the workflow infrastructure must have a way to communicate information to the user about what to do. For example, it might be relevant to instruct the user to submit the purchase requisition for review and approval at the appropriate time. The requirements to communicate with users throughout the workflow life cycle gave Microsoft an opportunity to standardize the way users interact with workflow in both the Microsoft Dynamics AX 2012 client and Enterprise Portal, including activating a workflow, and this resulted in the development of workflow common UI controls. The workflow common UI controls include the yellow workflow message bar (highlighted in Figure 8-17) and the workflow action button, labeled Submit.

A purchase requisition ready to be submitted to workflow for processing.

Figure 8-17. A purchase requisition ready to be submitted to workflow for processing.

The workflow common UI controls appear on the Purchase requisition form because that form has been enabled for workflow. To enable workflow in a form, you set the WorkflowEnabled property on the form to Yes in the Properties window, which is shown in Figure 8-18. You must also set the WorkflowDataSource property to one of the data sources on the form. The selected data source must be the same as the root data source that is used in the query referenced by the workflow document. Finally, you can set the WorkflowType property to constrain the form to use a specific workflow type.

Design properties for a Microsoft Dynamics AX 2012 form, including those for workflow.

Figure 8-18. Design properties for a Microsoft Dynamics AX 2012 form, including those for workflow.

If workflow is enabled for a form, the workflow common controls automatically appear in three cases:

  • When the currently selected document can be submitted to workflow (the canSubmitToWorkflow table or form method returns true)

  • When the current user is the originator of a workflow that has acted on the currently selected document

  • When the current user has been assigned to a work item for which he or she must take an action

The workflow common control uses the algorithm shown in Figure 8-19 to determine which workflow to use.

After a workflow has been identified, it’s easy for the workflow common UI controls to obtain the SubmitToWorkflow action menu item. This action menu item is then dynamically added to the form, along with the yellow workflow message bar.

If you look at the SubmitToWorkflow action menu item for the PurchReqApproval workflow type, you’ll notice that it is bound to the PurchReqWorkflow class. When you click the Submit button, the action menu items call the main method on the class it is bound to; thus, the code that activates the workflow is called from the main method. In this case, the call to the workflow activation API has been isolated within the submit method.

Workflow activation logic flowchart.

Figure 8-19. Workflow activation logic flowchart.

In Figure 8-20, notice how the Workflow::activatefromWorkflowType method is used. You can use two additional APIs to activate workflows: Workflow::activatefromWorkflowConfiguration and Workflow::activateFromWorkflowSequenceNumber.

The Submit method for the purchase requisition workflow.

Figure 8-20. The Submit method for the purchase requisition workflow.

For information about how to use these APIs, see the Microsoft Dynamics AX 2012 developer documentation on MSDN: http://msdn.microsoft.com/en-us/library/cc586793.aspx.

Understanding how to activate a workflow is important, but it is equally important to understand how to prevent a workflow from being activated. For example, you don’t want a user to submit a record to workflow before it is in a state to be submitted. An override method on the table or form, canSubmitToWorkflow, addresses this requirement. The canSubmitToWorkflow method returns a Boolean value. A value of true indicates that the record can be submitted to workflow. When the workflow data source on the form is initialized or when the record changes, this method is called; if it returns true, the Submit button is enabled. Typically, you should update the state of the document after invoking the workflow activation API so that you can correctly denote whether a document has been submitted to workflow. (In Figure 8-20, the purchase requisition is transitioned to the In Review state.)

Note

If the canSubmitToWorkflow method hasn’t been overridden either at the table or form level, the workflow common UI controls won’t appear, leaving a reserved space at the top of the form usually occupied by the controls.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset