Get Cost Center & Profit Center Hierarchy
Some reports may need to use Profit Center or Cost Center hierarchy defined in the SAP Master Data. Rather than define a table to provide the same information, you could use function module provided by SAP.
Lets say that we have Cost Center hierarchy like below and we want to get all cost center below the CTR group.
First, provide internal tables to contain the results like following
DATA : LT_SETLIST LIKE SETLIST OCCURS 0 WITH HEADER LINE, LT_sethier LIKE STANDARD TABLE OF sethier, LT_setvalues LIKE STANDARD TABLE OF setvalues.
G_SET_LIST_SELECT to get internal SAP name for the desired group name. Parameters needed are Group Name, Controlling Area, and Financial Statement Version.
CALL FUNCTION 'G_SET_LIST_SELECT' EXPORTING SETCLASS = '0101' SHORTNAME = 'CTR' KOKRS = 'KOKRS1' KTOPL = 'KTOPL1' TABLES MATCHING_SETS = LT_SETLIST. IF LT_SETLIST IS INITIAL. MESSAGE E002(SY) WITH 'Cost Center group does not exist'. EXIT. ELSE. READ TABLE LT_SETLIST INDEX 1. ENDIF.
Use class ‘0101’ for Cost Center Group & ‘0106’ for Profit Center Group.
The result of the previous statement then used in the following function.
CALL FUNCTION 'G_SET_TREE_IMPORT' EXPORTING SETID = LT_SETLIST-SETNAME TABLES SET_HIERARCHY = LT_SETHIER SET_VALUES = LT_SETVALUES EXCEPTIONS SET_NOT_FOUND = 1 ILLEGAL_FIELD_REPLACEMENT = 2 ILLEGAL_TABLE_REPLACEMENT = 3 OTHERS = 4.
If the statement are succesfully executed,
LT_SETHIER contain(s) first level cost center group(s) included in the CTR hierarchy, while
LT_SETVALUES contain(s) first level cost center(s) in the CTR hierarchy.
Thus, using the above example,
LT_SETHIER contents are CTR-A and CTR-B, while
LT_SETVALUES contents are 001-Z-CTR and 002-Z-CTR.
Use the above exampe inside a loop to access all cost center included in the CTR hierarchy.