Copyright 2020 - BV TallVision IT

When you are using an organisational plan (transaction PPOME), the hierarchy is locked into the relations table HRP1001. This is not a very simple but straightforward setup where the relations in both directions are described. Here's how to access this information from your Abap.

First of all, there is a function module you may want to consider: who's the boss ? Check out RH_GET_LEADING_POSITION or RH_GET_LEADER. I stumbled across a limitation with the module: if the org plan is not completely filled in, the actual boss of an employee needs to be looked up on a higher level. So I had a closer look at table HRP1001 - Organization plan relations and I found the recursive setup could quite easily be understood and coded. Just to demonstrate how the organisation plan hangs together, this could be a nice example. Make sure you read this article too, before composing your own selections as I did below. I would not code this again, now that I learned about evaluation paths - nevertheless the coding below nicely demonstrates how it works. The example:

FORM who_is_my_boss USING p_date p_pernr CHANGING p_the_boss.
  TYPES: BEGIN OF ty_object,
           otype TYPE hrp1001-otype,
           objid TYPE hrp1001-objid,
         END OF ty_object.
  DATA: lt_hrp1001 TYPE STANDARD TABLE OF hrp1001,
        lw_hrp1001 TYPE hrp1001,
        lv_varyf TYPE hrp1001-varyf,
        lv_fetch_request(4),
        lw_persoon TYPE ty_object,
        lw_formatieplaats TYPE ty_object,
        lw_organisatie TYPE ty_object,
        lw_formatieplaats_hoofd TYPE ty_object,
        lw_boss_figure TYPE ty_object.

  DEFINE fetch_1001.
    lv_fetch_request = &2.
    if &1 is initial. "Nothing in nothing out
      clear &3.
    else.
      select varyf from hrp1001 into lv_varyf
        up to 1 rows
        where otype = &1-otype and
              objid = &1-objid and
              rsign = lv_fetch_request(1) and
              relat = lv_fetch_request+1(3) and
              istat = '1' and
              begda <= p_date and endda >= p_date.
      endselect.
      if sy-subrc <> 0.
        clear &3.
      else.
        &3-otype = lv_varyf(2).
        move lv_varyf+2(8) to &3-objid.
      endif.
    endif.
  END-OF-DEFINITION.

* The employee for which we want to find the boss:
  lw_persoon-otype = 'P '.
  lw_persoon-objid = p_pernr.

* The employee is in a position
  fetch_1001 lw_persoon 'B008' lw_formatieplaats.
* The position is part of an organization
  fetch_1001 lw_formatieplaats 'A003' lw_organisatie.
* The organisation also has a position for the leader
  fetch_1001 lw_organisatie 'B012' lw_formatieplaats_hoofd.
* Which employee is assigned to the "boss" position ?
  fetch_1001 lw_formatieplaats_hoofd 'A008' lw_boss_figure.
* If we found the boss - no need to take this further
  IF NOT lw_boss_figure IS INITIAL AND
* The person could him/herself also be a boss - in which case, continue
    not ( lw_boss_figure-otype = lw_persoon-otype and
          lw_boss_figure-objid = lw_persoon-objid ).
    p_the_boss = lw_boss_figure-objid.
  ELSE.

    DO 10 TIMES. "Maximize nr of levels
* Above the organisation, there is another organisation
      fetch_1001 lw_organisatie 'A002' lw_organisatie.
* The organisation has a position for the leader
      fetch_1001 lw_organisatie 'B012' lw_formatieplaats_hoofd.
* Which employee is assigned to the "boss" position ?
      fetch_1001 lw_formatieplaats_hoofd 'A008' lw_boss_figure.
* If we found the boss - no need to take this further
      IF NOT lw_boss_figure IS INITIAL.
        p_the_boss = lw_boss_figure-objid.
        EXIT.
      ENDIF.
    ENDDO.
  ENDIF.

ENDFORM.                    "who_is_my_boss

What the example demonstrates is a common setup for leader-assignment can be extracted from the relations table. If you need some other sort of relation, be aware that the organization structure is available in a very basic form. It can be understood and selected on with very basic steps.

One improvement on the above logic would be to support the simple fact that users can be assigned to multiple organisation units, e.g. for 60% to some department and 40% to another department. The actual percentage can be checked on the HRP1001 selection from P to S (with B008) (field PROZT) or (much more reliable) the main department can be picked up from infotype 0001 (field PLANS).