Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
MGrob
Active Contributor

The business scenario:

We use product hierarchy from ECC for our SAP Companies. Our non SAP Companies use a subset of the products for reporting purposes. We only want to provide a shorter hierarchy to them.

 

The goal:

To create a product hierarchy based on an attribute that is flagged and indicates if it is relevant for the shorter version of the new hierarchy or not.
We also exchanged some texts of the hierarchy along the way. This shall all be fully automated via process chain.

Approach:

We started off with the help of http://wiki.sdn.sap.com/wiki/display/BI/Hierarchy+Download+to+Flatfile and enhanced to meet our requirements.

The processchain loads the original hierarchy first; extracts the middle texts we exchange into DSO then we have the ABAP programm wit RC creating the shorter hierarchy and download it to a flat file. From there an infopackage is loading and activating the new shorter hierarchy.

Some exlainations to the code:

REPORT ZBW_BPS_PRODHIER_DOWNLOAD               .
TYPE-POOLS: rs, rsdm, rrh1.
CONSTANTS: c_DIR_ALIAS TYPE STRING VALUE 'DIR_BWDATA',
           c_repname TYPE STRING VALUE 'ZBW_BPS_PRODHIER_DOWNLOAD'.

* File structure
TYPES:


* No dates/intervals

BEGIN OF y_s_hierfile_1,
  nodeid TYPE rshienodid,
  iobjnm TYPE rsiobjnm,
  nodename TYPE rsnodename,
  link TYPE rslink,
  parentid TYPE rsparent,
  langu TYPE langu,
  txtsh TYPE rstxtsh,
  txtmd TYPE rstxtmd,
  txtlg TYPE rstxtlg,
END OF y_s_hierfile_1,
y_t_hierfile_1 TYPE STANDARD TABLE OF y_s_hierfile_1.

DATA: lr_hiesel TYPE rsndi_s_hiesel,
      lr_hiedir TYPE rsndi_s_hiedir,
      lr_hiekey TYPE RSNDI_S_HIEKEY,
      lt_hiedirt TYPE TABLE OF rshiedirt,
      lt_hierstruc TYPE TABLE OF rssh_s_htab,
      lr_hierstruc TYPE rssh_s_htab,
      lt_thiernode TYPE TABLE OF rsthiernode
        WITH KEY langu hieid objvers nodename,
      lr_thiernode TYPE rsthiernode,
      lt_hierintvl TYPE TABLE OF rssh_s_jtab
        WITH KEY hieid objvers nodeid,
      lt_message TYPE TABLE OF rsndi_s_message,
      lr_message TYPE rsndi_s_message,
      lr_chavlinfo TYPE rsdm_s_chavlinfo,
      lt_chavlinfo TYPE rsdm_t_chavlinfo,
      l_subrc TYPE SY-SUBRC,
      l_txtmd TYPE RSTXTMD,
      l_zmm_acm TYPE /BIC/OIZMM_ACM,

      lr_file TYPE y_s_hierfile_1,
      lt_file TYPE y_t_hierfile_1,
      lr_file2 TYPE y_s_hierfile_1,
      lt_file2 TYPE y_t_hierfile_1,

      lr_user_dir TYPE USER_DIR,
      l_filename TYPE STRING,
      t_ref type ref to CX_SY_FILE_AUTHORITY,
      err_txt type string,

      lr_report_status TYPE ZRSPC_REP_STATUS,
      l_hienm TYPE rshiedir-hienm VALUE 'M20 BW 2007', "<-Name of the original hierarchy
      l_hieid TYPE RSHIEID.


* read hierarchy id for M20 BW 07 hierarchy
SELECT SINGLE HIEID FROM RSHIEDIR
  INTO l_hieid
  WHERE HIENM = l_hienm AND
        OBJVERS = 'A' AND
        IOBJNM = '0PROD_HIER'. " <- Use your Hierarchy object

lr_hiekey-hieid = l_hieid.
lr_hiekey-objvers = rs_c_objvers-active.
lr_hiesel-objvers = rs_c_objvers-active.
lr_hiesel-hienm = l_hienm.
lr_hiesel-iobjnm = '0PROD_HIER'. " <- Use your Hierarchy object
lr_hiesel-dateto = '99991231'.

CALL FUNCTION 'RSNDI_SHIE_STRUCTURE_GET'
  EXPORTING
    I_S_HIEKEY        = lr_hiekey
    i_s_hiesel        = lr_hiesel
    i_no_nodenm_table = rs_c_true
  IMPORTING
    e_s_hiedir        = lr_hiedir
    e_subrc           = l_subrc
  TABLES
    e_t_hiedirt       = lt_hiedirt
    e_t_hierstruc     = lt_hierstruc
    e_t_thiernode     = lt_thiernode
    e_t_hierintvl     = lt_hierintvl
    e_t_message       = lt_message.

IF l_subrc <> 0.
  READ TABLE lt_message INTO lr_message INDEX 1.
  IF sy-subrc = 0.
    MESSAGE ID lr_message-msgid TYPE 'I' NUMBER lr_message-msgno
      WITH lr_message-msgv1 lr_message-msgv2
           lr_message-msgv3 lr_message-msgv4.
  ELSE.
    MESSAGE ID 'RSBO' TYPE 'I' NUMBER 899
      WITH 'Hierarchy read error'.
  ENDIF.
  EXIT.
ENDIF.

* Nodes
REFRESH lt_file.

