How to Download a Hierarchy to a Flat File

15
How-to Guide SAP NetWeaver ‘04 How to… Download a Hierarchy to a Flat File Version 1.00 – May 2004 Applicable Releases: SAP NetWeaver ’04 (SAP BW 3.5)

Transcript of How to Download a Hierarchy to a Flat File

Page 1: How to Download a Hierarchy to a Flat File

How-to Guide SAP NetWeaver ‘04

How to… Download a Hierarchy to a Flat File Version 1.00 – May 2004 Applicable Releases: SAP NetWeaver ’04 (SAP BW 3.5)

Page 2: How to Download a Hierarchy to a Flat File

© Copyright 2004 SAP AG. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, MVS/ESA, AIX, S/390, AS/400, OS/390, OS/400, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere, Netfinity, Tivoli, and Informix are trademarks or registered trademarks of IBM Corporation in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. MaxDB is a trademark of MySQL AB, Sweden.

SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and in several other countries all over the world. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty. These materials are provided “as is” without a warranty of any kind, either express or implied, including but not limited to, the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. SAP shall not be liable for damages of any kind including without limitation direct, special, indirect, or consequential damages that may result from the use of these materials. SAP does not warrant the accuracy or completeness of the information, text, graphics, links or other items contained within these materials. SAP has no control over the information that you may access through the use of hot links contained in these materials and does not endorse your use of third party web pages nor provide any warranty whatsoever relating to third party web pages. SAP NetWeaver “How-To” Guides are intended to simplify the product implementation. While specific product features and procedures typically are explained in a practical business context, it is not implied that those features and procedures are the only approach in solving a specific business problem using SAP NetWeaver. Should you wish to receive additional information, clarification or support, please refer to SAP Consulting.

Page 3: How to Download a Hierarchy to a Flat File

- 1 -

1 Business Scenario

This document describes the how to download a master data hierarchy to a flat file. The file format matches the hierarchy upload format for sorted and time-depended hierarchies (IDOC).

2 Introduction

A custom program is being provided that can be used to easily download any master data hierarchy.

Page 4: How to Download a Hierarchy to a Flat File

- 2 -

3 The Step By Step Solution

3.1 Implement Program Using transaction SE38 implement ABAP program Z_SAP_HIERARCHY_DOWNLOAD (see Appendix). You can find the text elements for the selection screen in the coding.

3.2 Execute Program Start the program. On the selection screen, first enter or pick the InfoObject. Then you can select the hierarchy that you want to download. Optionally, you can enter which language of hierarchy node descriptions should be downloaded. As a default, the logon language is being used. Last but not least you enter or select the file name for the download.

Note: If the file exists already, it will be overwritten with the current hierarchy. If the download was successful, then a popup is displayed:

3.3 Hierarchy Upload In order to upload the file into BW, go to the Administrator Workbench (transaction RSA1) and create a hierarchy InfoSource. To match the download file format, you should use the IDOC transfer method:

Page 5: How to Download a Hierarchy to a Flat File

- 3 -

Page 6: How to Download a Hierarchy to a Flat File

- 4 -

Create a new hierarchy structure:

Select the options “Sorted hierarchy”, “Interval”, and “Time-dependent”. The structure will then match the download format.

Page 7: How to Download a Hierarchy to a Flat File

- 5 -

Create an InfoPackage. The file type is “ASCII”.

Now you can schedule the file upload

Page 8: How to Download a Hierarchy to a Flat File

- 6 -

4 Comments

Please note that Open Hub licenses are required if data is to be extracted and transferred from mySAP BI to third party target systems.

5 Appendix

************************************************************************ * SAP Consulting BW Tools: * ------------------------ * Download hierarchy into a flat file. The file has the correct format * for uploading into BW via a hierarchy InfoSource (IDOC). * * (c) SAP AG 2003-2004 MFB, SAP Labs LLC * created: 2003-07-23 * last update: 2004-05-02 ************************************************************************ * Text elements: * P_DATES Include from/to dates * P_DATETO Valid-to date * P_FNAME File name * P_HIENM Hierarchy name * P_INTER Include from/to leaves * P_IOBJNM InfoObject * P_LANGU Language * P_VERS Hierarchy version ************************************************************************ REPORT z_sap_hierarchy_download. TYPE-POOLS: rs, rsdm, rrh1. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME. PARAMETERS: p_iobjnm TYPE rsdiobjnm MEMORY ID rsc. SELECTION-SCREEN END OF BLOCK b1. SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME. PARAMETERS: p_hienm TYPE rshiedir-hienm, p_vers TYPE rshiedir-version, p_dateto TYPE rshiedir-dateto, p_langu TYPE rshiedirt-langu. SELECTION-SCREEN END OF BLOCK b2. SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME. PARAMETERS: p_fname LIKE rlgrap-filename, p_dates AS CHECKBOX DEFAULT 'X', p_inter AS CHECKBOX DEFAULT 'X'. SELECTION-SCREEN END OF BLOCK b3. * File structure TYPES: * No dates/intervals BEGIN OF y_s_hierfile_1, nodeid TYPE rshienodid,

