Skip to main content

Lesson 10 · Queries: reusing the data you already have

M
Written by Mairu

Path

Duration

Level

Before you start

🟣 Configurator · Level 2

75 min

Advanced

[[09 · Eventos - que el proceso avance solo

Events - letting the process move on its own]]

Queries are the most powerful tool in Mairu and also the one that is hardest at first. They serve one very simple idea: not asking again for a piece of data that is already recorded somewhere. With them you will fill in fields on their own, calculate indicators and show lists inside a form or a dashboard.

🎯 By the end you'll know how to

  • Choose the right query type depending on where you are going to use it.

  • Read and write the structure of a query: select, from, where, aggregate, order.

  • Create parameters so the query depends on what the user chooses.

  • Test a query before using it.

  • Set up a Masterdata form to store data that the master records don't support.


🧠 The idea: a question to your forms

A query is a question of the type:

Give me this piece of data (select) from form X (from) of the responses that meet these conditions (where).

{
  "select": "ID of the field you want",
  "from": "ID of the form",
  "where": [ ...conditions... ],
  "aggregate": "average",
  "order": { ... }
}

Notice that everything is written with IDs. That is why in lesson 5 we insisted so much on choosing them well.

⚠️ Queries read responses of forms, not the master records in Settings. An Asset-type query returns the Assets selected in a field of some form, not the full list of Assets. If you need additional data of a Product or a Supplier, store it in a Masterdata form (you will do this in this lesson).

The type depends on where you'll use it

Type

Returns

Used in

Single value

A single value (the first one it finds)

Short text, long text and number fields

Indicator

A calculated number (count, sum, average, min, max, stdDev…)

Number fields and KPI widget

List

Several responses with the fields you choose

Response selector and list, List widget, Load subform, Schedules

Asset / Product / User / Entity

Filtered values for a selector

The selector of the same type

File

Files from the responses

Media and Gallery components

When you configure a field, Mairu only offers you the queries compatible with that field type.

Fixed value or parameter

  • "value": "NOT OK" → the query always filters by that value.

  • "value": "producto" (name of a parameter) → the value comes from a form field each time it runs. The parameter type must match that of the field it filters on.


🛠️ Guided practice

First create a folder Packaging in Settings > Queries to store all the ones in this lesson there.

Query 1 · The average weight of the weighings (Indicator)

You will calculate the average weight of the five weighings of each control.

  1. New query Control average weight, type Indicator.

  2. Create a parameter of type Text called padre.

  3. Configure it like this:

{
  "select": "peso",
  "aggregate": "average",
  "from": "pesadas",
  "where": [
    {
      "allConditions": {
        "conditions": [
          { "field": "parent_answer_id", "operation": "=", "value": "padre" }
        ],
        "type": "and"
      },
      "type": "and"
    }
  ]
}

  1. In the Packaging control add a field Average weight (g), number, ID peso_medio, disabled. In its configuration choose this query and, as the value of the padre parameter, the Form ID option.

parent_answer_id is the identifier that each weighing stores for its control. This way the query only averages the weighings of this control.

Query 2 · Product data from a Masterdata (Single value)

The product has no shelf life or nominal weight attribute. Instead of writing them by hand, you will store them once in a Masterdata form.

  1. Create the form Product record (ID ficha_producto) with: Product (Product selector, producto), Nominal weight (g) (peso_nominal) and Shelf life (days) (vida_util). Publish it only for Quality and create one response for each product.

  2. Create the query Product shelf life, type Single value, with a parameter of type Product called producto:

{
  "select": "vida_util",
  "from": "ficha_producto",
  "where": [
    {
      "allConditions": {
        "conditions": [
          { "field": "producto", "operation": "=", "value": "producto" }
        ],
        "type": "and"
      },
      "type": "and"
    }
  ]
}

  1. In the Packaging control, open the vida_util field (lesson 7), remove its default value, assign it this query and link the producto parameter to the Product field of the control.

💡 A query configured on the field itself runs every time the response is opened. If you need it to run just when the user changes the product, launch it from a rule (When producto changes value → run query).

Query 3 · The latest rejections on the line (List)

  1. Query NOT OK by line, type List, with a parameter of type Asset called linea. Select created_at, lote, producto, motivo_rechazo and accion_correctora from control_envasado, with the conditions linea = linea and resultado = "NOT OK", ordered by created_at descending ("type": -1).

  2. In the Packaging control add a Response list with this query and its parameter linked to the linea field. This way the operator sees the latest problems on their line while doing the control.

Test before using

Before linking each query, open it and use Testing: choose test values for the parameters and press Run. The result appears in JSON.

You see…

It means

An empty result

The query works, but there is no data with those test values

Error – Status 500 or similar

There is a problem in the configuration: an ID that doesn't exist, a badly written structure…


🔍 Check that it works

  • Open a control with weighings of 248, 250, 252, 251 and 249 g: the average weight shows 250.

  • When you choose Preserve 500 g, the shelf life fills in on its own with the value from its record.

  • In a Line 1 control the latest NOT OK of Line 1 appear, and none from Line 2.


⚠️ Common mistakes

  • Putting the field title instead of its ID. It is the most frequent cause of errors. Copy the IDs from the Extra fields tab of the Form Builder.

  • The parameter type doesn't match the field type. Comparing an Asset with a Date-type parameter doesn't work.

  • "The query doesn't appear in the field." The query type is not compatible with that field: a user selector only accepts User-type queries, and response lists only accept List-type queries.

  • Trying to query the Products or Assets master directly. Queries read fields of forms: use a Masterdata form.

  • Confusing "empty" with "error". An empty result in Testing is not always a failure: try other values.


🏋️ Challenge

Quality wants to see the tolerance of the product in the control and, if the average weight falls outside nominal weight ± tolerance, for the result to turn NOT OK automatically. You will need to extend the Product record, a new single-value query and a rule. When it works, check: does the email from lesson 9 fire?


📚 Learn more

➡️ Next lesson: Schedules - planned work

Did this answer your question?