LOOP AT lt_hierstruc INTO lr_hierstruc.
  CLEAR lr_file.
  MOVE-CORRESPONDING lr_hierstruc TO lr_file.

* Texts for nodes
  READ TABLE lt_thiernode INTO lr_thiernode WITH TABLE KEY
    langu = 'E' hieid = lr_hierstruc-hieid
    objvers = rs_c_objvers-active nodename = lr_hierstruc-nodename.
  IF sy-subrc = 0.
    MOVE-CORRESPONDING lr_thiernode TO lr_file.
  ELSE.


* Texts for characteristic values
    REFRESH lt_chavlinfo.
    CLEAR lr_chavlinfo.
    lr_chavlinfo-c_chavl = lr_hierstruc-nodename.
    APPEND lr_chavlinfo TO lt_chavlinfo.

    CALL FUNCTION 'RSD_CHAVL_READ_ALL'
      EXPORTING
        i_iobjnm                  = lr_hierstruc-iobjnm
        i_langu                   = 'E'
        i_dateto                  = '99991231'
        i_check_value             = space
        i_sid_in                  = space
        i_hieid                   = lr_hiedir-hieid
        i_objvers                 = lr_hiedir-objvers
      CHANGING
        c_t_chavlinfo             = lt_chavlinfo
      EXCEPTIONS
        info_object_not_found     = 1
        routines_generation_error = 2
        check_table_not_existing  = 3
        text_table_not_existing   = 4
        OTHERS                    = 5.
    IF sy-subrc = 0.
      READ TABLE lt_chavlinfo INTO lr_chavlinfo INDEX 1.
      IF sy-subrc = 0.
        MOVE-CORRESPONDING lr_chavlinfo-e_chatexts TO lr_file.
      ENDIF.
    ENDIF.
  ENDIF.

* We add special text for some of the hierarchy nodes
  IF lr_file-iobjnm = '0HIER_NODE'.
    IF lr_file-NODENAME(31) = '0000000000000000000000000000000'.
      lr_file-NODENAME = lr_file-NODENAME+31(1).
    ENDIF.

    SELECT SINGLE TXTMD FROM /BIC/AZCOPAO0400 "<- read from dso
      INTO l_txtmd
      WHERE PROD_HIER = lr_file-nodename.

    IF SY-SUBRC = 0.
      lr_file-txtsh = l_txtmd(20).
      lr_file-txtmd = l_txtmd.
      lr_file-txtlg = l_txtmd.
    ENDIF.
  ENDIF.

  lr_file-langu = 'E'.

*Attribute that determins if the node is in the shorter hierarchy
  IF lr_file-iobjnm <> '0HIER_NODE'.
    SELECT SINGLE /BIC/ZMM_ACM FROM /BI0/PPROD_HIER "<- use your IO /Hierarchy
      INTO l_zmm_acm
      WHERE PROD_HIER = lr_file-nodename AND
            OBJVERS = 'A'.

    IF l_zmm_acm <> '002'. "<- if the value in our case is 002 then keep it
      APPEND lr_file TO lt_file2.
    ENDIF.
  ENDIF.
  APPEND lr_file TO lt_file.
ENDLOOP.

LOOP AT lt_file2 INTO lr_file2.
* check wether the node is a parent. No removal possible if yes
  READ TABLE lt_file TRANSPORTING NO FIELDS
    WITH KEY PARENTID = lr_file2-NODEID.

  IF SY-SUBRC <> 0.
    READ TABLE lt_file TRANSPORTING NO FIELDS
      WITH KEY NODEID = lr_file2-NODEID.
    DELETE lt_file INDEX SY-TABIX.
  ENDIF.
ENDLOOP.

* remove unnecessary text nodes from the hierarchy (empty ones)
lt_file2 = lt_file.
LOOP AT lt_file2 INTO lr_file2
  WHERE IOBJNM = '0HIER_NODE'.
* check wether the node is a parent. No removal possible if yes
  READ TABLE lt_file TRANSPORTING NO FIELDS
    WITH KEY PARENTID = lr_file2-NODEID.
  IF SY-SUBRC <> 0.
    READ TABLE lt_file TRANSPORTING NO FIELDS
      WITH KEY NODEID = lr_file2-NODEID.
    DELETE lt_file INDEX SY-TABIX.
  ENDIF.
ENDLOOP.

* get user directory
SELECT SINGLE * FROM USER_DIR
  INTO lr_user_dir
  WHERE ALIASS = c_DIR_ALIAS.

* Back to downloading the hierarchy to a text file
IF SY-SUBRC = 0.
CONCATENATE lr_user_dir-dirname '\PROD_HIER_HIER_BPS.txt'
    INTO l_filename.
  try.
    OPEN DATASET l_filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
    catch CX_SY_FILE_AUTHORITY INTO t_ref.
      err_txt = t_ref->get_longtext( ).
  endtry.

  IF SY-SUBRC = 0.
    LOOP AT lt_file INTO lr_file.
      TRANSFER lr_file TO l_filename.
    ENDLOOP.
  ELSE.
    l_subrc = 2.
  ENDIF.
  CLOSE DATASET l_filename.
ENDIF.


* update status table with return code for process chain type
* report with RC
lr_report_status-report = c_repname.
lr_report_status-status = l_subrc.
INSERT INTO ZRSPC_REP_STATUS values lr_report_status.
IF SY-SUBRC <> 0.
  UPDATE ZRSPC_REP_STATUS FROM lr_report_status.
ENDIF.

Labels in this area