Alternative to SAVE_LIST

By | February 23, 2012 | Tricks | 6,669 | 5

Demonstrates the Alternative to FM SAVE_LIST.

Forum post Alternative for function SAVE_LIST got to me thinking for other ways to convert the ABAP List to HTML.

Simplest way is to Submit the program which writes the list and get it from the Memory. But the drawback with this approach is you need to create another program as a wrapper program which Submit, get the list, convert it and send as attachment. But if it is not feasible, you may want to use below mentioned technique.

This technique uses:

  1. Write the List in a New Spool: Use NEW-PAGE PRINT ON and PRINT OFF
  2. Convert the Spool to List Object: SUBMIT REPORT RSPOLIST with list to memory. Use the FM LIST_FROM_MEMORY to get the list into List Object
  3. Convert the List Object to Email format: use FM WWW_HTML_FROM_LISTOBJECT

Code snippet for alternative to SAVE_LIST:

Alternative to SAVE_LIST

 
DATA: lt_html TYPE STANDARD TABLE OF w3html.
 
DATA lt_scarr TYPE TABLE OF scarr.
DATA carr TYPE scarr.
DATA: print_parameters TYPE pri_params,
      valid_flag       TYPE c LENGTH 1.
 
* print parameters
CALL FUNCTION 'GET_PRINT_PARAMETERS'
  IMPORTING
    out_parameters       = print_parameters
    valid                = valid_flag
  EXCEPTIONS
    invalid_print_params = 2
    OTHERS               = 4.
 
IF valid_flag = 'X' AND sy-subrc = 0.
 
  SELECT * FROM scarr INTO TABLE lt_scarr.
 
* This will generate the Spool
  NEW-PAGE PRINT ON PARAMETERS print_parameters
                    NO DIALOG.
 
  FORMAT COLOR = 1.
  ULINE AT /1(46).
  WRITE: / sy-vline,
         'CARRID',   10 sy-vline,
         'CARRNAME', 35 sy-vline,
         'CURRCODE', 46 sy-vline.
 
  FORMAT COLOR = 2.
  DO 5 TIMES.
    ULINE AT /1(46).
    LOOP AT lt_scarr INTO carr.
      WRITE: / sy-vline,
             carr-carrid,   10 sy-vline,
             carr-carrname, 35 sy-vline,
             carr-currcode, 46 sy-vline.
    ENDLOOP.
  ENDDO.
 
  ULINE AT /1(46).
  NEW-LINE.
  NEW-PAGE PRINT OFF.
 
  DATA: lv_spool TYPE tsp01-rqident.
  DATA: list_tab LIKE abaplist OCCURS 10.
 
* Get the List from the Spool
  lv_spool = sy-spono.
  SUBMIT rspolist EXPORTING LIST TO MEMORY AND RETURN
                  WITH rqident = lv_spool
                  WITH pages = 'X'.
 
* List from Memory
  CALL FUNCTION 'LIST_FROM_MEMORY'
    TABLES
      listobject = list_tab
    EXCEPTIONS
      not_found  = 1
      OTHERS     = 2.
 
* convert to HTML
  CALL FUNCTION 'WWW_HTML_FROM_LISTOBJECT'
    TABLES
      html       = lt_html
      listobject = list_tab.
 
* Email sending with HTML attachment 
  DATA send_request   TYPE REF TO cl_bcs.
  DATA document       TYPE REF TO cl_document_bcs.
  DATA sender         TYPE REF TO cl_sapuser_bcs.
  DATA recipient      TYPE REF TO if_recipient_bcs.
  DATA bcs_exception  TYPE REF TO cx_bcs.
  data text      type bcsy_text.
 
  TRY.
 
      send_request = cl_bcs=>create_persistent( ).
 
*     create document from internal table with text
      APPEND 'Hello world!' TO text.
      document = cl_document_bcs=>create_document(
                      i_type    = 'RAW'
                      i_text    = text
                      i_length  = '12'
                      i_subject = 'Test - ABAP List to HTML' ).
 
*     attachment
      document->add_attachment(
         i_attachment_type    = 'HTM'                       "#EC NOTEXT
         i_attachment_subject = 'ListToHTML'                "#EC NOTEXT
         i_att_content_text   = lt_html ).
 
*     add document to send request
      send_request->set_document( document ).
      sender = cl_sapuser_bcs=>create( sy-uname ).
      send_request->set_sender( i_sender = sender ).
      recipient = cl_cam_address_bcs=>create_internet_address(
                                        'zevolving@gmail.com' ).
 
      send_request->add_recipient(
          i_recipient = recipient
          i_express   = 'X' ).
 
      send_request->send(
        EXPORTING
          i_with_error_screen = 'X' ).
 
      COMMIT WORK.
    CATCH cx_bcs INTO bcs_exception.
  ENDTRY.
 
