Flow (for creating slug) not working - "You don't have permission to access this"

I have a Flow works fine when it comes to creating or updating a slug. It is what was shown here on Directus a year ago (or maybe even longer, when Flows became popular).

But that flow only receives the payload from the very field that was changed. Nothing else. So it needed to be a change in the “title” field in order to make the slug action work.

I, however, need to combine field in order to achieve the requirement of having a unique slug. Since the collection is one, where the titles will very often be just something like “Concert”, that uniquness is not given. I therefore want to use the date_created and the id field like this:

slug: {{ title }}-{{ date_created }}-{{ id }}

In order to do so I added a “Read Data” operator (? is that how those items are called?) to the flow, right after the “Trigger” and before the “Run Script” operators. I keep using {{ $trigger.collection }} and {{ $trigger.keys }} to get the proper item from the collection. I was hoping that that allows to access the whole collection in the “Run Script” operator, where I have this script:

module.exports = async function (data) {
  // Get the complete item from the “Read Data” operation
  const item = data.$trigger.operation[‘read-data’].result[0]; // [0] because it’s an array

  // Extract fields you need
  const title = item.title || ‘’;
  const dateCreated = item.date_created ? new Date(item.date_created).toISOString().split(‘T’)[0] : ‘’;

  // Generate slug using both fields
  const text = `${title}-${dateCreated}-{$item.id};`

  const slug = text
    .normalize(‘NFD’)
    .replace(/[\u0300-\u036f]/g, ‘’)
    .toLowerCase()
    .trim()
    .replace(/[^\w\s-]/g, ‘’)
    .replace(/[_-]+/g, ‘-’)
    .replace(/^-+|-+$/g, ‘’);

  return slug;
};

The final “Update Data” operator, again using {{ $trigger.collection }} and {{ $trigger.keys }}, uses this json in its Query field:

{ "slug": "{{ slugify_data }}" }

However, renaming the title of an event only ever gives me an error, and if I am not mistaken, that already happens in the Read Data operator:


Read all data from Events collection Read Data

Options


Payload


{ “name”: “DirectusError”, “message”: “You don’t have permission to access this.” }

Here are some screenshots about my flow. Can anybody help me out figuring out what I have to do to make this work, please?

Bit hard to answer exactly without kicking the tires directly, but here’s some additional context that might help:

Read and update data operations don’t run as system full-admin by default: both operations default to $trigger, meaning that they execute with the same accountability as the user who edited the item.

On an items.update event, the trigger data only includes the changed payload + keys and collection, it doesn’t automatically hydrate the full item, so adding the item-read step is the right way to fetch the full title, date_created, and id.

The permission error means that the triggering user/role cannot read that item or the fields you’re trying to load. You can fix that by granting the role of the user that triggers the flow the read access to those collections/fields, or by setting the permissions option on read/update data to full-admin access.

In the script step, you can read the previous operation result from it’s operation key, so if the step operation key is read_data you can read it from data.read_data?.[0]

Last but not least; the Update Data defaults emitEvents to false, so it won’t re-trigger the same flow unless you explicitly turned that on