How to use Directus flows to copy a value from one column to another on creation

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?

2 Answers

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

2

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.

1

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobmttZ26Adn%2BOoaawZaSkera%2FxGaboqqVmMG2v4yfo6ivo2LBsHnCqKeyZZFiw6K41J5kn6qfonqwusRmmqikpaK7bsDOZpinp6SdsrN5zqdknKqVlsGqu80%3D