Sending the Multiple ALVs as PDF Attachment through Email SAP ABAP


Brief Requirement Overview:  

In this document, an email, with the PDF attachment of 3 ALV outputs, will be send to the Email addresses of multiple persons or single person as required. The principle is to print ALVs to spool directly, convert spool to PDF, and send the PDF to email. It can be used in background as well as in foreground. The program must be executed in background to generate the spool request.

Scenario:

The program will display 3 ALV grids using Custom containers in foreground and will use FM REUSE_ALV_BLOCK_LIST_APPEND to generate spool. When the program is executed in foreground, the three ALV grids will be displayed and an email with PDF attachment of the ALV outputs will be send .When the program is executed in background, spool request will be generated containing the 3 ALV outputs and an email with PDF attachment of the ALV outputs will be send.

Challenge:

The challenge in this scenario is, the 3 ALV grids built using OOPS concept cannot be sent to Spool directly in foreground. For sending the 3 ALV grids to spool, FM REUSE_ALV_BLOCK_LIST_APPEND has been used. The foreground mode uses both the normal ALV grid display methods of OOPS to display the three ALVs in foreground and then uses REUSE_ALV_BLOCK_LIST_APPEND FM to send the report to spool as list. The background mode uses only REUSE_ALV_BLOCK_LIST_APPEND to send the report to spool.

Step1:    Creating screen for foreground display

Go to Screen painter Transaction Code SE51.

Create a screen with no 100.

Provide description for the screen and click on the Layout Button.

Place 3 Custom container UI elements and give names as ‘G_CONTAINER1’ , ‘G_CONTAINER2’ and ‘G_CONTAINER3’ .The screen 100 will have 3 custom containers as shown below. Activate the Object.  

Step 2: Flow Logic

Go to the Flow Logic Tab to write coding for PBO & PAI.

Step3:  ABAP Editor

Create a Z program with the code as below:

The three internal tables for 3 ALVs are: I_ORDERS1, I_ORDERS2 and I_INVSTATUS.

The three field catalogs are built and the field catalog names are: I_FIELDCATALOG1, I_FIELDCATALOG2 and I_FIELDCATALOG3.

*&———————————————————————*
*&      Module STATUS_0100 OUTPUT
*&———————————————————————*
*       text
*———————————————————————-*
MODULE status_0100 OUTPUT.

* PF status of the screen
  PERFORM sub_pf_status.
*  Set the title of report
  SET TITLEBAR ‘TTL’.

* Display ALV Data
  PERFORM sub_display_firstalv  USING  i_fieldcatalog1
                                       i_orders1.

  PERFORM sub_display_secondalv USING  i_fieldcatalog2
                                       i_orders2.

  PERFORM sub_display_thirdalv  USING  i_fieldcatalog3
                                       i_invstatus.
* Send email to customers
  PERFORM sub_send_mail.

*    Refresh the first display table
  CALL METHOD g_grid1->refresh_table_display
    EXCEPTIONS
      finished = 1
      OTHERS   = 2.

*    Refresh the second display table
  CALL METHOD g_grid2->refresh_table_display
    EXCEPTIONS
      finished = 1
      OTHERS   = 2.

*    Refresh the third display table
  CALL METHOD g_grid3->refresh_table_display
    EXCEPTIONS
      finished = 1
      OTHERS   = 2.

ENDMODULE.                 ” STATUS_0100  OUTPUT

Subroutine to set the PF Status of the report

*&———————————————————————*
*&      Form sub_pf_status
*&———————————————————————*
*       text
*———————————————————————-*
FORM sub_pf_status.

* Local data declaration
  DATA: lt_excl TYPE ty_t_excl.

*Set PF status
  SET PF-STATUS ‘ZSTATUS_0100’ EXCLUDING lt_excl.
ENDFORM.                    “SUB_PF_STATUS

Subroutine to Display First ALV


*&———————————————————————*
*&      Form  SUB_DISPLAY_FIRSTALV
*&———————————————————————*
*       text
*———————————————————————-*
*      –>P_I_FIELDCATALOG1  text
*      –>P_I_ORDERS1  text
*———————————————————————-*
FORM sub_display_firstalv  USING    fp_i_fieldcatalog1 TYPE lvc_t_fcat
                                    fp_i_orders1     TYPE ty_t_orders1.

  DATA:      lx_print TYPE lvc_s_prnt.

* Local data declaration
  DATA: li_layout TYPE lvc_s_layo.

* Layout for ALV
  PERFORM sub_prepare_layout USING c_x
                                   text-011
                                   c_x
                                   c_cellstyle
                          CHANGING li_layout.
* Use Flush
  CALL METHOD cl_gui_cfw=>flush.

  IF g_custom_container1 IS INITIAL.       “To ensure that object is created only once

    CREATE OBJECT g_custom_container1
      EXPORTING
        container_name = ‘G_CONTAINER1’.

* Splitting the container
    CREATE OBJECT g_split
      EXPORTING
        parent        = g_custom_container1
        sash_position = 50 “Position of Splitter Bar (in Percent)
        with_border   = 0.”With Border = 1 Without Border = 0

* Placing the containers in the splitter
    g_top_container = g_split->top_left_container.
    g_bottom_container = g_split->bottom_right_container.

*     Create an instance of ALV control
    CREATE OBJECT g_grid1
      EXPORTING
        i_parent = g_bottom_container.


*   Creating the document
    CREATE OBJECT g_document
      EXPORTING
        style = ‘ALV_GRID’.

*Top of page
    PERFORM sub_top_of_page.

    CALL METHOD g_grid1->set_table_for_first_display
      EXPORTING
        it_toolbar_excluding          = i_exclude
        is_layout                     = li_layout
        is_print                      = lx_print
      CHANGING
        it_outtab                     = fp_i_orders1
        it_fieldcatalog               = fp_i_fieldcatalog1
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ENDIF.

ENDFORM.                    “SUB_DISPLAY_FIRSTALV

Subroutine to Display Second ALV

*&———————————————————————*
*&      Form  SUB_DISPLAY_SECONDALV
*&———————————————————————*
*       text
*———————————————————————-*
*      –>P_I_FIELDCATALOG2  text
*      –>P_I_ORDERS2  text
*———————————————————————-*
FORM sub_display_secondalv  USING   fp_i_fieldcatalog2 TYPE lvc_t_fcat
                                    fp_i_orders2     TYPE ty_t_orders2.

* Local data declaration
  DATA: li_layout TYPE lvc_s_layo,
        lx_print TYPE lvc_s_prnt.


* Layout for ALV
  PERFORM sub_prepare_layout USING c_x
                                   text-012
                                   c_x
                                   c_cellstyle
                          CHANGING li_layout.
*  Use Flush
  CALL METHOD cl_gui_cfw=>flush.

  IF g_custom_container2 IS INITIAL.       “To ensure that object is created only once

    CREATE OBJECT g_custom_container2
      EXPORTING
        container_name = ‘G_CONTAINER2’.

*     Create an instance of ALV control
    CREATE OBJECT g_grid2
      EXPORTING
        i_parent = g_custom_container2.


    CALL METHOD g_grid2->set_table_for_first_display
      EXPORTING
        it_toolbar_excluding          = i_exclude
        is_layout                     = li_layout
        is_print                      = lx_print
      CHANGING
        it_outtab                     = fp_i_orders2
        it_fieldcatalog               = fp_i_fieldcatalog2
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ENDIF.

ENDFORM.                    “SUB_DISPLAY_SECONDALV

Subroutine to Display Third ALV

*&———————————————————————*
*&      Form  SUB_DISPLAY_THIRDALV
*&———————————————————————*
*       text
*———————————————————————-*
*      –>P_I_FIELDCATALOG3  text
*      –>P_I_INVSTATUS  text
*———————————————————————-*
FORM sub_display_thirdalv  USING    fp_i_fieldcatalog3 TYPE lvc_t_fcat
                                    fp_i_invstatus TYPE ty_t_invstatus.

* Local data declaration
  DATA: li_layout TYPE lvc_s_layo,
        lx_print TYPE lvc_s_prnt.


* Layout for ALV
  PERFORM sub_prepare_layout USING c_x
                                   text-013
                                   c_x
                                   c_cellstyle
                          CHANGING li_layout.
*  Use Flush
  CALL METHOD cl_gui_cfw=>flush.

  IF g_custom_container3 IS INITIAL.       “To ensure that object is created only once

    CREATE OBJECT g_custom_container3
      EXPORTING
        container_name = ‘G_CONTAINER3’.

*     Create an instance of ALV control
    CREATE OBJECT g_grid3
      EXPORTING
        i_parent = g_custom_container3.


    CALL METHOD g_grid3->set_table_for_first_display
      EXPORTING
        it_toolbar_excluding          = i_exclude
        is_layout                     = li_layout
        is_print                      = lx_print
      CHANGING
        it_outtab                     = fp_i_invstatus
        it_fieldcatalog               = fp_i_fieldcatalog3
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ENDIF.
ENDFORM.                    “SUB_DISPLAY_THIRDALV

Subroutine for ALV layout

*&———————————————————————*
*&      Form  SUB_PREPARE_LAYOUT
*&———————————————————————*
*       text
*———————————————————————-*
*      <–P_LI_LAYOUT  text
*———————————————————————-*
FORM sub_prepare_layout  USING fp_c_x         TYPE xfeld
                               fp_title       TYPE lvc_title
                               fp_smalltitle  TYPE xfeld
                               fp_stylename   TYPE lvc_fname
                      CHANGING fp_li_layout   TYPE lvc_s_layo.

  fp_li_layout-zebra      = fp_c_x.
  fp_li_layout-grid_title = fp_title.
  fp_li_layout-smalltitle = fp_smalltitle.
  fp_li_layout-stylefname = fp_stylename.

ENDFORM.                    “SUB_PREPARE_LAYOUT

Subroutine for Report header, which will be created dynamically based on the fields selected by the User for a User selected report layout. 

*&———————————————————————*
*&      Form SUB_TOP_OF_PAGE
*&———————————————————————*
*       text
*———————————————————————-*
* –> p1        text
* <– p2        text
*———————————————————————-*
FORM sub_top_of_page.

* Local data Declaration
  DATA: l_text TYPE sdydo_text_element,
        l_datel (10) TYPE c,
        l_dateh (10) TYPE c.

* Calling the methods for dynamic text
  CALL METHOD g_document->add_text
    EXPORTING
      text         = text-014
      sap_emphasis = cl_dd_area=>strong ” For bold
      sap_fontsize = cl_dd_area=>extra_large.

* Adding Line
  CALL METHOD g_document->new_line.
* Adding Line
  CALL METHOD g_document->new_line.

*   Sold-to customer
  IF s_kunnr-high IS NOT INITIAL.
    CONCATENATE  s_kunnr-low ‘to’ s_kunnr-high INTO l_text SEPARATED BY ‘ ‘.
  ELSE.
    l_text = s_kunnr-low.
  ENDIF.

  CALL METHOD g_document->add_text
    EXPORTING
      text         = text-015
      sap_emphasis = cl_dd_area=>strong. “For bold.

  CALL METHOD g_document->add_gap
    EXPORTING
      width = ’15’.

  CALL METHOD g_document->add_text
    EXPORTING
      text = l_text.
  CALL METHOD g_document->new_line.


  CLEAR: l_text.
*   Change date
*   Converting date format
  CALL FUNCTION ‘CONVERT_DATE_TO_EXTERNAL’
    EXPORTING
      date_internal = s_erdat-low
    IMPORTING
      date_external = l_datel.* Converting date format
  CALL FUNCTION ‘CONVERT_DATE_TO_EXTERNAL’
    EXPORTING
      date_internal = s_erdat-high
    IMPORTING
      date_external = l_dateh.

  IF s_erdat-high IS NOT INITIAL.
    CONCATENATE l_datel ‘to’ l_dateh INTO l_text SEPARATED BY ‘ ‘.
  ELSE.
    l_text = l_datel.
  ENDIF.

  CALL METHOD g_document->add_text
    EXPORTING
      text         = text-016
      sap_emphasis = cl_dd_area=>strong. “For bold.

  CALL METHOD g_document->add_gap
    EXPORTING
      width = ’24’.

  CALL METHOD g_document->add_text
    EXPORTING
      text = l_text.
  CALL METHOD g_document->new_line.

  CLEAR: l_text.
  IF NOT s_vkorg IS INITIAL.
*   Sales Organization
    IF s_vkorg-high IS NOT INITIAL.
      CONCATENATE  s_vkorg-low ‘to’ s_vkorg-high INTO l_text SEPARATED BY ‘ ‘.
    ELSE.
      l_text = s_vkorg-low.
    ENDIF.

    CALL METHOD g_document->add_text
      EXPORTING
        text         = text-017
        sap_emphasis = cl_dd_area=>strong. “For bold.

    CALL METHOD g_document->add_gap
      EXPORTING
        width = ’12’.

    CALL METHOD g_document->add_text
      EXPORTING
        text = l_text.
    CALL METHOD g_document->new_line.
  ENDIF.

  CLEAR : l_text.
  IF NOT s_bsark IS INITIAL.
*   PO Type
    IF s_bsark-high IS NOT INITIAL.
      CONCATENATE  s_bsark-low ‘to’ s_bsark-high INTO l_text SEPARATED BY ‘ ‘.
    ELSE.
      l_text = s_bsark-low.
    ENDIF.

    CALL METHOD g_document->add_text
      EXPORTING
        text         = text-018
        sap_emphasis = cl_dd_area=>strong. “For bold.

    CALL METHOD g_document->add_gap
      EXPORTING
        width = ’32’.

    CALL METHOD g_document->add_text
      EXPORTING
        text = l_text.
    CALL METHOD g_document->new_line.
  ENDIF.

* Display the data
  CALL METHOD g_document->display_document
    EXPORTING
      parent = g_top_container.
* Calling the method of ALV to process top of page
  CALL METHOD g_grid1->list_processing_events
    EXPORTING
      i_event_name = text-019
      i_dyndoc_id  = g_document.

ENDFORM.                    “SUB_TOP_OF_PAGE


Leave a Reply

Your email address will not be published. Required fields are marked *