Read the spool data with exact fieldcatalog.

We, ABAPers, often get this requirement that we need to extract the data out of spool and use it for some purpose like table display, send to excel, send as an email content. You get the jist, right? Then what? We go ahead and create a length according the data we see in the spool and pray that data being retieved doesn't exceed the length limit.






We can easily read the spool using different Function Modules and Spool Id. But we always get stuck when we need what kind of data is being retrieved? Why? Because field catalog is missing and what is returned is just plain text file.







What if I told you that this can be knocked over?

<<ENTERS>>      ALV Runtime Services


Have a look at a below code:

"Metadata -> To extract the fieldcatalog, layout etc of the ALV.
"Data -> Data published by ALV

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

Now you have set the parameters, its time to run the report in the background

SUBMIT (p_name) USING SELECTION-SET p_var AND RETURN.

This statement will run your report in the background and still will generate a spool but we will not need it to get the things we want!

Now its time to retrieve the results.

TRY.
      cl_salv_bs_runtime_info=>get_data_ref( IMPORTING r_data = lr_data ).
      ASSIGN lr_data->* TO <fs_data>.
      IF <fs_data> IS ASSIGNED.
        TRY.
            cl_salv_table=>factory( IMPORTING
                                    r_salv_table   = lr_table
                                  CHANGING
                                    t_table        = <fs_data>  ).
            lt_fieldcat = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
                r_columns      = lr_table->get_columns( ) " ALV Filter
                r_aggregations = lr_table->get_aggregations( ) " ALV Aggregations
        ).
          CATCH cx_root.

        ENDTRY.

        ENDIF.
    CATCH cx_salv_bs_sc_runtime_info.
      
  ENDTRY.

  cl_salv_bs_runtime_info=>clear_all( ).

This would simply provide you the results returned during spool generation. Great, right?

But, how does it work?

SAP uses session memory to store this information (Only if requested). Once this, request comes by setting the parameters, whatever parameters are passed to ALV gets stored in the session and then extracted from the memory using standard methods.

" This standard part will trigger the request, that runtime info is requested.
export s_runtime_info from ls_runtime_info to memory id cl_salv_bs_runtime_info=>c_memid_info.

" This code will retrieve the results set by ALV.
import t_component to lt_component from memory id cl_salv_bs_runtime_info=>c_memid_data_def.

" Of course these all happens within standard SAP Classes but SAP Provides a wrapper class 'CL_SALV_BS_RUNTIME_INFO' for us to carry out operations for our needs.

Now, yeah, this has an additional advantage over the traditional methods to read spool but it comes with limitations.

Let's talk about the limitations: