PXAccumulator: To Modify the Processing Form to Use the Field Updated by PXAccumulator

The following activity will walk you through the modification of the processing form to use the field updated by the custom PXAccumulator attribute.

Story

The Assign Work Orders (RS501000) processing form was developed for the Smart Fix company, and now its managers would like to make the automatic assignment of work orders on this form more efficient:

  • Repair work orders should be automatically assigned to the employee with the fewest repair work orders assigned.
  • If multiple employees have the smallest number of repair work orders assigned, the work orders will be assigned to the first of these employees to be selected from the database.

For the Assign Work Orders form, you’ve already implemented the custom PXAccumulator attribute, which counts the number of repair work orders assigned to each employee and updates this number in the database during processing. Now you need to modify the form so that it contains information about the number of work orders assigned to the potential assignees who are listed in the table.

In the table on the Assign Work Orders form, you need to have the following columns related to the employees and the number of assigned work orders:
  • Assignee: The assignee selected on the Repair Work Orders (RS301000) form for the work order. The column is empty if no value has been selected on the Repair Work Orders form. You’ll add this column temporarily for testing purposes; it won’t be editable.
  • Default Assignee: The default assignee, which is calculated as the employee with the lowest number of assigned work orders. To make sure that the values in the Assign To column are calculated correctly during testing, you’ll display the Default Assignee column, and it won’t be editable. (You will delete this column after testing.)
  • Assign To: The employee to which the repair work order will be assigned during processing. By default, the system inserts the value from the Assignee column for this work order if it isn’t null. If it’s null, the system inserts the value copied from the Default Assignee column. A user can override the default value in this column.
  • Number of Assigned Work Orders: An uneditable column showing the number of work orders assigned to the assignee that’s specified in the Assignee column.

Process Overview

You will modify the constructor of the RSSVAssignProcess graph to make the Assign To column editable.

In the RSSVWorkOrderEntry graph, you will also modify the implementation of the AssignOrders() method and add the complete() action handler so that 1 is added to or subtracted from the number of assigned work orders. The value specified for the number of assigned work orders in the AssignOrders() method and the complete() action handler will be added to the value stored in the database by the custom PXAccumulator attribute.

You’ll then modify the TypeScript code of the Assign Work Orders (RS501000) form and test the changes on the form.

System Preparation

Before you begin performing the steps of this activity, perform these prerequisite activities:

  1. Test Instance for Customization: To Deploy an Instance with a Custom Form that Implements a Workflow to prepare an MYOB Acumatica instance
  2. Processing Forms: To Create a Simple Processing Form to create a processing form without filtering parameters
  3. Filtering Parameters: To Add a Filter for a Processing Form to add filtering parameters to the form
  4. PXAccumulator: To Implement a Custom Accumulator Attribute to implement a custom accumulator attribute
  5. CacheAttached: To Replace Field Attributes in CacheAttached to define the business logic for particular fields
  6. To Fetch Calculated Data from a Non-Scalar Source (in RowSelecting) to define the business logic for fetching calculated fields

Step 1: Enabling the Editing of the Field

Edit the RSSVAssignProcess graph as follows:
  1. In the constructor of the RSSVAssignProcess graph, replace the Assignee field with the AssignTo field of the RSSVWorkOrder DAC. The resulting code of the constructor is shown in the following code.
            public RSSVAssignProcess()
            {
                WorkOrders.SetProcessCaption("Assign");
                WorkOrders.SetProcessAllCaption("Assign All");
                PXUIFieldAttribute.SetEnabled<RSSVWorkOrder.assignTo>(
                    WorkOrders.Cache, null, true);
            }
  2. Build the project.

Step 2: Modifying the Assignment and Completion Operations

In this step, you will modify the AssignOrders() static method and add the complete() action handler of the RSSVWorkOrderEntry graph so that they change the number of assigned work orders for each employee who’s assigned a repair work order or who completed a repair work order. You’ll assign 1 or -1 (depending on whether the work order is assigned or completed) to the RSSVWorkOrder.NbrOfAssignedOrders field; the custom accumulator attribute will add this value to the value stored in the database.

