Retrieve sap cost center group hierarchy

The following code demonstrates how to retreive the cost center group hierarchy, as seen via cost center group structure transaction KSH3.

*&---------------------------------------------------------------------*
*& Report  ZCCGROUP_HIER                                               *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT ZCCGROUP_HIER.
* GSETC type pool stores codes for differenct group classes
*    i.e. gsetc_costcenter_setclass = '0101'.
TYPE-POOLS: gsetc. "
TYPES: gseth_node_line    LIKE grpobjects,
       gseth_node_tab     TYPE gseth_node_line OCCURS 0.
TYPES: gseth_val_line     LIKE grpvalues,
       gseth_val_tab      TYPE gseth_val_line OCCURS 0.
TYPES: GSETH_MD_LINE      LIKE GRPMDLINE,
       GSETH_MD_TAB       TYPE GSETH_MD_LINE OCCURS 0.
DATA: it_nodes            TYPE  gseth_node_tab,
      it_values           TYPE  gseth_val_tab,
      it_mdtab            type gseth_md_tab,
      wa_mdtab            type GSETH_MD_LINE.
DATA: ld_setid            TYPE sethier-setid,
      ld_info             LIKE grphinfo,
      ld_overwrite        LIKE sy-datar.
PARAMETERS: p_ksgru  LIKE rksb1-ksgru,
            p_burks       TYPE bseg-BUKRS.
CONCATENATE gsetc_costcenter_setclass p_burks p_ksgru INTO ld_setid.
CALL FUNCTION 'K_HIERARCHY_TABLES_READ'
  EXPORTING
    e_class                           = gsetc_costcenter_setclass
                                                 "from gsetc type-pool
    e_setid                           = ld_setid
    e_kokrs                           = p_burks "company code
    E_MANDT                           = sy-mandt
    E_MASTER_DATA                     = 'XXX'
* displays popup to confirm if a large amount of data is to be retrieved
    E_STRUCTURE                       = 'X  X0200'  "displays popup to confirm
*     E_REPLACE_CLASS                   =
*     E_REPLACE_UNIT                    =
*     E_REPLACE_TABLE                   = ' '
*     E_REPLACE_FIELD                   = ' '
*     E_SUFFIX                          =
*     E_OLD_LINE_LEVEL                  = 0
*   IMPORTING
*     I_DOUBLE_CHECK                    =
*     I_MASTER_DATA                     =
  TABLES
    t_nodes                           = it_nodes
    t_values                          = it_values
    T_MASTER_DATA                     = it_mdtab
*     T_FORMULA                         =
*     T_FIELD_INFO                      =
*     T_NODE_LIST_OVERWRITE             =
  CHANGING
    c_info                            = ld_info
    c_overwrite                       = ld_overwrite
 EXCEPTIONS
   no_controlling_area               = 1
   no_chart_of_account               = 2
   different_controlling_areas       = 3
   different_chart_of_accounts       = 4
   set_not_found                     = 5
   illegal_field_replacement         = 6
   illegal_table_replacement         = 7
   fm_raise                          = 8
   convert_error                     = 9
   no_overwrite_standard_hier        = 10
   no_bukrs_for_kokrs                = 11
   OTHERS                            = 12.
BREAK-POINT.
* An alternative function module can be found below which
* is slightly easier to code but there is no direct link between
* the hier and the val tables, it is just based on there
* index possition.
*types : begin of T_SETHIER.
*   include STRUCTURE  SETHIER_CO.
*types: end of t_sethier.
*data: it_SETHIER type standard table of t_SETHIER.
*
*types : begin of T_SETVAL.
*   include STRUCTURE  SETVAL_CO.
*types: end of t_SETVAL.
*data: it_SETVAL type standard table of t_SETVAL.
*CALL FUNCTION 'K_GROUP_REMOTE_READ'
*  EXPORTING
*    setclass         = gsetc_costcenter_setclass "from gsetc type-pool
*    CO_AREA          = 'UNIV'
**   CHRT_ACCTS       =
*    groupname        =  p_ksgru
**   LANGUAGE         =
** IMPORTING
**   RETURN           =
*  tables
*    et_sethier       = it_SETHIER
*    ET_SETVAL        = it_SETVAL.

Related Articles

SAP FI ABAP code and information to assist in the developing of financial accounting applications
Retrieve SAP cost element group hierarchy
Validate SAP Customer number