PDF per Mail/Fax verschicken

Aus SAP-Wiki
Wechseln zu: Navigation, Suche

Faxe wie E-Mails können per BCS (Business Communication Services) verschickt werden.

Das nötige Coding ist im Funktionsbaustein SD_PDF_SEND_DATA gekapselt.

Testbeispiel SD_INVOICE_PRINT01

  • Im Programm SD_INVOICE_PRINT01 wird der Funktionsbaustein SD_PDF_SEND_DATA aufgerufen zum Verschicken einer E-Mail oder eines Faxes
  • Hier wird eine PDF-Rechnung verschickt

SAP-Funktionsbaustein SD_PDF_SEND_DATA

FUNCTION sd_pdf_send_data.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     REFERENCE(IV_DEVICE) TYPE  OUTPUT_DEVICE
*"     REFERENCE(IV_EMAIL_SUBJECT) TYPE  SO_OBJ_DES OPTIONAL
*"     REFERENCE(IT_EMAIL_TEXT) TYPE  BCSY_TEXT OPTIONAL
*"     REFERENCE(IS_MAIN_DATA) TYPE  FPFORMOUTPUT
*"     REFERENCE(IV_LANGUAGE) TYPE  SYLANGU DEFAULT SY-LANGU
*"     REFERENCE(IS_ADDRESS) TYPE  SDPRT_ADDR_S
*"  EXPORTING
*"     VALUE(EV_SEND_TO_ALL) TYPE  OS_BOOLEAN
*"  EXCEPTIONS
*"      EXC_DOCUMENT
*"      EXC_SEND_REQUEST
*"      EXC_ADDRESS
*"----------------------------------------------------------------------

  DATA:   lo_bcs_document     TYPE REF TO cl_document_bcs,
          lo_send_request     TYPE REF TO cl_bcs,
          lo_recipient        TYPE REF TO if_recipient_bcs,
          lo_sender           TYPE REF TO if_sender_bcs,
          lo_exc_doc_bcs      TYPE REF TO cx_document_bcs,
          lo_exc_send_req_bcs TYPE REF TO cx_send_req_bcs,
          lo_exc_address_bcs  TYPE REF TO cx_address_bcs,
          lv_pdf_size         TYPE so_obj_len,
          lt_bcs_pdf          TYPE solix_tab.



*--- Call the business communication service
  TRY.
      lo_send_request = cl_bcs=>create_persistent( ).

      lv_pdf_size = XSTRLEN( is_main_data-pdf ).

      CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
          buffer     = is_main_data-pdf
        TABLES
          binary_tab = lt_bcs_pdf.

      CASE iv_device.

        WHEN gc_device-email.

          CALL METHOD cl_document_bcs=>create_document
            EXPORTING
              i_type        = 'TXT'
              i_subject     = iv_email_subject
*             I_LENGTH      =
              i_language    = iv_language
              i_text        = it_email_text
            RECEIVING
              result        = lo_bcs_document.


*--- Add the document as an attachment
          CALL METHOD lo_bcs_document->add_attachment
            EXPORTING
              i_attachment_type     = 'PDF'
              i_attachment_size     = lv_pdf_size
              i_attachment_subject  = iv_email_subject
              i_attachment_language = iv_language
              i_att_content_hex     = lt_bcs_pdf.


*--- Set the e-mail address of the recipient
          CALL METHOD cl_cam_address_bcs=>create_internet_address
            EXPORTING
              i_address_string = is_address-recip_email_addr
            RECEIVING
              result           = lo_recipient.

*--- Set the e-mail address of the sender
          IF NOT is_address-sender_email_addr IS INITIAL.
            CALL METHOD cl_cam_address_bcs=>create_internet_address
              EXPORTING
                i_address_string = is_address-sender_email_addr
              RECEIVING
                result           = lo_sender.

          ELSE.
            lo_sender = cl_sapuser_bcs=>create( sy-uname ).
          ENDIF.

        WHEN gc_device-fax.

          CALL METHOD cl_document_bcs=>create_document
            EXPORTING
              i_type     = 'PDF'
              i_subject  = iv_email_subject
              i_length   = lv_pdf_size
              i_language = iv_language
              i_hex      = lt_bcs_pdf
            RECEIVING
              result     = lo_bcs_document.

