ax2009_enus_deviv_11

30
Chapter 11: Workflow 11-1 CHAPTER 11: WORKFLOW Objectives The objectives are: Identify the components required prior to using workflow Specify which application module a workflow is applicable to using a workflow category Create a new workflow template Link tables to workflows using a workflow document Define what happens when the workflow is approved or denied. Apply a workflow to a form Create Event Handlers and apply them to a workflow Configure a workflow Submit a record for workflow processing Use the workflow processor Introduction Workflow is a module in Microsoft Dynamics ® AX 2009, that allows flexible task and approval routes for documents created by users. For example, a purchase requisition may need to be approved by a number of different employees according to the requisition's total amount, and each employee has to approve it before the next employee in the approval route. A Workflow in Microsoft Dynamics AX uses a combination of AOT elements created by IT, and configuration that may be set up by a user. This lesson introduces the development side of creating an workflow, for which you will need to use skills developed from this class and the Morph X development class. Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Upload: varun-shah

Post on 23-Oct-2014

123 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-1

CHAPTER 11: WORKFLOW Objectives

The objectives are:

• Identify the components required prior to using workflow • Specify which application module a workflow is applicable to using

a workflow category • Create a new workflow template • Link tables to workflows using a workflow document • Define what happens when the workflow is approved or denied. • Apply a workflow to a form • Create Event Handlers and apply them to a workflow • Configure a workflow • Submit a record for workflow processing • Use the workflow processor

Introduction Workflow is a module in Microsoft Dynamics® AX 2009, that allows flexible task and approval routes for documents created by users. For example, a purchase requisition may need to be approved by a number of different employees according to the requisition's total amount, and each employee has to approve it before the next employee in the approval route.

A Workflow in Microsoft Dynamics AX uses a combination of AOT elements created by IT, and configuration that may be set up by a user. This lesson introduces the development side of creating an workflow, for which you will need to use skills developed from this class and the Morph X development class.

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

Page 2: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-2

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 that takes the customer over their credit limit, the sales order should be 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 Installation A number of the workflow system components are required to be installed before you can begin to create and configure workflows in Microsoft Dynamics AX.

• Workflow website. This is an IIS website that controls the flow of the workflows.

• Workflow accounts. There are two accounts used - a system account used to provide access to the workflow tables, and an execution account that is used to execute business logic.

• Microsoft Dynamics AX workflow server component. This is the workflow engine and is installed using the Microsoft Dynamics AX installation files. The website and the accounts are required to run this installation.

NOTE: This course does not cover the installation of the workflow system components; however you need to be aware of the requirements. For more information about workflow installation, refer to the Administrator Guide.

Create a Workflow Category A workflow category defines the module in which the workflow will be available. Modules are defined by the SysModule enum.

Demonstration: Creating a Workflow Category

This demonstration shows you how to create a category that allows the workflow to be configured from the Projects module.

1. Open the AOT 2. Expand the Workflow node 3. Right-click on the Workflow Category 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 SalesCreditLimitApproval

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

Page 3: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-3

6. Change the label property to Sales credit limit approval. 7. Change the Module property to Cust. 8. Right-click on the newly created workflow category and select Save.

FIGURE 11.1

Create a Workflow Template A workflow template brings all the different elements of the workflow together. Workflow configurations are created based on a template, and many configurations can be based on the same template. The template defines which actions are allowed and which are required.

Demonstration: Creating a Workflow Template

This demonstration creates a workflow template and binds 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 Templates node and select New

Workflow Template. A new workflow template named WorkflowTemplate1 will be created.

4. Right-click on the newly created workflow template and select Properties.

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

Page 4: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-4

5. Change the name property to SalesCreditLimitApproval. 6. Change the label property to Sales credit limit approval. 7. Change the category property to SalesCreditLimitApproval. 8. Right-click on the newly created workflow template and select Save.

FIGURE 11.2

Create a Workflow Document A workflow document defines what data is affected by the workflow. It can define one or more tables and all or selected fields on that table. This is done by using a query.

Demonstration: Creating a Workflow Document

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 template.

1. Open the AOT. 2. Right-click on the Query 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.

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

Page 5: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-5

7. Find the table SalesTable. 8. Drag the SalesTable table to the Data Sources node of the

