How to Steal Data from Standard SAP Reports

Sometimes, we get an ABAP spec where we need to steal the data from a standard SAP report. Or; the analyst wants to add new fields to a standard report and we decide that it’s better to steal the data + add our own fields in a Z-report instead of repairing the system.

If it is a MVC report, we can simply call the corresponding class and get the data we need. But more often than not, we don’t find such a clean class behind.

“Exporting list to memory” comes to mind; but it requires a lot of formatting and default user variants doesn’t make our lives easier.

Here are some possible solutions for such cases.

Solution 1: SALV Runtime Info

This is the easiest approach if your host application is using ALV. In the sample below, we want to steal data from ZBCP_JIRA_SYCMP.

FIELD-SYMBOLS <stolen_itab> TYPE STANDARD TABLE.

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

SUBMIT zbcp_jira_sycmp AND RETURN.

TRY.
    cl_salv_bs_runtime_info=>get_data_ref(
      IMPORTING r_data = DATA(stolen_itab) ).

    ASSIGN stolen_itab->* TO <stolen_itab>.
    DATA(meta) = cl_salv_bs_runtime_info=>get_metadata( ).
  CATCH cx_root ##no_handler.
ENDTRY.

cl_salv_bs_runtime_info=>clear_all( ).

When we run this program, that’s what we get in <stolen_itab> and meta.

As you see, we even get the layout and field catalog. Kudos to Berrin Ulus for pointing out this solution!

Solution 2: Enhancement

This solution can be used in case your target report doesn’t use ALV. If you are dealing with a program using Write, Tree display, etc; keep reading.

Step 1: Enhance the standard report

Let’s assume that the standard report looks like this.

REPORT ztest_std.

DATA: go_salv   TYPE REF TO cl_salv_table,
      gt_output TYPE t001w_tab.

START-OF-SELECTION.
  PERFORM: read_data,
           display_alv.

FORM read_data.
  SELECT * FROM t001w INTO TABLE @gt_output.
ENDFORM.

FORM display_alv.
  cl_salv_table=>factory(
    IMPORTING r_salv_table = go_salv
    CHANGING  t_table      = gt_output ).

  go_salv->display( ).
ENDFORM.

This oversimplified report would produce an output like this.

We enhance this standard report as the example below:

REPORT ztest_std.

DATA: go_salv   TYPE REF TO cl_salv_table,
      gt_output TYPE t001w_tab.

START-OF-SELECTION.
  PERFORM: read_data,
           display_alv.

FORM read_data.
  SELECT * FROM t001w INTO TABLE @gt_output.
ENDFORM.

FORM display_alv.
ENHANCEMENT 1  ZTEST_STD.    "active version
"$"$\SE:(4) Form DISPLAY_ALV, Start, Enhancement ZTEST_STD, Start
  IF sy-tcode EQ 'Z001'.
    EXPORT output = gt_output TO MEMORY ID 'ZMEMORY'.
    RETURN.
  ENDIF.
"$"$\SE:(5) Form DISPLAY_ALV, Start, Enhancement ZTEST_STD, End
ENDENHANCEMENT.

  cl_salv_table=>factory(
    IMPORTING r_salv_table = go_salv
    CHANGING  t_table      = gt_output ).

  go_salv->display( ).
ENDFORM.

Note that the enhancement makes the program export the list & quit instead of displaying the ALV grid – in case the TCode is Z001.

Step 2: Write your custom report

Now it’s time to write our custom report, which will be run under the TCode Z001.

REPORT ztest_custom.

DATA: go_salv        TYPE REF TO cl_salv_table,
      gt_custom_itab TYPE zbctt_daha_list,
      gt_stolen_data TYPE t001w_tab.

START-OF-SELECTION.
  PERFORM: steal_standard_data,
           build_custom_itab,
           display_alv.

FORM steal_standard_data.
  SUBMIT ztest_std AND RETURN. " Assuming SY-TCODE is Z001
  IMPORT output = gt_stolen_data FROM MEMORY ID 'ZMEMORY'.
ENDFORM.

FORM build_custom_itab.
  ##TODO. " Build gt_custom_itab from gt_stolen_data
ENDFORM.

FORM display_alv.
  cl_salv_table=>factory(
    IMPORTING r_salv_table = go_salv
    CHANGING  t_table      = gt_custom_itab ).

  go_salv->display( ).
ENDFORM.

Simple and effective, right?

Advertisement

Posted

in

,

by

Tags:

Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s