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

[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.