Entering a new room can cause temporary memory lapses for your original intention. This doorway effect is a normal brain process for organizing experiences into distinct events. It happens as your brain updates its mental model of the environment.
Working memory limitations and attention shifts also contribute to these forgetful moments. Retracing your steps often helps retrieve the forgotten thought by restoring context.
Summary from source