SalesCreditLimitApproval query. 9. Right click on the SalesCreditLimitApproval query and select Save. 10. In the AOT, right-click on the Classes node and select New Class. 11. Copy the following code into the ClassDeclaration. 12. Press F8 to save the method 13. Right-click on the class and select override method >

getQueryName. 14. Copy the following code in to the method. 15. Press F8 to save the method 16. Find the SalesCreditLimitApproval workflow template in the AOT. 17. Right-click on the workflow template and select Properties. 18. In the document property, enter SalesCreditLimitApproval. 19. Right-click on the workflow template and select Save.

class ProjTimeApproval extends workFlowDocument { }

QueryName getQueryName() { return queryStr(SalesCreditLimitApproval); }

Create a Workflow Approval An approval route may contain a number of outcomes. It 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 specific code by specifying a menu item for each item.

Demonstration: Creating a Workflow Approval

This demonstration creates a workflow approval and specifies how the approval route can be defined.

1. Open the AOT. 2. Expand the workflow node. 3. Right-click on approvals and select New Approval. 4. Right-click on the newly created approval and select properties. 5. Change the Name property to SalesCreditLimitApproval. 6. Change the Document property to SalesCreditLimitApproval. 7. Change the ParticipantProvider property to

WorkflowUserGroupParticipantProvider.

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

Page 6: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-6

8. Change the DueDateProvider to WorkflowWorkCalendarDueDateProvider.

9. Change the HierarchyProvider to WorkflowLimitHierarchyProvider. 10. Change the DocumentMenuItem to SalesTable.

The Providers specify classes that enable rules to be defined for the workflow. These providers are standard application classes but can be overridden and modified, or other providers can be used in their place.

FIGURE 11.3

Demonstration: Creating Approval Outcomes

Use a standard class that acts as an engine for all approval outcomes.

You are not required to do anything but set the workflow to either Approved or Denied, therefore call the same class from two different menu items. The two menu items simply allow you to use two different labels. In more complex workflows it may be necessary to override or copy and modify this class rather than use it directly.

1. Open the AOT. 2. Expand Menu Items. 3. Right-click on Action and select New Menu Item. 4. Right-click on the newly created Action item and select Properties. 5. Change the Name property to SalesCreditLimitApprove.

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

Page 7: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-7

6. Change the Label property to Approve. 7. Change the ObjectType property to Class. 8. Change the Object property to WorkflowWorkItemActionManager. 9. Right-click on the SalesCreditLimitApprove menu item and select

Save. 10. Right-click on Action and select New Menu Item. 11. Right-click on the newly created Action item and select Properties. 12. Change the Name property to SalesCreditLimitReject. 13. Change the Label property to Reject. 14. Change the ObjectType property to Class. 15. Change the Object property to WorkflowWorkItemActionManager. 16. Right-click on the SalesCreditLimitReject menu item and select

Save. 17. In the AOT, expand Workflow > Approvals >

SalesCreditLimitApproval > Outcomes. 18. Right-click on the Approve node and select Properties. 19. Change the ActionMenuItem property to SalesCreditLimitApprove. 20. Right-click on the Reject node and select Properties. 21. Change the ActionMenuItem property to SalesCreditLimitReject. 22. Right-click on the Deny node and select Properties. 23. Change the Enabled property to No. 24. Right-click on the RequestChange node and select Properties. 25. Change the Enabled property to No 26. Right-click on the SalesCreditLimitApproval approval node and

select Save.

FIGURE 11.4

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

Page 8: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-8

Demonstration: Attaching an Approval to a Template

The approval needs to be attached to the template. This demonstration shows you how to attach an approval to a template.

1. Open the AOT. 2. Expand Workflow > Workflow Templates >

SalesCreditLimitApproval. 3. Open another AOT window. 4. Expand Workflow > Approvals. 5. Find SalesCreditLimitApproval. 6. Drag the SalesCreditLimitApproval approval to the Required

Elements node of the SalesCreditLimitApproval template. 7. Right-click on the SalesCreditLimitApproval workflow template and

select save.

Enable Workflow on a Form Now that the workflow template is defined, you can specify which forms will use this template.

Demonstration: Add a WorkflowState Field

Any form that uses the same table in the datasource as is specified in a workflow document is able to use that document for workflow. This demonstration shows how to enable workflow on the Project Hours journal 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.

