Nesting of BOMs and various sub-branches to show on print format

Hi,

I would like to display the total nesting of BoMs along with its sub-branches on the print format. So, my Phase 1 will be the lowest level, where the components are bought, and the Phase 2 will include materials from Phase 1, and other purchased components, if necessary. My last level will include all the BoMs that are required to make the end-product. I have tried to write a code, but have been able to take into account only 7 to 8 nestings. Is there a way to include n number of nesting and loops, without having to bother about the actual number? My code is reproduced below: