Update an item causes recursive loop

Hi team,

So I am looking to run a method on an item whenever it is updated to update two hidden properties on the item that drive some functionality later on down the workflow.

The issue I am having is that when I use the onAfterUpdate event, because the method updates the item it causes a recursive loop and runs itself indefinitely.

I attempted onAfterVersion however this caused two weird scenarios - the logging wouldn't show the correct result until the second time the item was saved and even then it never actually updated the properties. I have this same event running onAfterAdd and it works successfully there but I need it to update anytime the item is saved.

Finally, I got the idea to run it via a wrapper from a client event on the form. In this case, onFormPopulate causes the recursive loop again, and so I am attempting a field even for onChange of the field that drives these in question. It still seems to have an issue so any suggestions would be welcome.