1. Open the AOT. 2. Expand Data Dictionary. 3. Right-click on Base Enums and select New 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.

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

Page 9: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-9

Demonstration: Enable Workflow on the Form

Workflow on the form is enabled using properties on the design node, and by overiding a form method. This demonstration shows you how to enable workflow on a form.

1. Open the AOT. 2. Expand Tables > SalesTable. 3. Create a new method and add the first method in the following code. 4. Save the changes made to the table. 5. Expand Forms > SalesTable > Designs. 6. Right-click on the design node and select Properties. 7. Change the WorkflowEnabled property to Yes. 8. Change the WorkflowDatasource property to SalesTable. 9. Right-click on the form Methods node and select Override

Method > canSubmitToWorkflow. 10. Copy the second method in the following code into the method.

boolean canSubmitToWorkflow() { 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; }

public boolean canSubmitToWorkflow() { return salesTable.canSubmitToWorkflow(); }

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

Page 10: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-10

The canSubmitToWorkflow method returns true if the ProjJournalTable record has not already been submitted and the total hours entered in the transactions is greater than 40.

Demonstration: Create a Submit to Workflow Class

To submit a document to workflow, call standard code to prompt the user for a comment and to process the submisson. This demonstration shows you how to create a submit to workflow class.

1. Open the AOT and create a new class. 2. Copy the following code in to the classDeclaration, submit method

and the main method. 3. Press F8 to save and compile the code. 4. Open another AOT and expand Menu Items. 5. Drag the SalesCreditLimitSubmit class to the Actions node. 6. Right-click on the newly created Actions node and select Properties. 7. Change the Label property to Submit. 8. Right-click on the SalesCreditLimitSubmit menu item and select

Save. 9. Locate the workflow template node in the AOT. 10. Right-click and select Properties. 11. Change the SubmitToWorkFlowMenuItem property to

SalesCreditLimitSubmit.

class SalesCreditLimitSubmit { }

void submit(Args args) { // Variable declaration. recId recId = args.record().RecId; WorkflowCorrelationId workflowCorrelationId; // Hardcoded template name WorkflowTemplateName workflowTemplateName = workflowtemplatestr(SalesCreditLimitApproval); // 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().getActiveWorkflowConfiguration());

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

Page 11: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-11

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::activateFromWorkflowTemplate(workflowTemplateName, recId, note, NoYes::No); SalesTable.CreditLimitApprovalStatus = SalesCreditLimitApprovalStatus::Submitted;

// Send an Infolog message. info("Submitted to workflow.");

ttscommit; }

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

args.caller().updateWorkFlowControls(); }

public static void main(Args _args) { SalesCreditLimitSubmit SalesCreditLimitSubmit = new SalesCreditLimitSubmit(); ;

SalesCreditLimitSubmit.submit(_args);

}

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

Page 12: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-12

Create Event Handlers Event handlers are used to execute business logic at specific events in the 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 WorkflowStartedEventHandler This event raises when the

workflow instance starts.

WorkflowCompletedEventHandler This event raises when the workflow instance ends after it is completed.

WorkflowCanceledEventHandler This event raises when the workflow instance ends after it is canceled. Use this event handler to perform any clean up operations needed.

WorkflowConfigDataChangeEventHandler This event raises when the workflow configuration data changes. Use this event handler to identify when a configuration has changed. For example, if you create an association between the application data and a workflow configuration, this event handler would raise if the configuration was deleted or updated.

Demonstration: Add Workflow Level Event Handlers

This demonstration shows you how to add workflow level event handlers.

1. Create a new class. Add the following ClassDeclaration and methods.

2. Open the properties form for the SalesCreditLimitApproval workflow template.

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

Page 13: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-13

3. Set StartedEventHandler, CompletedEventHandler and CanceledEventHandler properties to SalesCreditLimitEventHandler

class SalesCreditLimitEventHandler implements WorkflowStartedEventHandler, WorkflowCanceledEventHandler, WorkflowCompletedEventHandler { }

public void cancelled(WorkflowEventArgs _workflowEventArgs) { SalesTable SalesTable; ;

ttsbegin;

select forupdate SalesTable where SalesTable.RecId == _workflowEventArgs.parmWorkflowContext().parmRecId();

SalesTable.CreditLimitApprovalStatus = SalesCreditLimitApprovalStatus::NotSubmitted;

SalesTable.update();

ttscommit;

}

