fareez.info

Running a Report Program and Getting Its Result Programmatically

There are situations when you need to run a report program and get its result list programmatically and display or process as you need. This article helps you to achieve that.

At first, we are going to create a simple report with a selection screen. The selection screen will be having a single input end_val which gets an integer. Once executed the numbers are generated from 1 upto the end_val as the result.

ZTEST_REPORT

report ztest_report.

parameters:
  end_val type n length 10 default 10.

data:
      i type i value 0.

write:/5 'Hello World'.
write:/10 'This is test report'.

do end_val times.
  i = i + 1.
  write:/12 i.
enddo.

Now we are going to create a report which runs this report program and gets the result list in an internal table and writes it again on its output using SUBMIT command and LIST_FROM_MEMORY function module.

SUBMIT command has a vareity of options of which we are going to use the following structure

SUBMIT <report_name>
  WITH SELECTION-TABLE <rspar_tab>
  EXPORTING LIST TO MEMORY
  AND RETURN.

EXPORTING LIST TO MEMORY phrase makes sure once the report is executed the result is kept in a buffer which can be obtained by using the function module LIST_TO_MEMORY.

Now the selection screen data has to be passed to the SUBMIT command through a table of structure RSPARAMS.

FieldPurpose
SELNAMEName of the selection screen parameter
KINDPass value ‘P’ if parameter,‘S’ if select options
SIGN‘I’ to include, ‘E’ to exclude (for select options only)
OPTIONSelect option values such as ‘EQ’, ‘BT’, etc.
LOWSelection Value (for parameter, pass the value here)
HIGHSelection Value high

RSPARAMS has 45 character length for LOW and HIGH values. If you need longer character length then use RSPARAMSL_255.

LIST_FROM_MEMORY function module returns the output list in a table of structure ABAPLIST. This can be passed to WRITE_LIST function module which will print the result report of the called program in the output of the current program.

ZSUBMIT_N_RET

report  zsubmit_n_ret.

data:
      t_list type standard table of abaplist,
      t_params type standard table of rsparams,
      s_params type rsparams.

  s_params-selname = 'END_VAL'.
  s_params-kind = 'P'.
  s_params-low = 100.
  append s_params to t_params.

  submit ztest_report
    with selection-table t_params
    exporting list to memory
    and return.

  call function 'LIST_FROM_MEMORY'
    tables
      listobject       = t_list
    exceptions
     not_found        = 1
     others           = 2.

  if sy-subrc <> 0.
    message 'Unable to get list from memory'
      type 'E'.
  endif.

  call function 'WRITE_LIST'
*   EXPORTING
*     WRITE_ONLY       = 'X'
    tables
      listobject       = t_list
   EXCEPTIONS
     EMPTY_LIST       = 1
     OTHERS           = 2
            .
  if sy-subrc <> 0.
    message 'Unable to write list'
      type 'E'.
  endif.
comments powered by Disqus