Skip to Content
Author's profile photo Arup Goswami

Retrieve Hierarchy from node name via ABAP program

We have standard function module where the ABAP code retrieve the full BW hierarchy. Retrieving BW Hierarchy partially is always painful when we do not find the standard function module. This document will help you to create the ABAP program which will retrieve BW info object hierarchy fully or partially based on selected node.

Let me give you an example regarding 0GL_ACCOUNT info object. The hierarchy is maintained in 0GL_ACCOUNT. We will talk about the hierarchy name ‘XXXX’. (Please use Hierarchy Name – where you want to retrieve information)

Suppose we want to retrieve the hierarchy leaves information for node XXXX56.

>> We created a program in se38 t-code.

>> If we run the program, the below i/p screen will appear for user input. We have to provide Hierarchy name and Node Name.

>> The output will be below.

>> Please include below code in se38 screen.

PARAMETERS : p_node TYPE rsshnodenamestr DEFAULT ‘XXXX25’,
p_hinm       TYPE rshienm DEFAULT ‘XXXX’.

DATA: lw_hier_id TYPE rshi_s_rshiedirkey.
DATA: lt_nodes_a_leaves TYPE rshi_t_hienode,
lt_nodes_a_leaves_final TYPE rshi_t_hienode,
lt_nodes_a_leaves_parent TYPE rshi_t_hienode,
lt_nodes_a_leaves_temp TYPE rshi_t_hienode,
wa_leaves TYPE rshi_s_hienode.
DATA: lw_nodes_a_leaves LIKE LINE OF lt_nodes_a_leaves.

DATA: l_hier_name TYPE rshienm.
DATA: lw_hier_type TYPE rshiedir.

TYPES : BEGIN OF ty_counter ,
nodeid TYPE rshienodid,
parentid TYPE rshienodid,
childid TYPE rschild,
END OF ty_counter,

ty_t_counter TYPE TABLE OF  ty_counter.

DATA : it_counter TYPE ty_t_counter,
wa_counter TYPE ty_counter,
wa_counter1 TYPE ty_counter.

l_hier_name = p_hinm.

SELECT SINGLE * FROM rshiedir INTO lw_hier_type
WHERE hienm = l_hier_name
AND objvers = ‘A’.

IF sy-subrc = 0.
lw_hier_id-hieid = lw_hier_type-hieid.
lw_hier_id-objvers = ‘A’.

CALL FUNCTION ‘RSSH_HIERARCHY_READ’
EXPORTING
i_rshiedirkey           = lw_hier_id
IMPORTING
e_t_rsnodes             = lt_nodes_a_leaves
EXCEPTIONS
invalid_hierarchy       = 1
name_error              = 2
iobj_not_found          = 3
OTHERS                  = 4
.
IF sy-subrc <> 0.

ENDIF.

ENDIF.

READ TABLE lt_nodes_a_leaves INTO wa_leaves WITH KEY nodename = p_node.
IF sy-subrc IS INITIAL.
wa_counter-parentid = wa_leaves-nodeid.
APPEND:  wa_leaves TO lt_nodes_a_leaves_final ,
wa_counter TO it_counter.
ENDIF.

CLEAR : wa_counter,
wa_counter1.

LOOP AT it_counter INTO wa_counter .
CLEAR wa_leaves.
LOOP AT lt_nodes_a_leaves INTO wa_leaves WHERE parentid = wa_counter-parentid.
wa_counter1-parentid = wa_leaves-nodeid.

APPEND:  wa_leaves TO lt_nodes_a_leaves_final,
wa_counter1 TO it_counter.

CLEAR wa_leaves.
ENDLOOP.
CLEAR wa_counter.
ENDLOOP.

*
DELETE lt_nodes_a_leaves_final WHERE iobjnm = ‘0HIER_NODE’.

CLEAR wa_leaves.
LOOP AT lt_nodes_a_leaves_final INTO wa_leaves.
WRITE: /  wa_leaves-nodename.
ENDLOOP.

 

>> To retrieve Full hierarchy we have to include below code.

PARAMETERS : p_node TYPE rsshnodenamestr DEFAULT ‘MAGM25’,
p_hinm       TYPE rshienm DEFAULT ‘MAGM’.

DATA: lw_hier_id TYPE rshi_s_rshiedirkey.
DATA: lt_nodes_a_leaves TYPE rshi_t_hienode,
lt_nodes_a_leaves_final TYPE rshi_t_hienode,
lt_nodes_a_leaves_parent TYPE rshi_t_hienode,
lt_nodes_a_leaves_temp TYPE rshi_t_hienode,
wa_leaves TYPE rshi_s_hienode.
DATA: lw_nodes_a_leaves LIKE LINE OF lt_nodes_a_leaves.

DATA: l_hier_name TYPE rshienm.
DATA: lw_hier_type TYPE rshiedir.

TYPES : BEGIN OF ty_counter ,
nodeid TYPE rshienodid,
parentid TYPE rshienodid,
childid TYPE rschild,
END OF ty_counter,

ty_t_counter TYPE TABLE OF  ty_counter.

DATA : it_counter TYPE ty_t_counter,
wa_counter TYPE ty_counter,
wa_counter1 TYPE ty_counter.

l_hier_name = p_hinm.

SELECT SINGLE * FROM rshiedir INTO lw_hier_type
WHERE hienm = l_hier_name
AND objvers = ‘A’.

IF sy-subrc = 0.
lw_hier_id-hieid = lw_hier_type-hieid.
lw_hier_id-objvers = ‘A’.

CALL FUNCTION ‘RSSH_HIERARCHY_READ’
EXPORTING
i_rshiedirkey           = lw_hier_id
IMPORTING
e_t_rsnodes             = lt_nodes_a_leaves
EXCEPTIONS
invalid_hierarchy       = 1
name_error              = 2
iobj_not_found          = 3
OTHERS                  = 4
.
IF sy-subrc <> 0.

ENDIF.

ENDIF.

>> The internal table lt_nodes_a_leaves contains full hierarchy data.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Shanthi Bhaskar
      Shanthi Bhaskar

      Nice content.

      If you can add some screen shots to your content then it would be very easy to understand.

       

       

      Cheers

      Shanthi.

      Author's profile photo Arup Goswami
      Arup Goswami
      Blog Post Author

      Dear Shanthi,

      We do not have any options to attach any screen shot now a days.

       

      Thanks,

      Arup.

      Author's profile photo Sajid Amir
      Sajid Amir

      Shanthi, please use comments as you did here to get attention of the author. Please do not use "Alert Moderator" for such inquiries.

      Author's profile photo Shanthi Bhaskar
      Shanthi Bhaskar

      Was trying to loop in Moderator so that he would assist or guide the author in making blog/wiki more accurate with pictorial representation so that more people will read.

      Author's profile photo HARDIK PATEL
      HARDIK PATEL

      Hi,

      Can anyone of you help me, please ?

      I am loading FSV - USGAP Hierarchy in BW. Clients wants to delete or exclude all the accounts that are blocked for posting (Blocked for posting is an attribute of 0GL_ACCOUNT), it means client do not want those account in the hierarchy node.

      I am not good at all in ABAP. I would appreciate if you could provide a code for this, please?

      As you can see, i am able to delete the accounts from the H* table but then I need a code to adjust node id (Child ID and Next node ID). For example. node id 13,16,24.

       

      Could you please provide relevant abap code to achieve this, please ?

      Regards,

      Hardik Patel