Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[ABAP] Download Transport as ZIP

Related Reports:
https://nocin.eu/abap-import-transport-from-zip/
https://nocin.eu/abap-create-toc-for-a-given-transport-release-it-and-download-it-as-zip/

This report can be handy, especially since S/4HANA 2023 seems to have restricted the “classic” import way by using TCode CG3Y and CG3Z. See note 1949906, where it is recommended to create a custom report.

*&---------------------------------------------------------------------*
*& Report ZIP_TRANSPORT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zip_transport.

SELECTION-SCREEN BEGIN OF BLOCK bl01 WITH FRAME TITLE TEXT-t01.
PARAMETERS p_trkorr LIKE e070-trkorr OBLIGATORY.
PARAMETERS p_ttext  TYPE as4text.
SELECTION-SCREEN END OF BLOCK bl01.

SELECTION-SCREEN BEGIN OF BLOCK bl02 WITH FRAME TITLE TEXT-t02.
PARAMETERS p_sapdir TYPE string LOWER CASE OBLIGATORY DEFAULT '/usr/sap/trans/'.
PARAMETERS p_lcldir TYPE string LOWER CASE OBLIGATORY DEFAULT 'C:\temp\'.
SELECTION-SCREEN END OF BLOCK bl02.


START-OF-SELECTION.

  " Check if Transport is released
  DATA ls_request TYPE trwbo_request.
  CALL FUNCTION 'TR_READ_REQUEST'
    EXPORTING
      iv_read_e070     = abap_true
      iv_read_e07t     = abap_true
      iv_trkorr        = p_trkorr
    CHANGING
      cs_request       = ls_request
    EXCEPTIONS
      error_occured    = 1
      no_authorization = 2
      OTHERS           = 3.
  IF ls_request-h-trstatus <> 'R'.
    MESSAGE 'Transport not yet released' TYPE 'E'.
  ENDIF.


  " Read released Transport
  DATA lv_xcontent_k TYPE xstring.
  DATA lv_xcontent_r TYPE xstring.

  DATA(lv_transdir_k) = |{ p_sapdir }cofiles/K{ p_trkorr+4 }.{ p_trkorr(3) }|.
  DATA(lv_transdir_r) = |{ p_sapdir }data/R{    p_trkorr+4 }.{ p_trkorr(3) }|.


  TRY.

      " K
      OPEN  DATASET lv_transdir_k FOR INPUT IN BINARY MODE.
      READ  DATASET lv_transdir_k INTO lv_xcontent_k.
      CLOSE DATASET lv_transdir_k.

      " R
      OPEN  DATASET lv_transdir_r FOR INPUT IN BINARY MODE.
      READ  DATASET lv_transdir_r INTO lv_xcontent_r.
      CLOSE DATASET lv_transdir_r.

      " Add to ZIP
      DATA(lo_zipper) = NEW cl_abap_zip( ).

      lo_zipper->add( name    = |K{ p_trkorr+4 }.{ p_trkorr(3) }|
                      content = lv_xcontent_k ).

      lo_zipper->add( name    = |R{ p_trkorr+4 }.{ p_trkorr(3) }|
                      content = lv_xcontent_r ).


      " Download ZIP
      DATA(lv_xzip) = lo_zipper->save( ).

      " Convert to raw data
      DATA(lt_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xzip ).

      " Set zip filename
      DATA(lv_zip_name) = COND #( WHEN p_ttext IS INITIAL THEN |{ ls_request-h-as4text }_{ p_trkorr }|
                                                          ELSE |{ p_ttext              }_{ p_trkorr }| ).

      " Replace every character that is not [a-zA-Z0-9_] with '_'.
      REPLACE ALL OCCURRENCES OF REGEX '[^\w]+' IN lv_zip_name WITH '_'.

      cl_gui_frontend_services=>gui_download( EXPORTING filename = p_lcldir && lv_zip_name && '.zip'
                                                        filetype = 'BIN'
                                              CHANGING  data_tab = lt_data ).

    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'E'.
  ENDTRY.

  MESSAGE |{ lv_zip_name }.zip created and downloaded to { p_lcldir }| TYPE 'S'.

[ABAP] Export to memory / Import from memory

Exporting:

DATA: l_p0001  TYPE p0001,
      l_return TYPE bapireturn1.

"...

DATA(l_guid) = cl_system_uuid=>create_uuid_c32_static( ).       
 
EXPORT p0001 = l_p0001 TO MEMORY ID l_guid.
SUBMIT z_hr_report WITH p_guid = l_guid AND RETURN.
IMPORT return = l_return FROM MEMORY ID l_guid.

Importing:

REPORT z_hr_report.

PARAMETERS p_guid TYPE sysuuid_c32.

DATA: l_p0001  TYPE p0001,
      l_return TYPE bapireturn1.

START-OF-SELECTION.

IMPORT p0001 = l_p0001 FROM MEMORY ID p_guid.
CHECK sy-subrc = 0.

"do stuff...
CALL FUNCTION 'BAPI_EMPLOYEE_ENQUEUE'
  EXPORTING
    number = l_p0001-pernr
  IMPORTING
    return = l_return.

IF l_return-type = 'E'.
  EXPORT return = l_return TO MEMORY ID p_guid.
  RETURN.
ENDIF.

[ABAP] Download internal table as TXT or CSV

" ty_output and tt_output are just dummy types

DATA: l_output_line    TYPE ty_output,
      l_output_lines   TYPE tt_output,  
      l_csv_output     TYPE truxs_t_text_data,
      l_txt_output     TYPE TABLE OF string,

PARAMETERS: p_alv   RADIOBUTTON GROUP rb1 DEFAULT 'X' USER-COMMAND radio,
            p_csv   RADIOBUTTON GROUP rb1,
            p_flcsv TYPE rlgrap-filename DEFAULT 'c:\temp\file.csv',
            p_txt   RADIOBUTTON GROUP rb1,
            p_fltxt TYPE rlgrap-filename DEFAULT 'c:\temp\file.txt'.

" fill table l_output_lines with your data you want to export as txt or csv

  IF p_csv = abap_true.

    CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
      TABLES
        i_tab_sap_data       = l_output_lines
      CHANGING
        i_tab_converted_data = l_csv_output.

    TRY.
        cl_gui_frontend_services=>gui_download( EXPORTING filename = CONV #( p_flcsv )
                                                          filetype = 'ASC'
                                                CHANGING  data_tab = l_csv_output ).  
      CATCH cx_root INTO DATA(e_text).
        MESSAGE e_text->get_text( ) TYPE 'I'.
    ENDTRY.


  ELSEIF p_txt = abap_true.

    LOOP AT l_output_lines INTO l_output_line.
      CALL FUNCTION 'SO_STRUCT_TO_CHAR'
        EXPORTING
          ip_struct = l_output_line
        IMPORTING
          ep_string = l_string.
      APPEND l_string TO l_txt_output.
    ENDLOOP.

    TRY.
        cl_gui_frontend_services=>gui_download( EXPORTING filename = CONV #( p_fltxt )
                                                          filetype = 'ASC'
                                                CHANGING  data_tab = l_txt_output ). 
      CATCH cx_root INTO e_text.
        MESSAGE e_text->get_text( ) TYPE 'I'.
    ENDTRY.

  ENDIF.