How to: Enable a State Model for a Workflow Document

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

Workflow documents in a Microsoft Dynamics AX workflow must implement a state model to represent the state of a workflow, approval, or task for a workflow document in the application. For example, a workflow document in the NotSubmitted state can be changed to the Submitted state when a user clicks the Submit button. This section describes how to enable the state model for a workflow document by using a class or by creating a method on a table.

The following table contains recommended states. However, the needs of your application will determine what states are needed, validation type for state transitions, and how many states are needed. For more information, see Workflow Approval State Transitions.

For the state of the

Use

Workflow document

  • NotSubmitted

  • Submitted

  • ChangeRequested

  • Returned

  • Completed

Typically, the workflow state should be persisted in the root table of your workflow document data source. To add workflow state to a table, create an enum in the BaseEnums node of the Application Object Tree (AOT) that contains the workflow states shown in the previous table. The first enum defined should be the NotSubmitted enum, and this will also be the default setting in the table. After the enum is defined, drag the enum to the Fields node of the workflow document data source table. For more information, see Enums.

After a state model is defined, you can enable forms and lists to work with workflow. For more information, see How to: Enable a Form or List for Workflow.

Note

Workflow state can be implemented in many ways. The following procedure shows one way of performing this task.

To create a state change manager class

  1. In the AOT, expand the Classes node.

  2. Right-click the Classes node, and then select New Class. A class group displays under the Classes node.

  3. Right-click the new class, click Rename, and then enter a name for the class.

  4. Right-click the new class and then click New Method. A method node named method1 displays under the Classes node.

  5. Right-click method1 and then click Edit. Enter the following code for the started event method.

        public static void started(RecID _recID)
        {
            <insert method here>
        }
  1. Repeat steps 4 and 5 for the remaining states.

Example

The following code is part of the setWorkflowState on the PurchReqTable Table. This method sets the workflow state field in the table and updates the user interface with a user friendly status, and then updates the state in the database.

    static void setWorkflowState(RecId _purchReqRecId, PurchReqWorkflowState _purchReqWorkflowState)
    {
        // Declare variables.
        PurchReqTable purchReqTable;
    
        ttsbegin;
     
        // Selects the record to be updated based on the recId and then
        // sets the workflow state. 
        purchReqTable = PurchReqTable::findRecId(_purchReqRecId, true);
        purchReqTable.State = _purchReqWorkflowState;
    
        // Based on the workflow state, the status in the user interface
        // is updated with a user friendly status.
        switch (_purchReqWorkflowState)
        {
            case PurchReqWorkflowState::Submitted:
                purchReqTable.Status = PurchReqStatus::Submitted;
                break;
            case PurchReqWorkflowState::NotSubmitted:
                purchReqTable.Status = PurchReqStatus::Draft;
                break;
            case PurchReqWorkflowState::Completed:
                purchReqTable.Status = PurchReqStatus::Completed;
                break;
            case PurchReqWorkflowState::Returned:
                purchReqTable.Status = PurchReqStatus::Rejected;
                break;
            case PurchReqWorkflowState::ChangeRequest:
                purchReqTable.Status = PurchReqStatus::ChangeRequested;
                break;
        }
     
        // Commits the state to the database. 
        purchReqTable.update();
     
        ttscommit;
    }

See also

Requirements for Enabling Workflow in an Application Module

How to: Enable a Form or List for Workflow

Workflow Approval State Transitions

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.