*--- Set the fax address of the recipient
          CALL METHOD cl_cam_address_bcs=>create_fax_address
            EXPORTING
              i_country = is_address-recip_fax_country
              i_number  = is_address-recip_fax_number
            RECEIVING
              result    = lo_recipient.

*--- Set the fax address of the sender
          IF NOT is_address-sender_fax_number IS INITIAL.
            CALL METHOD cl_cam_address_bcs=>create_fax_address
              EXPORTING
                i_country = is_address-sender_fax_country
                i_number  = is_address-sender_fax_number
              RECEIVING
                result    = lo_sender.
          ELSE.
            lo_sender = cl_sapuser_bcs=>create( sy-uname ).
          ENDIF.

      ENDCASE.

*--- Assign document to the send request
      CALL METHOD lo_send_request->set_document( lo_bcs_document ).

*--- Add the sender
      CALL METHOD lo_send_request->set_sender
        EXPORTING
          i_sender = lo_sender.

*--- Add the recipient
      CALL METHOD lo_send_request->add_recipient
        EXPORTING
          i_recipient = lo_recipient.

*--- Send the document
      CALL METHOD lo_send_request->send
        EXPORTING
          i_with_error_screen = gc_true
        RECEIVING
          result              = ev_send_to_all.

*--- Exception handling
    CATCH cx_document_bcs INTO lo_exc_doc_bcs.
      IF NOT  lo_exc_doc_bcs->msgid IS INITIAL
      AND NOT lo_exc_doc_bcs->msgty IS INITIAL
      AND NOT lo_exc_doc_bcs->msgno IS INITIAL.
        MESSAGE ID     lo_exc_doc_bcs->msgid
                TYPE   lo_exc_doc_bcs->msgty
                NUMBER lo_exc_doc_bcs->msgno
                WITH   lo_exc_doc_bcs->msgv1
                       lo_exc_doc_bcs->msgv2
                       lo_exc_doc_bcs->msgv3
                       lo_exc_doc_bcs->msgv4
                RAISING exc_document.
      ELSE.
        RAISE exc_document.
      ENDIF.

    CATCH cx_send_req_bcs INTO lo_exc_send_req_bcs.
      IF NOT  lo_exc_send_req_bcs->msgid IS INITIAL
      AND NOT lo_exc_send_req_bcs->msgty IS INITIAL
      AND NOT lo_exc_send_req_bcs->msgno IS INITIAL.
        MESSAGE ID     lo_exc_send_req_bcs->msgid
                TYPE   lo_exc_send_req_bcs->msgty
                NUMBER lo_exc_send_req_bcs->msgno
                WITH   lo_exc_send_req_bcs->msgv1
                       lo_exc_send_req_bcs->msgv2
                       lo_exc_send_req_bcs->msgv3
                       lo_exc_send_req_bcs->msgv4
                RAISING exc_send_request.
      ELSE.
        RAISE exc_send_request.
      ENDIF.
    CATCH cx_address_bcs INTO lo_exc_address_bcs.
      IF NOT  lo_exc_address_bcs->msgid IS INITIAL
      AND NOT lo_exc_address_bcs->msgty IS INITIAL
      AND NOT lo_exc_address_bcs->msgno IS INITIAL.
        MESSAGE ID     lo_exc_address_bcs->msgid
                TYPE   lo_exc_address_bcs->msgty
                NUMBER lo_exc_address_bcs->msgno
                WITH   lo_exc_address_bcs->msgv1
                       lo_exc_address_bcs->msgv2
                       lo_exc_address_bcs->msgv3
                       lo_exc_address_bcs->msgv4
                RAISING exc_address.
      ELSE.
        RAISE exc_address.
      ENDIF.
  ENDTRY.

ENDFUNCTION.

Web-Links

Literatur