Page 9: How to Download a Hierarchy to a Flat File

- 7 -

iobjnm TYPE rsiobjnm, nodename TYPE rsnodename, * tlevel TYPE rstlevel, link TYPE rslink, parentid TYPE rsparent, childid TYPE rschild, nextid TYPE rsnext, 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, * With dates BEGIN OF y_s_hierfile_2, nodeid TYPE rshienodid, iobjnm TYPE rsiobjnm, nodename TYPE rsnodename, * tlevel TYPE rstlevel, link TYPE rslink, parentid TYPE rsparent, childid TYPE rschild, nextid TYPE rsnext, dateto TYPE rsdateto, datefrom TYPE rsdatefrom, langu TYPE langu, txtsh TYPE rstxtsh, txtmd TYPE rstxtmd, txtlg TYPE rstxtlg, END OF y_s_hierfile_2, y_t_hierfile_2 TYPE STANDARD TABLE OF y_s_hierfile_2, * With intervals BEGIN OF y_s_hierfile_3, nodeid TYPE rshienodid, iobjnm TYPE rsiobjnm, nodename TYPE rsnodename, * tlevel TYPE rstlevel, link TYPE rslink, parentid TYPE rsparent, childid TYPE rschild, nextid TYPE rsnext, leafto TYPE rsleafto, leaffrom TYPE rsleaffrom, langu TYPE langu, txtsh TYPE rstxtsh, txtmd TYPE rstxtmd, txtlg TYPE rstxtlg, END OF y_s_hierfile_3, y_t_hierfile_3 TYPE STANDARD TABLE OF y_s_hierfile_3, * With dates/intervals BEGIN OF y_s_hierfile_4, nodeid TYPE rshienodid, iobjnm TYPE rsiobjnm, nodename TYPE rsnodename, * tlevel TYPE rstlevel, link TYPE rslink, parentid TYPE rsparent, childid TYPE rschild, nextid TYPE rsnext, dateto TYPE rsdateto, datefrom TYPE rsdatefrom, leafto TYPE rsleafto, leaffrom TYPE rsleaffrom, langu TYPE langu, txtsh TYPE rstxtsh, txtmd TYPE rstxtmd, txtlg TYPE rstxtlg,

Page 10: How to Download a Hierarchy to a Flat File

- 8 -

END OF y_s_hierfile_4, y_t_hierfile_4 TYPE STANDARD TABLE OF y_s_hierfile_4. * Hierarchy definition DATA: g_s_hiesel TYPE rsndi_s_hiesel, g_s_hiedir TYPE rsndi_s_hiedir, g_subrc TYPE sy-subrc, g_t_hiedirt TYPE TABLE OF rshiedirt, g_s_hierstruc TYPE rssh_s_htab, g_t_hierstruc TYPE TABLE OF rssh_s_htab, g_s_thiernode TYPE rsthiernode, g_t_thiernode TYPE TABLE OF rsthiernode WITH KEY langu hieid objvers nodename, g_s_hierintvl TYPE rssh_s_jtab, g_t_hierintvl TYPE TABLE OF rssh_s_jtab WITH KEY hieid objvers nodeid, g_s_message TYPE rsndi_s_message, g_t_message TYPE TABLE OF rsndi_s_message, g_s_chavlinfo TYPE rsdm_s_chavlinfo, g_t_chavlinfo TYPE rsdm_t_chavlinfo. * File DATA: g_fname TYPE string, g_struct_s TYPE string, g_struct_t TYPE string, gr_s_file TYPE REF TO data, gr_t_file TYPE REF TO data. FIELD-SYMBOLS: <g_langu> TYPE ANY, <g_s_file> TYPE ANY, <g_t_file> TYPE STANDARD TABLE. *----------------------------------------------------------------------- AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_iobjnm. CALL FUNCTION 'RSD_IOBJ_F4' EXPORTING i_show_cha = rs_c_true i_objvers = rs_c_objvers-active i_hietabfl = rs_c_true CHANGING c_iobjnm = p_iobjnm EXCEPTIONS illegal_input = 1. CHECK sy-subrc = 0. *----------------------------------------------------------------------- AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_hienm. DATA: l_s_hiertxt TYPE rrh1_s_hiertxt, l_t_hiertxt TYPE rrh1_t_hiertxt. CALL FUNCTION 'RRH1_HIERARCHY_HELP_VALUES_GET' EXPORTING i_iobjnm = p_iobjnm i_dateto = p_dateto i_hienm = p_hienm i_version = p_vers IMPORTING e_t_hiertxt = l_t_hiertxt EXCEPTIONS no_f4_available = 1 dialogue_canceled = 2 OTHERS = 3.

