Configuration of Actions

Actions are methods that can be invoked from outside of a graph, from the UI, or through the web service API. Actions are associated with buttons or commands (or both) on the user interface.

Action Declaration in a Graph

The declaration of an action in a graph consists of the following:

  • A field of the PXAction<> type, which is declared as follows.
    public PXAction<Shipment> CancelShipment;

    In the PXAction<> type parameter, you should specify the main DAC of the primary data view. Otherwise, the button corresponding to the action cannot be displayed on the form toolbar (and the corresponding command cannot be displayed on the More menu).

  • A method that implements the action; this method has the PXButton and PXUIField attributes. This method has the following forms of declaration:
    • Without parameters and returning void: This standard form of declaration is shown in the following code example.
      [PXButton]
      [PXUIField(DisplayName = "Cancel Shipment")]
      protected virtual void cancelShipment()
      {
          ...
      }

      This type of declaration is used for an action that is executed synchronously and is not called from a processing form.

    • With a parameter of the PXAdapter type and returning IEnumerable: You can see an example of this form of declaration in the following code.
      [PXButton]
      [PXUIField(DisplayName = "Release")]
      protected virtual IEnumerable release(PXAdapter adapter)
      {
          ...
          return adapter.Get();
      }

      This type of declaration should be used when the action initiates a background operation or is called from a processing form.

The field and the method should have the same name, differing only in the capitalization of the first letter.

Tip:
A graph includes the Actions collection of all PXAction<> objects defined in the graph.

Callback on the Action

When a user invokes an action through a button or command on the UI, the page sends a request to the server side of the application (that is, it executes the callback). By default, for a button or command, the callback is always executed—that is, the CommitChanges property of the PXButton attribute is true. If you do not need the form to send the recent changes made on the form, set the CommitChanges property of the PXButton attribute to false as follows.

[PXButton(CommitChanges = false)]

The CommitChanges property must be always set to true for the actions that cause changes to be saved to the database.

Types of Actions

You typically use actions for the following purposes:

  • To redirect a user to a specific form or report.
  • To modify or validate data records and save changes to the database.
  • To start a background operation, which is executed on a separate thread.

Changing of Appearance of Buttons and Commands on the UI at Runtime

You can adjust the appearance of a button or command (or both, if applicable) on the UI that is associated with an action at runtime (for example, to make the button or command unavailable or hidden). To do this, you should use the methods of the PXAction<> class, as the following code example shows.

// Disabling the CancelShipment action
CancelShipment.SetEnabled(false);

You do not use the static methods of the PXUIField attribute, because these methods work only with the attribute copies stored in PXCache objects.