1

I found a solution here which I try to apply.

 cl_salv_bs_runtime_info=>set(
  EXPORTING
    display  = abap_false
    metadata = abap_false
    data     = abap_true
).

SUBMIT ('RM07MLBS')
AND RETURN.


DATA: lt_outtab TYPE STANDARD TABLE OF alv_t_t2.
FIELD-SYMBOLS: <lt_outtab> like lt_outtab.
DATA lo_data TYPE REF TO data.

TRY.
    " get data from SALV model"&nbsp;
    cl_salv_bs_runtime_info=>get_data_ref(
          IMPORTING
            r_data = lo_data
    ).
    ASSIGN lo_data->* to <lt_outtab>.
    BREAK-POINT.

  CATCH cx_salv_bs_sc_runtime_info.
ENDTRY.

Source: http://zevolving.com/2015/07/salv-table-22-get-data-directly-after-submit/

But this does not work. I get a type mismatch error in this line:

ASSIGN lo_data->* to <lt_outtab>.

What could be wrong?

Is there a way to do this generic? At runtime I don't know which report is to be called.

My overall goal is to get the report in XML or JSON format.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
guettli
  • 25,042
  • 81
  • 346
  • 663
  • The code in the example only works for the program that is being called in the example (salv_demo_table_simple) you will need to find out the type of the table used for the ALV in the program you are calling (RM07MLBS) and then replace `alv_t_t2` with that type. I suspect you can do this dynamically with the data returned from `GET_DATA_REF( )`. – Gert Beukema Oct 15 '18 at 23:14
  • @GertBeukema can you convert your comment into an answer so that I can vote for it? – Sandra Rossi Oct 16 '18 at 06:34
  • @GertBeukema is there a way to do this generic? At runtime I don't know which report gets called. The code in the question is just an example. – guettli Oct 16 '18 at 06:44
  • @guettli if you don't know which report then the type will be dynamic, so why do you want to dereference it to a static type? If you want all [generic](https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenbuilt_in_types_generic.htm), then use Run Time Type Services + type your field symbols with `TYPE ANY`, `TYPE ANY TABLE` + use `ASSIGN` (including `ASSIGN COMPONENT ... OF STRUCTURE ...`) What is the final goal, what do you want to do with this data object? – Sandra Rossi Oct 16 '18 at 08:01
  • @SandraRossi My overall goal is to get the report in XML (or json) format. – guettli Oct 16 '18 at 08:35
  • Can you try that? `FIELD-SYMBOLS TYPE ANY. ... ASSIGN lo_data->* to . CALL TRANSFORMATION ID SOURCE data = RESULT XML data(xstring).` – Sandra Rossi Oct 16 '18 at 08:43

2 Answers2

2

With the help of the answer of user mkysoft, this is the working solution, which exports the data in json format:

FUNCTION /Z_FOO/CALL_REPORT_XML.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  EXPORTING
*"     VALUE(EV_RESULT_JSON) TYPE  STRING
*"----------------------------------------------------------------------

DATA: lo_data        TYPE REF TO data.

" Let know the model
cl_salv_bs_runtime_info=>set(
 EXPORTING
   display  = abap_false
   metadata = abap_false
   data     = abap_true
).


SUBMIT ('RM07MLBS')
   WITH WERKS = '0557'
  AND RETURN.


" get data from SALV model
cl_salv_bs_runtime_info=>get_data_ref(
      IMPORTING
        r_data = lo_data
).

field-SYMBOLS <lv_data> type any table.
ASSIGN lo_data->* TO <lv_data>.
ev_result_json = /ui2/cl_json=>serialize( data = <lv_data> pretty_name = /ui2/cl_json=>pretty_mode-low_case ).

cl_salv_bs_runtime_info=>clear_all( ).

ENDFUNCTION.

This helped me to get it done: https://blogs.sap.com/2011/07/07/gain-programmatic-access-to-data-of-sapgui-alv-reports/

guettli
  • 25,042
  • 81
  • 346
  • 663
1

I added dynamic table, line and component to your code for creating working example.

REPORT zmky_catch_report.

DATA: lo_data        TYPE REF TO data,
      lr_structdescr TYPE REF TO cl_abap_structdescr,
      lr_tabledescr  TYPE REF TO cl_abap_tabledescr,
      ls_component   TYPE abap_compdescr .
FIELD-SYMBOLS: <fs_table> TYPE table,
               <fs_line>  TYPE any,
               <fs_field> TYPE any.

" Let know the model
cl_salv_bs_runtime_info=>set(
 EXPORTING
   display  = abap_false
   metadata = abap_false
   data     = abap_true
).


SUBMIT ('RM07MLBS')
  AND RETURN.

TRY.
    " get data from SALV model
    cl_salv_bs_runtime_info=>get_data_ref(
          IMPORTING
            r_data = lo_data
    ).
    lr_tabledescr ?= cl_abap_tabledescr=>describe_by_data_ref( lo_data ).
    lr_structdescr ?= lr_tabledescr->get_table_line_type( ).

* Table header
    WRITE 'ROWNUM     '.
    LOOP AT lr_structdescr->components INTO ls_component.
      WRITE ls_component-name.
    ENDLOOP.
    ULINE.

* Lines
    ASSIGN lo_data->* TO <fs_table>.
    LOOP AT <fs_table> ASSIGNING <fs_line>.
      WRITE sy-tabix.
      LOOP AT lr_structdescr->components INTO ls_component.
        ASSIGN COMPONENT ls_component-name OF STRUCTURE <fs_line> TO <fs_field>.
        WRITE <fs_field>.
      ENDLOOP.
      WRITE /.
    ENDLOOP.

  CATCH cx_salv_bs_sc_runtime_info.
ENDTRY.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
mkysoft
  • 5,392
  • 1
  • 21
  • 30
  • I get an error in this line `WRITE .`: "OBJECTS_NOT_CHARCONV" – guettli Oct 16 '18 at 09:37
  • Is there a way to use this to serialize the result? `/ui2/cl_json=>serialize( data = pretty_name = /ui2/cl_json=>pretty_mode-low_case ).` – guettli Oct 16 '18 at 09:40
  • I found a solution now and created a new answer. The second answer is mostly your idea. I hope this is ok for you. I could delete my question, if you think this is more appropriate. – guettli Oct 16 '18 at 11:30
  • @guettli some type cannot convert to char automatically. You can check field type with ls_component-type_kind before to write. – mkysoft Oct 16 '18 at 11:34