public void completed(WorkflowEventArgs _workflowEventArgs) { SalesTable SalesTable; ;

ttsbegin;

select forupdate SalesTable where SalesTable.RecId == _workflowEventArgs.parmWorkflowContext().parmRecId();

if (salesTable.CreditLimitApprovalStatus == SalesCreditLimitApprovalStatus::Submitted) { SalesTable.CreditLimitApprovalStatus = SalesCreditLimitApprovalStatus::Approved;

SalesTable.update(); }

ttscommit;

}

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

Page 14: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-14

public void returned(WorkflowEventArgs _workflowEventArgs) { SalesTable SalesTable; ;

ttsbegin;

select forupdate SalesTable where SalesTable.RecId == _workflowEventArgs.parmWorkflowContext().parmRecId();

SalesTable.CreditLimitApprovalStatus = SalesCreditLimitApprovalStatus::Rejected;

SalesTable.update();

ttscommit;

}

public void started(WorkflowEventArgs _workflowEventArgs) { SalesTable SalesTable; ;

ttsbegin;

select forupdate SalesTable where SalesTable.RecId == _workflowEventArgs.parmWorkflowContext().parmRecId();

SalesTable.CreditLimitApprovalStatus = SalesCreditLimitApprovalStatus::Submitted;

SalesTable.update();

ttscommit;

}

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

Page 15: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-15

Element Level Event Handlers

The interfaces at the workflow element level are as follows:

Event Description

WorkflowElementStartedEventHandler This event raises when the task or approval starts. For approvals, you can use this event to transition the workflow document state from Submitted to PendingApproval.

WorkflowElementCanceledEventHandler This event raises when the task or approval is canceled. For approvals, you can use this event to transition the workflow document state from the current state to Canceled.

WorkflowElementCompletedEventHandler This event raises when the task or approval is completed. For approvals, you can use this event to transition the workflow document state from PendingApproval to Approved.

WorkflowElementReturnedEventHandler This event raises when the task or approval is returned to the originator. For approvals, you can use this event to transition the workflow document state from the current state to RequestChange.

WorkflowElemChangeRequestedEventHandler This event raises when an approver requests a change to the task or approval. For approvals, you can use this event to transition the workflow document state from PendingApproval to RequestChange.

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

Page 16: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-16

Demonstration: Add Element Level Event Handlers

This demonstration shows you how to add element level event handlers.

1. Create a class and add the following ClassDeclaration and methods. 2. Find the SalesCreditLimitApproval Workflow Approval element 3. Expand the node and open the properties for the Reject node. 4. Set the EventHandler property to SalesCreditLimitElementHandler

class SalesCreditLimitElementHandler implements WorkflowElementCompletedEventHandler, WorkflowElementCanceledEventHandler, WorkflowElementReturnedEventHandler, WorkflowElemChangeRequestedEventHandler, WorkflowElementStartedEventHandler

{ }

public void returned(WorkflowEventArgs _workflowEventArgs) { SalesTable SalesTable; ;

ttsbegin;

select forupdate SalesTable where SalesTable.RecId == _workflowEventArgs.parmWorkflowContext().parmRecId();

SalesTable.CreditLimitApprovalStatus = SalesCreditLimitApprovalStatus::Rejected;

SalesTable.update();

ttscommit;

}

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

Page 17: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-17

Configure a Workflow Now that you have created a template and enabled it on a form, you can configure it for use.

Demonstration: Configuring a Workflow

This demonstration shows you how to configure a workflow from the main menu.

1. Open the main menu and select Accounts Receivable > Setup > Workflow configurations.

2. Click New. 3. Select Sales credit limit approval and click Create configuration. 4. Enter "Credit limit approval" as the name. 5. Click Create Instruction, enter "Please approve" and then click OK. 6. Click the Details tab, expand SalesCreditLineApproval, and click on

Step 1. 7. Under Step details, click on Assignment tab. 8. Click the Choose button. 9. Select User based, enter a user in the Select users field and then click

OK. 10. Close the approval form. 11. On the configuration form click Set as active. 12. Click the Overview tab. 13. Click Set as default.

The workflow is now ready for use.

Demonstration: Test the Workflow

This demonstration shows you how to can test the workflow by creating a timesheet