ELSE.
  WRITE: 'Some error occured'.
ENDIF.
 
WRITE: 'done'.
 

Long Live List…!

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

5 Comments

  • Clemens Li

    Thank you Naimesh,

    this is exactly what I really wanted.

    I think I will put the NEW-PAGE PRINT ON in a static method ‘PREPARE_LIST’ wrapped in condition IF SY-BATCH IS NOT INITIAL. This makes determination of print prarameters obsolete – a batch job always knows its print parameters.

    A second static method GET_LIST will be used to retrieve the LIST object. In online mode (SY-BATCH IS INITIAL), function SAVE_LIST is OK, in BATCH mode it will do the SUBMIT rspolist and CALL FUNCTION ‘LIST_FROM_MEMORY’.

    Adding those two methods to any existing program will enable us to send the list output as mail.

    Note: Of course, ‘classic’ coders may replace the methods by FUNCTION modules; I prefer the method approach just because it looks better.

    START-OF-SELECTION.

    ZCL_LIST=>PREPARE_LIST( ).

    END-OF-SELECTION.

    listobject = ZCL_LIST=>GET_LIST( ).

    looks smart, doesn’t it?

    Thanks a lot!

    Regards

    Clemens

  • Hello Clemens,

    I’m glad that it works for you.

    Yes, it definitely looks great using methods. Thanks for sharing those with us.

    Regards,
    Naimesh Patel

  • Clemens Li

    Hello Naimesh,

    oops, I’m not yet finished: If the user has not maintained a printer in his user settings, a background job using the above technique is canceled. The Message “Printer name ‘ ‘ not defined”(PT 001) is issued.

    As soon as I find a solution, I will come back.

    Kind regards

    Clemens

  • Clemens Li

    Hello Naimesh and all other readers,

    I hope I found the solution:

    At start of selection, I call the static method PROGRAM_LISTOBJECT_PREPARE:
    [sorry don’t know how to format code here]

    Zevolving ABAP Syntax Highlighter

     
    METHOD program_listobject_prepare.
      DATA:
        ls_pri_params TYPE pri_params,
        lv_valid      TYPE flag.
      CHECK sy-batch IS NOT INITIAL.
      CALL FUNCTION 'GET_PRINT_PARAMETERS'
        EXPORTING
          no_dialog              = abap_true
          mode                   = 'CURRENT'
        IMPORTING
          out_parameters         = ls_pri_params
          valid                  = lv_valid
        EXCEPTIONS
          archive_info_not_found = 1
          invalid_print_params   = 2
          invalid_archive_params = 3
          OTHERS                 = 4.
      IF sy-subrc = 0 AND lv_valid IS NOT INITIAL.
        NEW-PAGE PRINT ON PARAMETERS ls_pri_params NO DIALOG..
      ENDIF.
    ENDMETHOD.
     

    At END-OF_SELECTION (or wherever you want to send the programs list output as nicely-formatted HTML), I get the List content using static method PROGRAM_LISTOBJECT_GET.
    The grogram gets the list
    lt_list = cl_tool_static=>program_listobject_get( ).

    Zevolving ABAP Syntax Highlighter

     
    METHOD program_listobject_get.
      data:
        lv_spono type sy-spono.
      IF sy-batch IS NOT INITIAL.
    * Get the List from the Spool
        NEW-PAGE PRINT OFF.
        lv_spono = sy-spono.
        SUBMIT rspolist EXPORTING LIST TO MEMORY AND RETURN
                        WITH rqident = lv_spono
                        WITH pages = 'X'.
    * List from Memory
        CALL FUNCTION 'LIST_FROM_MEMORY'
          TABLES
            listobject = rt_list
          EXCEPTIONS
            OTHERS     = 0.
      ELSE.
    * Current list index 1
        CALL FUNCTION 'SAVE_LIST'
          TABLES
            listobject = rt_list
          EXCEPTIONS
            OTHERS     = 0.
      ENDIF.
    ENDMETHOD.
     

    The rest remains the same (see above).

    Hope, now we got it.

    Regards,

    Clemens

  • Naimesh Patel

    Hello Clemens,

    Thanks for sharing the code which can work in Background Processing.

    Btw, I have corrected the code formatting. This is kind of yet beta release of the syntax highlighter for comments. I’ll soon add instructions on this.

    Thanks,
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.