Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

NESTED LOOP IN ABAP

Former Member
0 Kudos
199

Hi experts

Can we print BOM components in the following form .

Level1 Component1 Level2 comp1 Level3 comp1 Level4 comp1

Level1 Component1 Level2 comp1 Level3 comp2 Level4 comp1

Level1 Component2 Level2 comp2 Level3 comp1

Level1 Component3

Level1 Component4

and can anyone give me the logic for printing this form.

Thanks in advance.

Regards

Rajaram

1 REPLY 1

Former Member
0 Kudos
82

Try something like:

report zlocal_jc_bom_hierarchy
 line-size 132.

data:
  begin of gs_hier,  "simulate a BOM hierarchy for demo
    level               like stpox-stufe, "level
    element             like stpox-idnrk, "BOM item
  end of gs_hier,
  gt_hier               like gs_hier occurs 0.

start-of-selection.
  perform gt_hier_fill.  "pretend we read from BOM db
  perform hierarchy.   "print it

*&---------------------------------------------------------------------*
*&      Form  hierarchy
*&---------------------------------------------------------------------*
form hierarchy.

  data:
    l_prev_level        like gs_hier-level,
    ls_hier_set         like gs_hier,
    lt_hier_set         like ls_hier_set occurs 10,
    ls_hier             like gs_hier.

  format reset.

*" print out hierarchy in rows
  loop at gt_hier into ls_hier.

    if ls_hier-level = 1. "new top element, start fresh
      if not lt_hier_set[] is initial. "not first loop
        perform print_set
          tables
            lt_hier_set.
      endif.
      clear: lt_hier_set, lt_hier_set[]." start afresh
      l_prev_level = 1.
    endif.

    if ls_hier-level < l_prev_level. "have stepped back up
      perform print_set
        tables
          lt_hier_set.
*" remove existing rows at this level and below
      delete lt_hier_set where level ge ls_hier-level.
    endif.
    append ls_hier to lt_hier_set.

    l_prev_level = ls_hier-level."remember previous loop

  endloop.

*" print last set
  if not lt_hier_set[] is initial. "not first loop
    perform print_set
      tables
        lt_hier_set.
  endif.

endform.                    "hierarchy

*&---------------------------------------------------------------------*
*&      Form  print_set
*&---------------------------------------------------------------------*
form print_set
  tables
    it_hier_set         structure gs_hier.
*
*" take a hierarchy subset and print it, or whatever...
*
  data:
    l_char130(130)      type c,
    ls_hier_set         like gs_hier,
    lt_hier_set         like ls_hier_set occurs 10.

  lt_hier_set[] = it_hier_set[].

  loop at lt_hier_set into ls_hier_set.
    concatenate l_char130 ls_hier_set-element
      into l_char130 separated by space.
  endloop.

  format reset.
  format color col_normal.
  write: /
    l_char130, at sy-linsz space.
  format reset.

endform.                    "print_set

*&---------------------------------------------------------------------*
*&      Form  gt_hier_fill
*&---------------------------------------------------------------------*
form gt_hier_fill.

  data:
    ls_hier             like gs_hier.

  clear: gt_hier, gt_hier[].
*
*" Simulate structure of for demo:
*"   Level1 Component1 Level2 comp1 Level3 comp1 Level4 comp1
*"   Level1 Component1 Level2 comp1 Level3 comp2 Level4 comp1
*"   Level1 Component2 Level2 comp2 Level3 comp1
*"   Level1 Component3
*:   Level1 Component4
*
  ls_hier-level   = 1.
  ls_hier-element = 'Lvl 1 Comp 1'.
  append ls_hier to gt_hier.

  ls_hier-level   = 2.
  ls_hier-element = 'Lvl 2 Comp 1'.
  append ls_hier to gt_hier.

  ls_hier-level   = 3.
  ls_hier-element = 'Lvl 3 Comp 1'.
  append ls_hier to gt_hier.

  ls_hier-level   = 4.
  ls_hier-element = 'Lvl 4 Comp 1'.
  append ls_hier to gt_hier.

  ls_hier-level   = 3.
  ls_hier-element = 'Lvl 3 Comp 2'.
  append ls_hier to gt_hier.

  ls_hier-level   = 4.
  ls_hier-element = 'Lvl 4 Comp 2'.
  append ls_hier to gt_hier.

  ls_hier-level   = 1.
  ls_hier-element = 'Lvl 1 Comp 2'.
  append ls_hier to gt_hier.

  ls_hier-level   = 2.
  ls_hier-element = 'Lvl 2 Comp 2'.
  append ls_hier to gt_hier.

  ls_hier-level   = 3.
  ls_hier-element = 'Lvl 3 Comp 2'.
  append ls_hier to gt_hier.

  ls_hier-level   = 1.
  ls_hier-element = 'Lvl 1 Comp 3'.
  append ls_hier to gt_hier.

  ls_hier-level   = 1.
  ls_hier-element = 'Lvl 1 Comp 4'.
  append ls_hier to gt_hier.

endform.                    "gt_hier_fill