1. Select a customer from the customer table and set a credit limit. 2. Create a new sales order and create lines such that the balance of the

customer plus the total amount on the lines is greater than the credit limit.

3. The workflow submit button and dialog should appear. 4. Click the submit button and enter a comment. 5. Open the AOT. 6. Expand the Forms node. 7. Find the form Tutorial_WorkFlowProcessor. 8. Right-click on this form and select Open. 9. Click Start.

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

Page 18: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-18

10. When the form says that it has zero records in queue, click Stop and go back to the sales table form.

11. Select Actions > History. You will see that the document is waiting for approval by the person you assigned to approve it.

12. Logon as the user who should approve the sales order 13. Open the sales order form. 14. Click the workflow Actions button and select Approve. 15. Open the Tutorial_WorkflowProcessor form again and click Start,

wait for it to complete and click Stop. 16. The workflow has now been approved.

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

Page 19: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-19

Lab 11.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;

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

Page 20: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-20

}

boolean canPickingListBeUpdated() { ......

ok = ok && salesTable.canPostCreditLimit();

return ok;

}

boolean canPackingslipBeUpdated() { ......

ok = ok && salesTable.canPostCreditLimit();

return ok;

}

boolean canInvoiceBeUpdated() { ......

ok = ok && salesTable.canPostCreditLimit();

return ok;

}

Code Walkthrough: Submitting a workflow When a record is submitted to workflow, the main() method in the submit to workflow class is called.

1. View the ProjTimeApprovalsSTWF.main() method created in the Create A Submit to Workflow Class demonstration. The user is prompted for a comment while submitting the workflow

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

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

Page 21: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-21

The record is retrieved from the calling form and the recId is passed to the static method Workflow::activateFromWorkflowTemplate(), which runs the submit process.

ProjJournalTable = args.record(); // Get comments from the submit to workflow dialog. _initialNote = workflowSubmitDialog.parmWorkflowComment();

try { ttsbegin;

// Activate the workflow. _workflowCorrelationId = Workflow::activateFromWorkflowTemplate(_workflowTemplateName, _recId, _initialNote, NoYes::No);

2. View the method Workflow::activateFromWorkflowTemplate()

tableId = Workflow::getDocumentTableId(_workflowTemplateName); configTable = Workflow::findWorkflowConfigToActivateForTemplate(_workflowTemplateName, _recId, tableId);

The tableId that the workflow is to be performed on is retrieved from the query specified in the workflow document class.

The workFlowContext class holds all the relevant data for the workflow submission. The SysWorkFlowEventDispatcher class creates records that will be read by the Workflow Processor class to determine which actions should be executed in the next step of the workflow.

workflowContext = WorkflowContext::newRootWorkflowContext(curext(), tableId, _recId, correlationId);

try { SysWorkflowEventDispatcher::onWorkflowSubmit(workflowContext, _submittingUser, configTable.ConfigurationId, _initialNote, _activatingFromWeb); }

3. Return to the ProjTimeApprovalsSTWF.main() method

ProjJournalTable.WorkFlowState = true;

The journal is marked as submitted.

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

Page 22: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-22

Code Walkthrough: Workflow Processor All workflows are processed through a batch process. To view a simulation of how the workflows are processed, use the form Tutorial_WorkflowProcessor.

1. View the form method doMessageProcessing on the form Tutorial_WorkflowProcessor.

while select workflowWorkItemTable where workflowWorkItemTable.Type == WorkflowWorkItemType::WorkItem && (workflowWorkItemTable.Status == WorkflowWorkItemStatus::Pending || workflowWorkItemTable.Status == WorkflowWorkItemStatus::Delegated) && workflowWorkItemTable.DueDateTime < DateTimeUtil::getSystemDateTime() { WorkflowWorkItem::escalateWorkItem(SysWorkflowWorkItemContext::newWorkflowWorkItemContextFromWorkItem(workflowWorkItemTable));

cntWorkItems++; }

All records due for processing are retrieved. The WorkflowWorkItem::escalateWorkItem() is called

2. View the method WorkflowWorkItem::escalateWorkItem()

try { workItemId = SysWorkflowEventDispatcher::onWorkItemEscalation(_workItemContext); }

3. View the method SysWorkflowEventDispatcher::onWorkItemEscalation()

workItemTable = WorkflowWorkItemTable::findPendingActivityInstanceId(_workItemContext.parmWorkflowActivityInstanceKey().parmWorkflowActivityInstanceId(), true);

The workItemTable record is retrieved. This is the next pending activity on the workflow, based on the configuration.

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

Page 23: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-23

The action to be performed is examined

switch (stepTable.EscalationType) { case WorkflowEscalationType::Action: workItemInstanceId = workItemTable.Id;

The next step is completed.

workItemTable.Status = SysWorkflowEventDispatcher::completeWorkItem( _workItemContext, workItemTable, stepTable.EscalationAction, workItemTable.UserId, workflowTable.Originator, // always set the auto-escalate user to the workflow originator "@SYS110277");

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

Page 24: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-24

Lab 11.2 - Enable Resubmit Scenario

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

Challenge Yourself!

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

Use the PurchReqWorkflow class as inspriration.

Step by Step

1. Create a new action menu item called SalesCreditLimitResubmit 2. Set the ObjectTypePropety to class, the Object property to

SalesCreditLimitSubmit and the Label property to Resubmit. 3. Modify the Main method on the SalesCreditLimitSubmit class and

add a new method Resubmit as follows: 4. On the SalesCreditLimitApproval approval element, set the

ResubmitMenuItem property to SalesCreditLimitResubmit

public static void main(Args _args) { SalesCreditLimitSubmit SalesCreditLimitSubmit = new SalesCreditLimitSubmit(); ;

if (_args.menuItemName() == menuitemactionstr(SalesCreditLimitSubmit)) { SalesCreditLimitSubmit.submit(_args); } else { SalesCreditLimitSubmit.resubmit(_args); }

}

void resubmit(Args args) { // Variable declaration. recId _recId = args.record().RecId; WorkflowCorrelationId _workflowCorrelationId; // Hardcoded template name WorkflowTemplateName _workflowTemplateName = workflowtemplatestr(SalesCreditLimitApproval);

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

Page 25: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-25

// Initial note is the information that users enter when they // submit the document for workflow. WorkflowComment _initialNote =""; WorkflowWorkItemActionDialog WorkflowWorkItemActionDialog; SalesTable SalesTable; ;

// 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.");

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

Page 26: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-26

ttscommit; }

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

} }

args.caller().updateWorkFlowControls(); }

