Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
jan_dahl
Explorer

Ever wondered how to get out your inline created dynamic table out of your method / sub procedure?


As known, only references can be passed through a method interface. But I failed always, because the referred table was empty outside of the method (freed stack) because it is created on stack memory which is only valid inside of the method scope.

So I managed to dynamically create a table of the same structure using RTTS and copied the content of the table in there. This table can be assigned to a class attribute reference without loosing the content. Why is ABAP that laborious?

Additionally I've only the select statement as declaration of my data to process in the report. If the customer needs an additional field even from an additional table, no problem, just add it to the select statement. That's it!
REPORT zzjd_tmp2.

CLASS lc_flights DEFINITION.
  PUBLIC SECTION.
    METHODS main.
    METHODS get_data RETURNING VALUE(rr_dyntab) TYPE REF TO data.
    METHODS display_alv IMPORTING REFERENCE(ir_dyntab) type REF TO data.

  PRIVATE SECTION.
    DATA _r_datatable TYPE REF TO data.
ENDCLASS.
CLASS lc_flights IMPLEMENTATION.
  METHOD get_data.
*   create a fancy dynamic table
    SELECT FROM sflights AS f
      JOIN scarr AS ON c~carrid = f~carrid
      FIELDS c~*, f~*
      INTO TABLE @DATA(lt_flight).

*** copy table to heap memory ***
*   Create table description
    DATA(lo_tabledesc) = CAST cl_abap_tabledescr(
                           cl_abap_tabledescr=>describe_by_data( p_data = lt_flight ) ).
*   Create table in heap memory and set return reference
    CREATE DATA rr_dyntab TYPE HANDLE lo_tabledesc.

    FIELD-SYMBOLS <lt_datatable> LIKE lt_flight. " Create a field-symbol...
    ASSIGN rr_dyntab->TO <lt_datatable>.    " because append doesn't work with references
    APPEND LINES OF lt_flight TO <lt_datatable>. " Copy content to heap table
*********************************
  ENDMETHOD.
  METHOD display_alv.
    FIELD-SYMBOLS <lt_datatable> TYPE ANY TABLE.
    ASSIGN ir_dyntab->TO <lt_datatable>.

    cl_salv_table=>factory(
      IMPORTING r_salv_table   = DATA(lo_salv)
      CHANGING  t_table        = <lt_datatable>
    ).
    lo_salv->display( ).
  ENDMETHOD.
  METHOD main.
    _r_datatable = get_data( ).
    display_alv( _r_datatable ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA(lo_flights) = NEW lc_flights( ).
  lo_flights->main( ).

Is there any more simple solution for that? Please let me know in the comments.

Please follow my profile.
Read also:
25 Comments
Labels in this area