Use actions.name.trigger ()

Sometimes, you will need to handle the result of another action or execute some actions in bulk using JavaScript. In these cases, you can use await actions.action.trigger() syntax, which returns Promise and allows you to reuse the result of other actions.

Let’s check a couple of cases, where action.trigger comes in handy.

Basic setup

In the basic setup, you can use await actions.actionName.trigger() anywhere in your app actions code, like this:

const user = await {{ actions.getCurrentUser.trigger() }};

It is also possible to pass custom parameters to the action:

const user = await {{ actions.getUser.trigger('email@example.com') }};

The parameters will be available as {{params}} variable in any action step.

Bulk actions

Getting data for a certain record

Let’s review the case when you need to combine the data from several tables about a certain record. For the example, we will use MySQL data source and two tables: Orders and Orders details. Follow the steps below:

  1. Add a new action - Load Table. Select Order Details table as a resource. Make sure the action is titled as loadOrderDetails.

  2. Configure selected record field as Order Number ={{data}}.

  3. Next up, add another action - Load Row. Select Orders table as a resource. Make sure the action is titled as loadOrder.

  4. Configure selected record field as Order Number ={{data}}.

  5. Now, to combine the data from both the actions, add a new action - Code. Rename it to loadDetails and specify the code:

const orderId = {{data}};

const [orderData, orderDetails] = await Promise.all([{{ actions.loadOrder.trigger(orderId) }}, {{ actions.loadOrderDetails.trigger(orderId) }}]);

return {
	...orderData,
  	details: orderDetails,
  	detailsNames: orderDetails.map(item => item.productCode),
  	detailsAmount: orderDetails ? orderDetails.length : 0,
};

To display the obtained data, find a Details component and drag it onto the canvas. Assign the loadDetails action to the Details component.

Please note that you'll need to unhide the details Amount and detail Names fields to make them visible, as well as change detail Names field's view type to Multiselect/Tag for a better representation.

Getting data for all records

In case you need to see the combined data for all the records in the Table, follow these steps:

  1. Add a new action loadOrderDetails - Load Table. Select Order Details table as a resource.

  2. Configure selected record field as Order Number ={{data}}

  3. Add a new Workflow: load Orders table as a first step. For testing purposes, we recommend setting the limit to 10 records.

  4. Add a Code step with the below code as a second step:

async function applyDetailsToOrder(order) {
    const orderDetails = await {{ actions.loadOrderDetails.trigger(order.orderNumber) }};
    return {
        ...order,
        details: orderDetails,
        detailsNames: orderDetails.map(item => item.productCode),
        detailsAmount: orderDetails ? orderDetails.length : 0,
    };
}

const result = {{ data }}.map(item => applyDetailsToOrder(item));
return Promise.all(result);

After that, add a Table and assign the applyDetailsToOrder action to it to retrieve the data.

Please note that you'll need to unhide the details Amount and detail Names fields to make them visible, as well as change detail Names field's view type to Multiselect/Tag for a better representation.

Getting certain items from the Table

Say you have a table with lots of records, but just need to display certain items. Let’s check how this can be done on an example of an HTTP API data source.

  1. Add a new action getUser - HTTP request. Select the data source, set GET method, specify the URL as {{data}}

  2. Add the next action getUsers - Code. Specify the below code:

const usersToGet = [1, 10, 2, 5, 9];
const result = [];
for (const id of usersToGet) {
    const user = await {{ actions.getUser.trigger(id) }};
    result.push(user);
}
return result;

As the last step, add a table to display the obtained data.

Last updated