Skip to Content

With this code we will build an Itab that contains organizational chart:

TYPES: tt_hrp1001 TYPE STANDARD TABLE OF hrp1001 INITIAL SIZE 0.

DATA: it_hrp1001 TYPE tt_hrp1001.

TYPES: BEGIN OF t_orgdisplay,

  level(40) TYPE c,

  id        type i,

  parent    type i,

  objid TYPE hrp1001-objid,

  orgtx   TYPE string,

  begda TYPE hrp1001-begda,

  endda TYPE hrp1001-endda,

  display type string,

END OF t_orgdisplay.

DATA: it_orgdisplay TYPE STANDARD TABLE OF t_orgdisplay,

      wa_orgdisplay TYPE t_orgdisplay.

DATA: wa_hrp1001 LIKE LINE OF it_hrp1001,

      wa_objid TYPE hrp1001-objid ,

      wa_objid1 TYPE hrp1001-objid,

      wa_objid2 TYPE hrp1001-objid,

      wa_objid3 TYPE hrp1001-objid,

      wa_objid4 TYPE hrp1001-objid,

      wa_objid5 TYPE hrp1001-objid,

      wa_objid6 TYPE hrp1001-objid,

      gd_branchid type i,

      gd_parent1 type i,

      gd_parent2 type i,

      gd_parent3 type i,

      gd_parent4 type i,

      gd_parent5 type i,

      gd_parent6 type i,

      gd_parent7 type i.

SELECT *

  FROM hrp1001

* up to 500 rows

  INTO TABLE it_hrp1001

WHERE otype = ‘O’  AND

       plvar = ’01’ AND

       rsign = ‘B’  AND

       ( relat = ‘002’ OR

         relat = ‘003’ ) AND

       begda LE sy-datum AND

       endda GE sy-datum.

clear: gd_branchid.

*level 1

gd_parent1 = 0.

LOOP AT it_hrp1001 INTO wa_hrp1001 WHERE objid = ‘50006550’.

  wa_objid = wa_hrp1001-varyf+2(8).

  wa_objid = ‘50006550’.

  wa_orgdisplay-level(2) = wa_orgdisplay-level(2) + 1.

  perform append_org_data using gd_branchid gd_parent1.

  gd_parent2 = gd_branchid.

*  WRITE:/ ‘level1’.

* level 2

  LOOP AT it_hrp1001 INTO wa_hrp1001 WHERE objid    = wa_objid

                                       AND varyf(1) = ‘O’.

    wa_objid1 = wa_hrp1001-varyf+2(8).

    wa_orgdisplay-level+2(2) = wa_orgdisplay-level+2(2) + 1.

    perform append_org_data using gd_branchid gd_parent2.

    gd_parent3 = gd_branchid.

*    WRITE:/ ‘level2’.

*   level 3

    LOOP AT it_hrp1001 INTO wa_hrp1001 WHERE objid    = wa_objid1

                                         AND varyf(1) = ‘O’.

      wa_objid2 = wa_hrp1001-varyf+2(8).

      wa_orgdisplay-level+4(2) = wa_orgdisplay-level+4(2) + 1.

      perform append_org_data using gd_branchid gd_parent3.

      gd_parent4 = gd_branchid.

*      WRITE:/ ‘level3:’, wa_objid2 .

*     level4

      LOOP AT it_hrp1001 INTO wa_hrp1001 WHERE objid    = wa_objid2

                                           AND varyf(1) = ‘O’.

        wa_objid3 = wa_hrp1001-varyf+2(8).

        wa_orgdisplay-level+6(2) = wa_orgdisplay-level+6(2) + 1.

        perform append_org_data using gd_branchid gd_parent4.

        gd_parent5 = gd_branchid.

*        WRITE:/ ‘level4:’, wa_objid3.

*       level5

        LOOP AT it_hrp1001 INTO wa_hrp1001 WHERE objid    = wa_objid3

                                             AND varyf(1) = ‘O’.

          wa_objid4 = wa_hrp1001-varyf+2(8).

          wa_orgdisplay-level+8(2) = wa_orgdisplay-level+8(2) + 1.

          perform append_org_data using gd_branchid gd_parent5.

          gd_parent6 = gd_branchid.

*          WRITE:/ ‘level5:’, wa_objid4.

*       level6

        LOOP AT it_hrp1001 INTO wa_hrp1001 WHERE objid    = wa_objid4

                                             AND varyf(1) = ‘O’.

          wa_objid5 = wa_hrp1001-varyf+2(8).

          wa_orgdisplay-level+10(2) = wa_orgdisplay-level+10(2) + 1.

          perform append_org_data using gd_branchid gd_parent6.

          gd_parent7 = gd_branchid.

