Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Introduction

This following is to get object service files, attached to the document and to send an e-mail with attachments.

Ex: Expense form has to be to submitted to the manger for approvals and along with the expense form all the corresponding bills ( ex: Hotel/air bills ) attached to the trip document need to send as attachemnts along with expense form along with the notification, for mangers's approvals. Manager could see the bills if require before approving the expense form.

This could be used for ex: custom portal developments and custom workflow development etc.The same logic could be used for any other object service files manipulated..

Please check the parameters details, source code and correspoding includes below

Function module Z_TR_PR05_MAIL_ATTACHMENT_N

RFC to get the object service files attached and to mail.

Parameters:

Import:

EMPLOYEENUMBER TYPE BAPIEMPL-PERNR                                  Personnel Number
TRIPNUMBER            TYPE FTPT_REQ_HEAD-REINR                       Trip Number
DOCUMENT_DATA  TYPE SODOCCHGI1                                         Subject
IND_ATT                     TYPE CHAR1                                                     Single-Character Indicator

Tables:

OBJECT_HEADER           LIKE SOLISTI1 SAPoffice: Single List with Column Length 255
CONTENTS_TXT            LIKE SOLISTI1 SAPoffice: Single List with Column Length 255
RECEIVERS                     LIKE SOMLRECI1 SAPoffice: Structure of the API Recipient List

Exceptions:

TOO_MANY_RECEIVERS
DOCUMENT_NOT_SENT
DOCUMENT_TYPE_NOT_EXIST
OPERATION_NO_AUTHORIZATION
PARAMETER_ERROR
X_ERROR
ENQUEUE_ERROR

**** Source code **** Start
FUNCTION z_tr_pr05_mail_attachment_n.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(EMPLOYEENUMBER) TYPE BAPIEMPL-PERNR
*" VALUE(TRIPNUMBER) TYPE FTPT_REQ_HEAD-REINR
*" VALUE(DOCUMENT_DATA) TYPE SODOCCHGI1
*" VALUE(IND_ATT) TYPE CHAR1 OPTIONAL
*" TABLES
*" OBJECT_HEADER STRUCTURE SOLISTI1 OPTIONAL
*" CONTENTS_TXT STRUCTURE SOLISTI1 OPTIONAL
*" RECEIVERS STRUCTURE SOMLRECI1 OPTIONAL
*" EXCEPTIONS
*" TOO_MANY_RECEIVERS
*" DOCUMENT_NOT_SENT
*" DOCUMENT_TYPE_NOT_EXIST
*" OPERATION_NO_AUTHORIZATION
*" PARAMETER_ERROR
*" X_ERROR
*" ENQUEUE_ERROR
*"----------------------------------------------------------------------
  REFRESH: all_connections,
    signature.
  CLEAR:objtxt[],
  reclist[],
  objpack[],
  objhead[],
  objbin[],
  objtxt[],
  signature[],
  components[],
  content[],
  ascii_content[].
  CONCATENATE employeenumber tripnumber INTO objkey.
  DATA: lw_classname LIKE bapibds01-classname.
  DATA: lw_system LIKE bapibds01-log_system.
  DATA: lw_bds_conn00 TYPE bds_conn00.
  DATA: lw_att.
  lw_classname = 'BUS2089'.
  CONCATENATE sy-sysid sy-mandt INTO lw_system.
  DATA: it_components LIKE bapicompon OCCURS 0 WITH HEADER LINE.
  SELECT SINGLE * FROM bds_conn00
        INTO lw_bds_conn00
        WHERE classname = lw_classname
        AND classtype = 'BO'
        AND object_key = objkey.
  IF sy-subrc = 0.
    lw_att = 'X'.
  ENDIF.
  IF lw_att = 'X'.
    CALL FUNCTION 'BDS_BUSINESSDOCUMENT_QUERY_D'
      EXPORTING
        logical_system = lw_system
        classname      = lw_classname
        classtype      = 'BO'
        object_key     = objkey
      TABLES
        components     = it_components
      EXCEPTIONS
        OTHERS         = 7.
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    CALL FUNCTION 'BDS_ALL_CONNECTIONS_GET'
      EXPORTING
        logical_system   = lw_system
        classname        = lw_classname
        classtype        = 'BO'
        objkey           = objkey
      TABLES
        signature        = signature
        all_connections  = all_connections
      EXCEPTIONS
        no_objects_found = 1
        error_kpro       = 2
        internal_error   = 3
        not_authorized   = 4
        OTHERS           = 5.
    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.