Do the following to modify the AssignOrders() method and add the complete() action handler:

  1. In the RSSVWorkOrderEntry graph, define the data view for the calculation of the number of assigned work orders per employee, as shown in the following code.
            //The view for the calculation of the number of assigned work orders 
            //per employee
            public SelectFrom<RSSVEmployeeWorkOrderQty>.View Quantity = null!;
  2. In the AssignOrders() method of the RSSVWorkOrderEntry graph, add the following line in the beginning of the try clause.
                        workOrder.Assignee = workOrder.AssignTo;
  3. In the AssignOrders() method of the RSSVWorkOrderEntry graph, add the following code before the workOrderEntry.Actions.PressSave() call.
                        //Modify the number of assigned orders for the employee.
                        RSSVEmployeeWorkOrderQty employeeNbrOfOrders =
                            new RSSVEmployeeWorkOrderQty();
                        employeeNbrOfOrders.UserID = workOrder.Assignee;
                        employeeNbrOfOrders.NbrOfAssignedOrders = 1;
                        workOrderEntry.Quantity.Insert(employeeNbrOfOrders);
  4. In the RSSVWorkOrderEntry graph, replace the definition of the Complete action with the following code.
            public PXAction<RSSVWorkOrder> Complete = null!;
            [PXButton]
            [PXUIField(DisplayName = "Complete", Enabled = false)]
            protected virtual IEnumerable complete(PXAdapter adapter)
            {
                // Get the current order from the cache
                RSSVWorkOrder row = WorkOrders.Current;
                //Modify the number of assigned orders for the employee
                RSSVEmployeeWorkOrderQty employeeNbrOfOrders =
                    new RSSVEmployeeWorkOrderQty();
                employeeNbrOfOrders.UserID = row.Assignee;
                employeeNbrOfOrders.NbrOfAssignedOrders = -1;
                Quantity.Insert(employeeNbrOfOrders);
                // Trigger the Save action to save changes in the database
                Actions.PressSave();
                return adapter.Get();
            }
  5. Rebuild the project.

Step 3: Adjusting the TypeScript Code (Self-Guided Exercise)

Perform the following general steps on your own:
  1. In the RS501000.ts file, add the Default Assignee, Assign To, and Number of Assigned Work Orders columns to the table on the Assign Work Orders (RS501000) form. Hide the selector links for the Default Assignee and Assign To columns.
  2. Remove PXFieldOptions.CommitChanges for the Assignee column.
  3. For the Assign To column, set the PXFieldOptions.CommitChanges option for the property type.
  4. Publish the customization project.

Step 4: Testing the Form and the Attribute

In this step, you’ll test the Assign Work Orders (RS501000) form and the custom accumulator attribute. Do the following:
  1. On the Repair Work Orders (RS301000) form, create three repair work orders with the settings specified in the following table. Save each of them and click Remove Hold on the form toolbar.
    Work Order 1 Work Order 2 Work Order 3
    Customer ID C000000001 C000000002 C000000001
    Service Battery Replacement Screen Repair Battery Replacement
    Device Nokia 3310 Samsung Galaxy S4 Motorola RAZR V3
    Assignee Andrews, Michael Empty Beauvoir, Layla
    Description Test order Test order Test order

    Notice that the created work orders have the Ready for Assignment status.

  2. On the Assign Work Orders (RS501000) form, make sure that you see the three repair work orders that you have created. Also, be sure that the Assignee, Default Assignee, and Assign To columns are filled in for these orders.

    For the first work order, the Assign To setting is Andrews, Michael, which is also specified in the Assignee column. (You specified this employee on the Repair Work Orders form.)

    For the second work order, the Assign To setting is Baker, Maxwell—which is also specified in the Default Assignee column. The database currently doesn’t have information about the number of repair work orders assigned to the employee. Therefore, it has selected the employee with the first UserID (the key field) in the database.

    For the third work order, the Assign To setting is Beauvoir, Layla—which is also specified in the Assignee column.
    Figure 1. The assignees on the Assign Work Orders form


  3. For the third work order, change the employee in the Assign To column to Becher, Joseph.
  4. On the form toolbar, click Assign All. The work orders should be processed successfully.
  5. In the Processing dialog box, make sure that the processed repair work orders have the assignees specified as follows:
    • First work order: Andrews, Michael
    • Second work order: Baker, Maxwell
    • Third work order: Becher, Joseph
  6. Review the records in the RSSVEmployeeWorkOrderQty table by using Microsoft SQL Server Management Studio. The table contains three records (one for each employee to which repair work orders have been assigned during this testing). The value in the NbrOfAssignedOrders column is 1 for each row.
  7. On the Repair Work Orders (RS301000) form, open one of the processed work orders. Click Complete on the form toolbar.
  8. In SQL Server Management Studio, review the records in the RSSVEmployeeWorkOrderQty table. Now for the row representing the completed order, the value of NbrOfAssignedOrders is 0.

Step 5: Removing Unnecessary Columns from the Form (Self-Guided Exercise)

You should now remove the Assignee and Default Assignee columns from the table on the Assign Work Orders (RS501000) form on your own.

You can remove the columns by editing the TypeScript code of the form on the Modern UI Files page of the Customization Project Editor or by editing the code in Visual Studio Code or another code editor.