Change document field inside Update Trigger

Is it possible to update a field in an Update/Insert document trigger without it refiring the trigger multiple times due to the change? I suspect this just isn’t possible but thought I’d ask :slight_smile:

Thanks!
-Shane

Hi, it sounds like your trigger is reacting to itself. IE, the trigger is listening to a collection and modifying that same collection. That means that every time the trigger runs and updates the document it will cause another event to fire and update the document (and then another).

You will need to short-circuit this by not performing the write if the update being made is from this function code (you can examine the contents of the changeEvent / updateDescription for this)

Some pseudo-code would be:

if changeEvent.operationType === "update" && changeEvent.updateDescription.length === 1 && changeEvent.updateDescription["modifiedAt"] !== undefined  {
     return 
}

I saw your other post in which you are updating the “modifiedAt” field. So the idea is just that if we can identify that this is just a trigger running, then we can stop executing the function.

1 Like

Hi,
Yep, that method seems like it should work. I removed the other post because I figured out why it was retriggering but didn’t know how to prevent it.

Thanks for the help!
-Shane