Page 11: How to Download a Hierarchy to a Flat File

- 9 -

CHECK sy-subrc = 0. READ TABLE l_t_hiertxt INTO l_s_hiertxt INDEX 1. CHECK NOT l_s_hiertxt IS INITIAL. p_hienm = l_s_hiertxt-hienm. DATA: l_s_dynpfields TYPE dynpread, l_t_dynpfields TYPE STANDARD TABLE OF dynpread. CLEAR: l_t_dynpfields, l_s_dynpfields. l_s_dynpfields-fieldname = 'P_VERS'. WRITE l_s_hiertxt-version TO l_s_dynpfields-fieldvalue. APPEND l_s_dynpfields TO l_t_dynpfields. l_s_dynpfields-fieldname = 'P_DATETO'. WRITE l_s_hiertxt-dateto TO l_s_dynpfields-fieldvalue. APPEND l_s_dynpfields TO l_t_dynpfields. CALL FUNCTION 'DYNP_VALUES_UPDATE' EXPORTING dyname = sy-repid dynumb = sy-dynnr TABLES dynpfields = l_t_dynpfields. *----------------------------------------------------------------------- AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname. DATA: l_filename1 TYPE string, l_filename2 TYPE string, l_path TYPE string, l_fullpath TYPE string, l_action TYPE i. l_filename1 = p_fname. CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING window_title = 'Select Download File' default_extension = 'txt' default_file_name = l_filename1 file_filter = 'All Files (*.*)|*.*|Text files (*.txt)|*.txt' CHANGING filename = l_filename2 path = l_path fullpath = l_fullpath user_action = l_action EXCEPTIONS cntl_error = 1 OTHERS = 2. "#EC NOTEXT CHECK sy-subrc = 0. CALL METHOD cl_gui_cfw=>flush. IF l_action = 0. p_fname = l_fullpath. ENDIF. *----------------------------------------------------------------------- INITIALIZATION. GET PARAMETER ID 'RSC' FIELD p_iobjnm. *----------------------------------------------------------------------- START-OF-SELECTION.

Page 12: How to Download a Hierarchy to a Flat File

- 10 -

* Check input CHECK NOT p_fname IS INITIAL. IF p_langu IS INITIAL. p_langu = sy-langu. ENDIF. IF p_dateto IS INITIAL. p_dateto = '99991231'. ENDIF. * Read hierarchy CLEAR g_s_hiesel. g_s_hiesel-objvers = rs_c_objvers-active. g_s_hiesel-hienm = p_hienm. g_s_hiesel-version = p_vers. g_s_hiesel-iobjnm = p_iobjnm. g_s_hiesel-dateto = p_dateto. CALL FUNCTION 'RSNDI_SHIE_STRUCTURE_GET' EXPORTING i_s_hiesel = g_s_hiesel i_no_nodenm_table = rs_c_true IMPORTING e_s_hiedir = g_s_hiedir e_subrc = g_subrc TABLES e_t_hiedirt = g_t_hiedirt e_t_hierstruc = g_t_hierstruc e_t_thiernode = g_t_thiernode e_t_hierintvl = g_t_hierintvl e_t_message = g_t_message. IF g_subrc <> 0. READ TABLE g_t_message INTO g_s_message INDEX 1. IF sy-subrc = 0. MESSAGE ID g_s_message-msgid TYPE 'I' NUMBER g_s_message-msgno WITH g_s_message-msgv1 g_s_message-msgv2 g_s_message-msgv3 g_s_message-msgv4. ELSE. MESSAGE ID 'RSBO' TYPE 'I' NUMBER 899 WITH 'Hierarchy read error'. ENDIF. EXIT. ENDIF. * Defined output structures IF p_dates IS INITIAL AND p_inter IS INITIAL. g_struct_s = 'Y_S_HIERFILE_1'. g_struct_t = 'Y_T_HIERFILE_1'. ELSEIF p_dates = 'X' AND p_inter IS INITIAL. g_struct_s = 'Y_S_HIERFILE_2'. g_struct_t = 'Y_T_HIERFILE_2'. ELSEIF p_dates IS INITIAL AND p_inter = 'X'. g_struct_s = 'Y_S_HIERFILE_3'. g_struct_t = 'Y_T_HIERFILE_3'. ELSE. g_struct_s = 'Y_S_HIERFILE_4'. g_struct_t = 'Y_T_HIERFILE_4'. ENDIF. CREATE DATA gr_s_file TYPE (g_struct_s). ASSIGN gr_s_file->* TO <g_s_file>. CREATE DATA gr_t_file TYPE (g_struct_t). ASSIGN gr_t_file->* TO <g_t_file>. * Nodes REFRESH <g_t_file>.