* Get the Binary Contents based on hardcoded parameters
  REFRESH: components,
  content,
  ascii_content.
  CLEAR objtxt[].
  LOOP AT contents_txt.
    objtxt = contents_txt.
    APPEND objtxt.
  ENDLOOP.
  DESCRIBE TABLE objtxt LINES tab_lines.
  READ TABLE objtxt INDEX tab_lines.
  docdata-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
  CLEAR objpack-transf_bin.
  objpack-head_start = 1.
  objpack-head_num = 0.
  objpack-body_start = 1.
  objpack-body_num = tab_lines.
  objpack-doc_type = 'RAW'.
  APPEND objpack.
  DATA: lw_cnt TYPE i.
  DATA: lw_start TYPE i.
  DELETE signature WHERE prop_name <> 'BDS_DOCUMENTTYPE'.
  DATA: lw_signature LIKE bapisignat OCCURS 1 WITH HEADER LINE.
  DATA: lw_it_components LIKE it_components.
* Extract the file content
* IF NOT SIGNATURE[] IS INITIAL.
  IF lw_att = 'X'.
    LOOP AT signature.
      CLEAR lw_signature[].
      lw_signature = signature.
      APPEND lw_signature. CLEAR lw_signature.
      lw_cnt = lw_cnt + 1.
      CALL FUNCTION 'BDS_BUSINESSDOCUMENT_GET_TAB'
        EXPORTING
          classname       = 'BUS2089'
          classtype       = 'BO'
          object_key      = objkey
          binary_flag     = ' '
        TABLES
          signature       = lw_signature
          components      = components
          content         = content
          ascii_content   = ascii_content
        EXCEPTIONS
          nothing_found   = 1
          parameter_error = 2
          not_allowed     = 3
          error_kpro      = 4
          internal_error  = 5
          not_authorized  = 6
          OTHERS          = 7.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
* Because listobject is of size 1022
* and objbin is of size 255, do conversion
      PERFORM convert_binary_content.
      APPEND LINES OF lit_objbin TO objbin.
      IF lw_cnt = 1.
        lw_start = 1.
      ELSE.
        lw_start = lw_start + tab_lines.
      ENDIF.
      DESCRIBE TABLE lit_objbin LINES tab_lines.
      objpack-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objbin ).
      objpack-transf_bin = 'X'.
      objpack-head_start = 1.
      objpack-head_num = 0.
      objpack-body_start = lw_start.
      objpack-body_num = tab_lines.
      READ TABLE lw_signature WITH KEY prop_name = 'BDS_DOCUMENTTYPE'.
      lv_filename = lw_signature-prop_value.
      SPLIT lv_filename AT '.' INTO lw_var1 lw_var2.
      CALL FUNCTION 'SPLIT_FILENAME'
        EXPORTING
          long_filename  = lv_filename
        IMPORTING
          pure_extension = lv_extension.
      objpack-doc_type = lw_var2.
      objpack-obj_name = 'ATTACHMENT'.
      objpack-obj_descr = lw_var1.
      IF lw_var2 = ' '.
        READ TABLE it_components INTO lw_it_components
        WITH KEY doc_count = signature-doc_count.
        CLEAR lv_extension.
        CALL FUNCTION 'SPLIT_FILENAME'
          EXPORTING
            long_filename  = lw_it_components-comp_id
          IMPORTING
            pure_extension = lv_extension.
        objpack-doc_type = lv_extension.
      ENDIF.
      APPEND objpack.
    ENDLOOP.

  ENDIF.
* Now create the message and send the document.
* Create Message Body
  docdata = document_data.
  CLEAR: lw_var1,
  lw_var2.
* Create Message Attachment
* Write Packing List (Attachment)
  CLEAR reclist[].
  LOOP AT receivers.
    FIND '@' IN receivers.
    IF sy-subrc = 0.
      reclist-rec_type = 'U'.
    ELSE.
      reclist-rec_type = 'B'.
    ENDIF.
    reclist-receiver = receivers.
    APPEND reclist. CLEAR reclist.
  ENDLOOP.
