REPORT ZVVMUR12 .
TABLES: KNVH.
TYPES: BEGIN OF WORKTYPE,
         LEVEL(2),
         HKUNNR LIKE KNVH-KUNNR,
         KUNNR  LIKE KNVH-HKUNNR,
       END OF WORKTYPE.
DATA: IT_KNVH TYPE TABLE OF WORKTYPE,
      WA_KNVH LIKE LINE OF IT_KNVH,
      IT_TEMP TYPE TABLE OF WORKTYPE,
      WA_TEMP LIKE LINE OF IT_TEMP,
      IT_WORK TYPE TABLE OF WORKTYPE,
      WA_WORK LIKE LINE OF IT_WORK.
DATA : BEGIN OF IT_NODES OCCURS 0.
        INCLUDE STRUCTURE SNODETEXT.
DATA : END OF IT_NODES.
CONSTANTS: NUMBER_OF_LEVELS TYPE I VALUE 6.
PARAMETER: P_HKUNNR LIKE KNVH-HKUNNR.
START-OF-SELECTION.
* Parent = 1. hierarchy node
WA_TEMP-KUNNR = P_HKUNNR.
APPEND WA_TEMP TO IT_TEMP.
WA_WORK-KUNNR = WA_TEMP-KUNNR.
WA_WORK-LEVEL = 1.
APPEND WA_WORK TO IT_WORK.
* Reading customer hierarchy (max. 6 level)
DO NUMBER_OF_LEVELS TIMES.
  CHECK NOT IT_TEMP IS INITIAL.
  SELECT KUNNR HKUNNR
    FROM KNVH
    INTO CORRESPONDING FIELDS OF TABLE IT_KNVH
    FOR ALL ENTRIES IN IT_TEMP
    WHERE HKUNNR = IT_TEMP-KUNNR.
  LOOP AT IT_KNVH INTO WA_KNVH.
    WA_KNVH-LEVEL = SY-INDEX + 1.
    APPEND WA_KNVH TO IT_WORK.
  ENDLOOP.
  IT_TEMP[] = IT_KNVH[].
ENDDO.
* Hierarchy nodes -> tree control
LOOP AT IT_WORK INTO WA_WORK WHERE LEVEL = 1.
  PERFORM MAKE_NODE.
  LOOP AT IT_WORK INTO WA_WORK WHERE LEVEL = 2 AND
                                     HKUNNR = WA_WORK-KUNNR.
    PERFORM MAKE_NODE.
    LOOP AT IT_WORK INTO WA_WORK WHERE LEVEL = 3 AND
                                       HKUNNR = WA_WORK-KUNNR.
      PERFORM MAKE_NODE.
      LOOP AT IT_WORK INTO WA_WORK WHERE LEVEL = 4 AND
                                         HKUNNR = WA_WORK-KUNNR.
        PERFORM MAKE_NODE.
        LOOP AT IT_WORK INTO WA_WORK WHERE LEVEL = 5 AND
                                           HKUNNR = WA_WORK-KUNNR.
          PERFORM MAKE_NODE.
          LOOP AT IT_WORK INTO WA_WORK WHERE LEVEL = 6 AND
                                          HKUNNR = WA_WORK-KUNNR.
            PERFORM MAKE_NODE.
          ENDLOOP.
        ENDLOOP.
      ENDLOOP.
    ENDLOOP.
  ENDLOOP.
ENDLOOP.
* Making the tree control
CALL FUNCTION 'RS_TREE_CONSTRUCT'
       TABLES
            NODETAB      = IT_NODES
       EXCEPTIONS
            TREE_FAILURE = 1.
* Display the tree control
  DATA : F15 TYPE C.
  CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
       EXPORTING
            CALLBACK_PROGRAM      = SY-REPID
       IMPORTING
            F15                   = F15 .
 
FORM MAKE_NODE.
  IT_NODES-NAME = WA_WORK-KUNNR.
  IT_NODES-COLOR = 1.
  IT_NODES-INTENSIV = 1.
  IT_NODES-TEXT = WA_WORK-KUNNR.
  IT_NODES-TLENGTH = 16.
  IT_NODES-TLEVEL = WA_WORK-LEVEL.
  IT_NODES-TCOLOR = 1.
  IT_NODES-TINTENSIV = 1.
  APPEND IT_NODES.
ENDFORM.


Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP MM SD Consultant
Contact : serkurumsal@yandex.com