2

In directus I am creating records where one column is a float that contains negative numbers.

On creation of a record I would like to copy the absolute value of this column to another column in the same collection automatically.

Example: After creating a record with a column having this: -45.20 value, I would like to create an automatic flow that copies 45.20 part of the cell to another column in that same record just inserted.

Is this possible with flows in directus?

bcsta
  • 1,963
  • 3
  • 22
  • 61

2 Answers2

3

Yes, it is possible now. Below example on how to do this with non-blocking action event, but you should be able to do this with filter event and modify the variable before initial database insert. The flow described below will do the update after data is already in database.

Trigger: event hook

type: action scope: items create collection: (choose target collection)

Condition

{
    "$trigger": {
        "payload": {
            "field_name": {
                "_eq": "_null"
            }
        }
    }
}

(this means the flow will continue only if field/column "field_name" will be empty)

Run script

module.exports = async function(data) {
    data.$trigger.payload.field_name = Math.abs(data.$trigger.payload.source_field);
    return data;
}

Here we setting a new property with absolute value of "source_field"

Update data

collection: choose same collection, IDs: {{$trigger.key}}

You need to switch input field to RAW editor before entering moustache variable here!

Then in payload textarea:

{
    "field_name": "{{ $trigger.payload.field_name }}"
}

This time do not switch to RAW editor!

This is how it looks like in the flow editor: flow view

Artur Czyżewski
  • 705
  • 5
  • 12
  • works as expected! you can also Update data in the system collections like `directus_users` - just type it manually – Adam Bubela Oct 30 '22 at 22:47
  • I was able to get the null filter condition working like this: `"field_name": { "_null": "" }` – jakeorr Jan 18 '23 at 19:03
1

In general terms, it sounds like you are looking to read data the moment it is created, transform it, then write it back in to another Field/Collection. This is my understanding of how to do this:

For the first and last steps, you can use the Event Hook Trigger as well as the Read/Update Items Operations.

However, the second step requires you write a function to transform the value. At the time of writing this answer, there is no Operation to write/apply a function in the Flow. Therefore, in order to have function logic, you can create your own Custom Hook to add that logic or use a Webhook Operation to send the data off to another service, process/transform it as desired, then return it- either to another Flow via a Webhook Trigger, or just writing the new Item directly into the database.

  • Recent versions of Directus have a *Run Script* operation now, for custom js code. Unfortunately it's not documented yet. – piotr_cz Aug 22 '22 at 19:42