* Send Message
* IF NOT SIGNATURE[] IS INITIAL.
  IF ( lw_att = 'X' AND NOT signature[] IS INITIAL ).
    CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
      EXPORTING
        document_data              = docdata
        put_in_outbox              = 'X'
        commit_work                = 'X'
      TABLES
        packing_list               = objpack
        object_header              = objhead
        contents_bin               = objbin
        contents_txt               = objtxt
        receivers                  = reclist
      EXCEPTIONS
        too_many_receivers         = 1
        document_not_sent          = 2
        document_type_not_exist    = 3
        operation_no_authorization = 4
        parameter_error            = 5
        x_error                    = 6
        enqueue_error              = 7
        OTHERS                     = 8.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
    IF sy-subrc <> 0.
      MESSAGE ID 'SO' TYPE 'S' NUMBER '023'
      WITH docdata-obj_name.
    ENDIF.
  ELSE.
    CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
      EXPORTING
        document_data              = document_data
        document_type              = 'RAW'
        put_in_outbox              = 'X'
        commit_work                = 'X'
      TABLES
        object_content             = objtxt
        receivers                  = reclist
      EXCEPTIONS
        too_many_receivers         = 1
        document_not_sent          = 2
        document_type_not_exist    = 3
        operation_no_authorization = 4
        parameter_error            = 5
        x_error                    = 6
        enqueue_error              = 7
        OTHERS                     = 8.
    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.
  SUBMIT rsconn01 WITH mode = 'INT'
  AND RETURN.
ENDFUNCTION.
*** Source code *** End

saplz_tr_pr05_mail_n

*functionpool  : saplz_tr_pr05_mail_n
INCLUDE lz_tr_pr05_mail_ntop. " Global Data
INCLUDE lz_tr_pr05_mail_nuxx. " Function Modules
INCLUDE z_pro5_mail_forms.
********

Include LZ_TR_PR05_MAIL_NTOP

*** Include LZ_TR_PR05_MAIL_NTOP **** START
FUNCTION-POOL z_tr_pr05_mail_n. "MESSAGE-ID ..
DATA: docdata LIKE sodocchgi1,
objpack LIKE sopcklsti1 OCCURS 1 WITH HEADER LINE,
objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE,
objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE,
objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE,
reclist LIKE somlreci1 OCCURS 1 WITH HEADER LINE,
all_connections LIKE bdn_con OCCURS 1 WITH HEADER LINE,
signature LIKE bapisignat OCCURS 1 WITH HEADER LINE,
components LIKE bapicompon OCCURS 1 WITH HEADER LINE,
content LIKE bapiconten OCCURS 1 WITH HEADER LINE,
ascii_content LIKE bapiascont OCCURS 1 WITH HEADER LINE,
lv_filename LIKE dbmsgora-filename,
lv_extension LIKE sdbad-funct.

DATA: tab_lines TYPE i,
doc_size TYPE i,
att_type LIKE soodk-objtp,
objkey TYPE swotobjid-objkey.
DATA : pa_mail TYPE so_recname, " OBLIGATORY,
pa_obkey TYPE swotobjid-objkey.
DATA:lit_objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE.
DATA: lw_var1(50).
DATA: lw_var2(10).
**    Include LZ_TR_PR05_MAIL_NTOP  **** END

z_pro5_mail_forms

**    include z_pro5_mail_forms **** start
*&---------------------------------------------------------------------*
*& Include Z_PRO5_MAIL_FORMS
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form convert_binary_content.
*&---------------------------------------------------------------------*
FORM convert_binary_content .
  DATA:
* work area to hold the line datalv_str type xstring,
  l_content TYPE x,
  lv_len_in TYPE i,
  lv_len_out TYPE i,
  objftext LIKE solisti1 OCCURS 10 WITH HEADER LINE,
  content_try LIKE bapiconten OCCURS 1 WITH HEADER LINE.
* read the file sizeread table components index 1.
  lv_len_in = components-comp_size.
* convert to one string
  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = lv_len_in*
      first_line   = 0
      buffer       = lv_str
    TABLES
      binary_tab   = content
    EXCEPTIONS
      failed       = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
* convert to 255 char
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer        = lv_str
    IMPORTING
      output_length = lv_len_out
    TABLES
      binary_tab    = lit_objbin.
ENDFORM. " convert_binary_content
**    Include Z_PRO5_MAIL_FORMS **** End
  • No labels