Summary The workflow module is a highly configurable and flexible module. However, by using Morph X and some standard code templates, it can be configured for any part of the Microsoft Dynamics AX application.

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

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

Page 27: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-27

Test Your Knowledge 1. Which application element is used to define to which module a workflow is

applicable?

( ) Workflow template ( ) Workflow category ( ) A field in the workflow configuration ( ) SalesTable

2. Which type of AOT element needs to be created to specify which tables will be affected by a workflow?

( ) Extended data type ( ) Class ( ) Form ( ) Query

3. There are three types of providers that define what rules the workflow can follow. What are they?

( ) Participant provider ( ) DueDate provider ( ) Hierarchy provider ( ) Internet provider

4. Which two properties on a form data source need to be modified to allow the form to use a workflow?

( ) WorkflowTemplate ( ) WorkflowEnabled ( ) WorkflowDocument ( ) WorkflowDatasource

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

Page 28: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-28

Quick Interaction: Lessons Learned Take a moment and write down three key points you have learned from this chapter

1.

2.

3.

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

Page 29: AX2009_ENUS_DEVIV_11

Chapter 11: Workflow

11-29

Solutions Test Your Knowledge

1. Which application element is used to define to which module a workflow is applicable?

( ) Workflow template (•) Workflow category ( ) A field in the workflow configuration ( ) SalesTable

2. Which type of AOT element needs to be created to specify which tables will be affected by a workflow?

( ) Extended data type ( ) Class ( ) Form (•) Query

3. There are three types of providers that define what rules the workflow can follow. What are they?

(√) Participant provider (√) DueDate provider (√) Hierarchy provider ( ) Internet provider

4. Which two properties on a form data source need to be modified to allow the form to use a workflow?

( ) WorkflowTemplate (√) WorkflowEnabled ( ) WorkflowDocument (√) WorkflowDatasource

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

Page 30: AX2009_ENUS_DEVIV_11

Development IV in Microsoft Dynamics® AX2009

11-30

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