Action Definition: To Define an Action for a Table

The following activity will walk you through the process of implementing an action for a table, which is represented by a button on the table toolbar.

Story

Suppose that you need to give the users the ability to update the following prices on the Repair Work Orders (RS301000) form:

  • The base prices of repair items on the Repair Items tab
  • The default prices of labor items on the Labor tab

You need to define an action for each tab along with the associated button on the table toolbar of the tab. These buttons will be available only if no invoice has been created for the repair work order selected on the form.

Process Overview

In this activity, you will implement actions with buttons on the table toolbar of the Repair Items and Labor tabs of the Repair Work Orders (RS301000) form by performing the following steps:
  1. Creating an action and the associated Update Prices button on the table toolbar of the Repair Items tab
  2. Specifying the location of the Update Prices button on the Repair Items tab
  3. Creating an action and the associated Update Prices button, and specifying the location of this button on the table toolbar of the Labor tab
  4. Testing the Update Prices buttons and the associated actions

System Preparation

Make sure that you have configured your instance, as described in Test Instance for Customization: To Deploy an Instance for Creating Actions, and have completed the steps described in the Action Definition: To Define an Action for a Form prerequisite activity.

Step 1: Implementing the UpdateItemPrices Action

In this step, you will implement the UpdateItemPrices action in the RSSVWorkOrderEntry graph.

You will specify the availability of the button related to the action in the RowSelected event handler of the same graph by using the SetEnabled() method of PXAction<>. You will hide the button from the form toolbar and the corresponding command from the More menu by setting the DisplayOnMainToolbar property of the PXButton attribute to false.

To implement the UpdateItemPrices action, do the following:

  1. In the Actions region of the RSSVWorkOrderEntry graph, add the UpdateItemPrices action, as the following code shows.
            public PXAction<RSSVWorkOrder> UpdateItemPrices;
            [PXButton(DisplayOnMainToolbar = false)]
            [PXUIField(DisplayName = "Update Prices", Enabled = true)]
            protected virtual void updateItemPrices()
            {
                var order = WorkOrders.Current;
                if (order.ServiceID == null || order.DeviceID == null) return;
                var repairItems = RepairItems.Select();
                foreach (RSSVWorkOrderItem item in repairItems)
                {
                    RSSVRepairItem origItem = SelectFrom<RSSVRepairItem>.
                            Where<RSSVRepairItem.inventoryID.IsEqual<@P.AsInt>>.View.
                            Select(this, item.InventoryID);
                    if (origItem != null)
                    {
                        item.BasePrice = origItem.BasePrice;
                        RepairItems.Update(item);
                    }
                }
                Actions.PressSave();
            }

    In the action method, you have selected a repair item specified on the Services and Prices (RS203000) form and assigned its price to a repair item specified on the Repair Work Orders (RS301000) form. (In the T220 Data Entry and Setup Forms training course, a similar scenario is implemented in the RowUpdated event handler of the RSSVWorkOrderEntry graph.)

    In the action method, you do not need to update the RSSVWorkOrder.OrderTotal field, which contains the total price of the work order. The value is updated automatically because the PXFormula attribute is specified for the RSSVWorkOrderItem.BasePrice field.

  2. In the RowSelected event handler, specify that the button associated with the action should be available only when no invoice has been created for the order, as the following code shows.
                UpdateItemPrices.SetEnabled(WorkOrders.Current.InvoiceNbr == null);
  3. Rebuild the project.

Step 2: Specifying the Location of the Update Prices Button

In this step, you will specify the location of the Update Prices button on the table toolbar of the Repair Items tab. To configure the location of a button on the form, you should define the PXToolBarButton element in the desired location of the form's ASPX file.

Thus, to place the Update Prices button on the table toolbar of the Repair Items tab, you should modify the ASPX page of the Repair Work Orders (RS301000) form. Do the following:

  1. Open the RS301000.aspx file.
    Tip:
    The file is located in the Pages/RS folder of the instance.
  2. Add the following code in the <px:PXTabItem Text="Repair Items"> tag after the Levels closing tag.
                <ActionBar>
                    <CustomItems>
                        <px:PXToolBarButton Text="UpdateItemPrices">
                             <AutoCallBack Command="UpdateItemPrices" Target="ds" />
                        </px:PXToolBarButton>
                    </CustomItems>
                </ActionBar>

    In this code, you have defined an action bar on the table toolbar of the Repair Items tab and added a button to it.

  3. Save your changes.
  4. In the Customization Project Editor, update the RS301000.aspx file, and publish the customization project.

Step 3: Adding the Update Prices Button on the Labor Tab

In this step, you will implement the UpdateLaborPrices action in the RSSVWorkOrderEntry graph. You will configure the availability of the button associated with the action on the form in the RowSelected event handler of the same graph, hide the button from the form toolbar (and the corresponding command from the More menu), and define the location of the button on the Labor tab of the Repair Work Orders (RS301000) form.

In the action method, you should select a labor item specified on the Services and Prices (RS203000) form and assign its price to a labor item specified on the Repair Work Orders (RS301000) form.

In the RowSelected event handler, make the UpdateLaborPrices action enabled when no invoice has been created for the order.

To perform this step, use the information you have learned in Steps 1 and 2 of this activity.

Tip:
You can find the code changes made in this step in the Customization\T230\CodeSnippets\Step2.3 folder, which you have downloaded from MYOB GitHub.

Step 4: Testing the Update Prices Buttons and Associated Actions

In this step, you will test the Update Prices buttons that you added to the Repair Items and Labor tabs of the Repair Work Orders (RS301000) form. To test the buttons and the underlying actions, do the following:

  1. Modify the original repair and labor items by doing the following:
    1. Open the Service and Prices (RS203000) form, and specify the following settings:
      • Service: Battery Replacement
      • Device: Nokia 3310
    2. On the Repair Items tab, in the row with the BAT3310 inventory ID, enter 25 in the Price column.
    3. On the Labor tab, in the row with the CONSULT inventory ID, enter 10 in the Default Price column.
    4. Save your changes.
  2. Update the prices by doing the following:
    1. Open the Repair Work Orders (RS301000) form.
    2. Select the repair work order (for example, 000003) that you created in Step 3 of the Action Definition: To Define an Action for a Form activity. On the Repair Items tab, make sure that the Update Prices button is displayed, as shown in the following screenshot.
      Figure 1. The Update Prices button on the Repair Items tab


    3. On the Repair Items tab, notice the old price of the repair item with the BAT3310 inventory ID.
    4. On the table toolbar, click Update Prices.

      Notice that the item price has been updated and the Order Total value in the Summary area of the form has been updated.

    5. On the Labor tab, notice the old price of the labor item with the CONSULT inventory ID.
    6. On the table toolbar, click Update Prices.

      Notice that the item price has been updated and the Order Total value in the Summary area of the form has been updated.