*          WRITE:/ ‘level6:’, wa_objid5.

*       level7

        LOOP AT it_hrp1001 INTO wa_hrp1001 WHERE objid    = wa_objid5

                                             AND varyf(1) = ‘O’.

          wa_objid6 = wa_hrp1001-varyf+2(8).

          wa_orgdisplay-level+12(2) = wa_orgdisplay-level+12(2) + 1.

          perform append_org_data using gd_branchid gd_parent7.

*          WRITE:/ ‘level6:’, wa_objid6.

        ENDLOOP.

          CLEAR: wa_orgdisplay-level+12(2).

        ENDLOOP.

         CLEAR: wa_orgdisplay-level+10(2).

        ENDLOOP.

        CLEAR: wa_orgdisplay-level+8(2).

      ENDLOOP.

      CLEAR: wa_orgdisplay-level+6(2).

    ENDLOOP.

    CLEAR: wa_orgdisplay-level+4(2).

  ENDLOOP.

  CLEAR: wa_orgdisplay-level+2(2).

ENDLOOP.

CLEAR: wa_orgdisplay-level(2).

export it_orgdisplay to memory id ‘ZORGSTRUCT’.

DATA A LIKE TABLE OF it_orgdisplay .

IMPORT A FROM MEMORY ID ‘ZORGSTRUCT’.

BREAK-POINT.

*&———————————————————————*

*&      Form  append_org_data

*&———————————————————————*

*       text

*———————————————————————-*

*  –>  p1        text

*  <–  p2        text

*———————————————————————-*

form append_org_data using p_id

                           p_parent.

  gd_branchid = gd_branchid + 1.

  wa_orgdisplay-objid = wa_hrp1001-varyf+2(8).

  wa_orgdisplay-begda = wa_hrp1001-begda.

  wa_orgdisplay-endda = wa_hrp1001-endda.

  wa_orgdisplay-id     = p_id.

  wa_orgdisplay-parent = p_parent.

  if wa_orgdisplay-level+2  is initial.

    wa_orgdisplay-display = ‘TOP’.

  endif.

* Retrieve Org. Unit text

*  50006550

  PERFORM get_org_unit USING wa_orgdisplay-objid

                             wa_hrp1001-begda

                             wa_hrp1001-endda

                       CHANGING wa_orgdisplay-orgtx.

  translate wa_orgdisplay-orgtx using ”’ ‘.

  if wa_orgdisplay-orgtx is initial.

    wa_orgdisplay-orgtx = ‘Text unavailable’.

  endif.

  APPEND wa_orgdisplay TO it_orgdisplay.

  clear: wa_orgdisplay-display.

endform.                    ” append_org_data

*&———————————————————————*

*&      Form  get_org_unit

*&———————————————————————*

* Retrieve Org. Unit text

*———————————————————————-*

FORM get_org_unit USING    p_orgeh

                           p_begda

                           p_endda

                  CHANGING p_orgtx.

  DATA: orgeh_short(12) TYPE c,

        orgeh_stext(40) TYPE c,

        read_return TYPE i.

* Returns full orgunit text, entry in table t527x is sometimes

* truncated so that it fits into a 25 character field

  CALL FUNCTION ‘HR_READ_FOREIGN_OBJECT_TEXT’

       EXPORTING

          otype                   = ‘O’

*         otype                   = ot_orgunit

            objid                   = p_orgeh   “p0001-orgeh

            begda                   = p_begda   “p0001-begda

            endda                   = p_endda   “p0001-endda

            reference_date          = p_begda   “p0001-begda

       IMPORTING

            short_text              = orgeh_short

            object_text             = orgeh_stext

            return                  = read_return

       EXCEPTIONS

            nothing_found           = 1

            wrong_objecttype        = 2

            missing_costcenter_data = 3

            missing_object_id       = 4

            OTHERS                  = 5.

  IF sy-subrc EQ 0.

    p_orgtx = orgeh_stext.

  ELSE.

*     If function module fails return text from text table

    SELECT SINGLE orgtx

      FROM t527x

      INTO p_orgtx

     WHERE orgeh EQ p_orgeh AND  “Replace with org. unit field

           sprsl EQ sy-langu AND

           ( endda GE sy-datum AND

             begda LE sy-datum ).

  ENDIF.

ENDFORM.                    ” get_org_unit

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply