Sending the Multiple ALVs as PDF Attachment through Email

...Previous

Step4:  Sending an Email with PDF attachment of the output.

The ALV output is first send to spool request, then the spool is converted to PDF and then the PDF attachment is sent to Email.

a)     Converting Spool to PDF

*&---------------------------------------------------------------------*
*&      Form  SUB_CONVERT_SPOOL_TO_PDF
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM sub_convert_spool_to_pdf CHANGING fp_size      TYPE i
                                      fp_i_mess_att TYPE ty_t_mess_att.

  TYPES: ty_t_pdf TYPE STANDARD TABLE OF tline.

  DATA: lv_buffer     TYPE string,
        lv_spool_nr   TYPE tsp01-rqident,
        lw_mess_att   TYPE solisti1,
        li_pdf_output TYPE ty_t_pdf,
        lw_pdf_output TYPE tline.

* Get the spool number
  MOVE sy-spono TO lv_spool_nr.
  CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
    EXPORTING
      src_spoolid            = lv_spool_nr "Spool Number
      no_dialog              = space
      dst_device             = 'LP01' "Printer Name
    IMPORTING
      pdf_bytecount          = fp_size "Output Size
    TABLES
      pdf                    = li_pdf_output "Spool data in PDF Format
    EXCEPTIONS
      err_no_abap_spooljob   = 1
      err_no_spooljob        = 2
      err_no_permission      = 3
      err_conv_not_possible  = 4
      err_bad_destdevice     = 5
      user_cancelled         = 6
      err_spoolerror         = 7
      err_temseerror         = 8
      err_btcjob_open_failed = 9.

  IF sy-subrc EQ 0.
    LOOP AT li_pdf_output INTO lw_pdf_output.
      TRANSLATE lw_pdf_output USING c_col1.
      CONCATENATE lv_buffer lw_pdf_output INTO lv_buffer.
      CLEAR :lw_pdf_output.
    ENDLOOP.
    TRANSLATE lv_buffer USING c_col2.
    DO.
      lw_mess_att = lv_buffer.
      APPEND lw_mess_att TO fp_i_mess_att.
      SHIFT lv_buffer LEFT BY c_255 PLACES.
      IF lv_buffer IS INITIAL.
        EXIT.
      ENDIF.
      CLEAR lw_mess_att.
    ENDDO.
  ENDIF.

ENDFORM.                    “SUB_CONVERT_SPOOL_TO_PDF

b) Sending PDF attachment to Email

The email addresses of the receivers are populated in table I_EMAIL which is passed in the below subroutine as FP_I_EMAIL.

*&---------------------------------------------------------------------*
*&      Form  SUB_SEND_PDF_TO_MAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM sub_send_pdf_to_mail USING  fp_size       TYPE i
                                 fp_i_mess_att TYPE ty_t_mess_att
                                 fp_i_email    TYPE ty_t_email.

  DATA: l_ref_bcs      TYPE REF TO cl_bcs,
        l_ref_document TYPE REF TO cl_document_bcs,
        lv_text        TYPE so_obj_des,
        lv_data        TYPE bcsy_text,
        lv_filesize    TYPE so_obj_len,
        lw_email       TYPE ty_email,
        l_recipient    TYPE REF TO if_recipient_bcs,
        l_ref_bcs_exception TYPE REF TO cx_bcs,
        sent_to_all    TYPE os_boolean.

  LOOP AT fp_i_email INTO lw_email.

    TRY.
* Create persistent send request
        l_ref_bcs = cl_bcs=>create_persistent ( ).
* Mail subject
        lv_text = text-021.
        APPEND text-021 TO lv_data.

* Create document
        l_ref_document = cl_document_bcs=>create_document (
        i_type = c_raw
        i_text = lv_data
        i_subject = lv_text).

* Create document reference
        lv_filesize = fp_size.
        CALL METHOD l_ref_document->add_attachment
          EXPORTING
            i_attachment_type    = c_pdf
            i_attachment_size    = lv_filesize
            i_attachment_subject = lv_text
            i_att_content_text   = fp_i_mess_att [].

* Set the document
        l_ref_bcs->set_document (l_ref_document).

* Get Recipient Object
        l_recipient = cl_cam_address_bcs=>create_internet_address (lw_email-smtp_addr).
*
* Add recipient with its respective attributes to send request
        CALL METHOD l_ref_bcs->add_recipient
          EXPORTING
            i_recipient = l_recipient.

* Set that you don't need a Return Status E-mail
        CALL METHOD l_ref_bcs->set_status_attributes
          EXPORTING
            i_requested_status = 'E'
            i_status_mail      = 'E'.

* Set send immediately flag
        l_ref_bcs->set_send_immediately( 'X' ).

* Send E-mail
        CALL METHOD l_ref_bcs->send (
        EXPORTING
          i_with_error_screen = 'X'
        RECEIVING
          result              = sent_to_all).
        IF sent_to_all = 'X'.
* E-mail sent successfully
          MESSAGE i028 WITH text-020.
        ENDIF.


      CATCH cx_bcs INTO l_ref_bcs_exception.
        IF l_ref_bcs_exception IS NOT INITIAL.
          CLEAR l_ref_bcs_exception.
        ENDIF.
    ENDTRY.
  ENDLOOP.
  COMMIT WORK.

ENDFORM.                    “SUB_SEND_PDF_TO_MAIL
 

Step 5: Report output in foreground

Step 6: Functionality for executing the report in Background.

When the report is executed in background, the single spool of three ALV grids will be created. For displaying the three ALV Grids in single spool, we will use the following FMs:

a)     REUSE_ALV_BLOCK_LIST_INIT

b)    REUSE_ALV_BLOCK_LIST_APPEND

c)     REUSE_ALV_BLOCK_LIST_DISPLAY

The three field catalogs are built for this and the field catalog names are: LI_FIELDCAT1, LI_FIELDCAT2 and LI_FIELDCAT3.

The header of the three ALVs in the spool is created as follows by using the below code:


  PERFORM sub_header_list CHANGING li_events_1
                                   li_events_2
                                   li_events_3.

*&---------------------------------------------------------------------*
*&      Form  SUB_HEADER_LIST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_LI_EVENTS_1  text
*      <--P_LI_EVENTS_2  text
*      <--P_LI_EVENTS_3  text
*----------------------------------------------------------------------*
FORM sub_header_list  CHANGING fp_li_events_1 TYPE slis_t_event
                               fp_li_events_2 TYPE slis_t_event
                               fp_li_events_3 TYPE slis_t_event.

  DATA: lw_events   TYPE slis_alv_event.

  lw_events-name = 'TOP_OF_PAGE'.
  lw_events-form = 'TOP_OF_PAGE_1'."Subroutine name
  APPEND lw_events TO fp_li_events_1.
  CLEAR  lw_events.
  lw_events-name = 'TOP_OF_PAGE'.
  lw_events-form = 'TOP_OF_PAGE_2'."Subroutine name
  APPEND lw_events TO fp_li_events_2.
  CLEAR  lw_events.
  lw_events-name = 'TOP_OF_PAGE'.
  lw_events-form = 'TOP_OF_PAGE_3'."Subroutine name
  APPEND lw_events TO fp_li_events_3.
  CLEAR  lw_events.

ENDFORM.                    “SUB_HEADER_LIST

Click here to continue...

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image