Trigger.after update fails

Hi, i was trying to use a trigger.after when creating a new user, but it is not working as it has to, when I create a new user my trigger is showing me this:
“Records for current filter not found”

but the user is created, because if I put “console.log(event.data);” in my catch it shows me the data of the new user and in my DB appears as a created user

Hello!

Sorry about the long wait. We’ll give you a response soon.

Lada Kokotova | Technical Support Engineer

Сould you attach the actual code snippets of the trigger and related stuff to make the debug easier? Also, can you send workspace ID, environment Name, and environment ID, please?

Lada Kokotova | Technical Support Engineer

Is there any update…?

Our developer is checking your issue. We’ll let you know soon. Sorry about the long wait.
Lada Kokotova | Technical Support Engineer

1 Like

Hi!
I’m backend developer from 8base
How do you call the mutation? there are two ways, call user creation directly or inside another mutation.
Here is an example

mutation UserCreate {
   userCreate(...) {
   }
}

mutation SomeTableCreate {
   someTableCreate(
     user: {
         create: { ... } 
     }
}

if you call with help of the first example, the trigger should work and we will research the issue in this case.
If you call with help of the second example, it’s okay that you can’t update the record, because the transaction for this operation is not committed yet.

The first one, here’s an example:

hm, it’s an update mutation. but not create. Provide, please, mutation of creating action,

sorry, fkUsersToUSersNotificationsTypes is relation to user table, right?

yes, it has a relation

ok, that’s the second example, so, it’s well-known behavior that triggers for user create mutation can’t fetch your current user. You need to create users as root mutation if you want to fetch them in “after” trigger.
Here is an example of how you can rebuild your query

mutation userCreate {
   userCreate(
     data: {
        isEnable:true
        notificationType: { connect: ...  } 
        <reference to user table>: { connect: ... } 
     } 
   ) {
      email
   }
}

let me know if it helps you.

But, I need to notice you, that it’s forbidden to call create record in the after trigger of the same table!

There is something that I can’t understand, why even if the trigger executes after the user is created and the system is given me the data of that user, like id, name,etc…
I want to update that register, why I can’t update it?, record is already created in my DB and I need to add some relational registers to it.

Is the problem caused because I want to insert records in my relational tables?

When you run userUpdate mutation the server starts transaction in the database. Any triggers that are called for records that created as relations can’t fetch the created record via gqlRequest, because it runs another request in another transaction for the database. As the first transaction hasn’t been committed at the moment of trigger calls, there isn’t a new record in the database.
You see data in the event, because the creation was completed for the transaction, but it wasn’t committed.

oh i see, basically I can’t use update with a trigger when the update has relational tables in it

yes, you are right. but it’s only for a case if you need to fetch the id that was created in the current request.
But, as I mentioned, it’s bad practice if you call some action in the trigger for the current record. it could bring to loop calls.

1 Like

Ok, thanks for the information!

but the after trigger for the root mutation can fetch the current record, because transaction is committed at this moment

What do you mean by “for the root mutation can fetch the current record”?

I mean, if you use after trigger for some table, this trigger can fetch created record data from the database.
here is an example

mutation {
   someTableCreate( ... ) { ... }
}

trigger code:

async (event, context) =>  {
  const result = await context.gqlRequest("get query", { id: event.data.id }) 
}

The result would contain created record.

Hello Jetix!

Can you write this in a more detailed step-by-step manner cause it seems a bit tricky now?

Lada Kokotova | Technical Support Engineer