The Recursive GlideRecord Avoidance Pattern transforms recursive database traversals into iterative loops using data structures like stacks or queues. This prevents JavaScript call stack overflow errors that occur when traversing deep hierarchies like organizational structures, category trees, or approval chains with many levels.
Recursive GlideRecord queries become problematic when hierarchies exceed the JavaScript engine's stack limit (typically around 1000 levels in ServiceNow). Each recursive function call consumes stack memory, and deep traversals can crash with 'Maximum call stack size exceeded' errors. This pattern replaces recursive function calls with a manual stack data structure, allowing unlimited traversal depth while maintaining the same logical flow.
The iterative approach processes nodes level-by-level using a stack to track pending work. Instead of calling functions recursively, nodes are pushed onto the stack and processed in a loop until the stack is empty. This transforms O(depth) stack memory usage into O(1) stack usage with O(width) heap memory for the manual stack.