Page 13: How to Download a Hierarchy to a Flat File

- 11 -

LOOP AT g_t_hierstruc INTO g_s_hierstruc. CLEAR <g_s_file>. MOVE-CORRESPONDING g_s_hierstruc TO <g_s_file>. * Texts for nodes READ TABLE g_t_thiernode INTO g_s_thiernode WITH TABLE KEY langu = p_langu hieid = g_s_hierstruc-hieid objvers = rs_c_objvers-active nodename = g_s_hierstruc-nodename. IF sy-subrc = 0. MOVE-CORRESPONDING g_s_thiernode TO <g_s_file>. ELSE. * Texts for characteristic values REFRESH g_t_chavlinfo. CLEAR g_s_chavlinfo. g_s_chavlinfo-c_chavl = g_s_hierstruc-nodename. APPEND g_s_chavlinfo TO g_t_chavlinfo. CALL FUNCTION 'RSD_CHAVL_READ_ALL' EXPORTING i_iobjnm = g_s_hierstruc-iobjnm i_langu = p_langu i_dateto = p_dateto i_check_value = space i_sid_in = space i_hieid = g_s_hiedir-hieid i_objvers = g_s_hiedir-objvers CHANGING c_t_chavlinfo = g_t_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 g_t_chavlinfo INTO g_s_chavlinfo INDEX 1. IF sy-subrc = 0. MOVE-CORRESPONDING g_s_chavlinfo-e_chatexts TO <g_s_file>. ASSIGN COMPONENT 'LANGU' OF STRUCTURE <g_s_file> TO <g_langu>. IF sy-subrc = 0. <g_langu> = p_langu. ENDIF. ENDIF. ENDIF. ENDIF. * Intervals IF g_s_hierstruc-intervl = 'X' AND p_inter = 'X'. READ TABLE g_t_hierintvl INTO g_s_hierintvl WITH TABLE KEY hieid = g_s_hierstruc-hieid objvers = rs_c_objvers-active nodeid = g_s_hierstruc-nodeid. IF sy-subrc = 0. MOVE-CORRESPONDING g_s_hierintvl TO <g_s_file>. ENDIF. ENDIF. APPEND <g_s_file> TO <g_t_file>. ENDLOOP. * Download output table g_fname = p_fname. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = g_fname

Page 14: How to Download a Hierarchy to a Flat File

- 12 -

write_field_separator = space TABLES data_tab = <g_t_file> EXCEPTIONS file_write_error = 1 no_batch = 2 gui_refuse_filetransfer = 3 invalid_type = 4 no_authority = 5 unknown_error = 6 header_not_allowed = 7 separator_not_allowed = 8 filesize_not_allowed = 9 header_too_long = 10 dp_error_create = 11 dp_error_send = 12 dp_error_write = 13 unknown_dp_error = 14 access_denied = 15 dp_out_of_memory = 16 disk_full = 17 dp_timeout = 18 file_not_found = 19 dataprovider_exception = 20 control_flush_error = 21 OTHERS = 22. IF sy-subrc = 0. MESSAGE ID 'RSBO' TYPE 'I' NUMBER 899 WITH 'Hierarchy download successful!'. ELSE. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.

Page 15: How to Download a Hierarchy to a Flat File

www.sdn.sap.com/irj/sdn/howtoguides