Skip to Content
Introduction: This blog describes how to Attach the documents from BSP Application to any SAP Business transactions like IE01, VA01, FB01, WTY etc.

In order to achieve this, we are going to use Business Document Service (BDS).

Before we see the code, let us look at the result.

As I said earlier, you can attach it to any Business transaction. I took an example of attaching the document into ISR: Notification (IQS22) Transaction.

Note: This example will not show how to create the Notification. Assume that Notification already exists.


image

    1. Once you click on attachment, you can see the attachment list in pop-up where you can select & view the documents.


image



image


Let’s look at the procedure to create attachment:


Here is the Function module, which can be used to upload the document to any business transaction. All you need to do is, finding a proper business object as per your requirement:

Note: Make it as “Remote Enabled”, so that it can be called outside SAP also.


FUNCTION ZATTACH_CREATE.
*”—-


“Local Interface:
*”  IMPORTING
*”     VALUE(FILE) TYPE  SAPB-SAPFILES
*”     VALUE(STR_FILE_CONTENT) TYPE  XSTRING
*”     VALUE(OBJECT_KEY) TYPE  BAPIBDS01-OBJKEY
*”     VALUE(BUS_OBJECT) TYPE  BAPIBDS01-CLASSNAME
*”  TABLES
*”      RETURN STRUCTURE  BAPIRET2
*”—-



  DATA: LOGICAL_SYSTEM TYPE BAPIBDS01-LOG_SYSTEM,
        CLASSNAME TYPE  BAPIBDS01-CLASSNAME,
        CLASSTYPE TYPE  BAPIBDS01-CLASSTYPE.


  DATA:  IT_FILES TYPE TABLE OF  BAPIFILES,
         IT_SIGNATURE TYPE TABLE OF  BAPISIGNAT,
         FILES TYPE  BAPIFILES,
         SIGNATURE TYPE   BAPISIGNAT.


  DATA: I_TOADD TYPE TOADD,
        FILENAME_ALL TYPE SAPB-SAPFILES,
        FILE_PATH TYPE SAPB-SAPFILES,
        FILE_PATH_MEMORY(250) TYPE C,    ” path für SAP memory
        FILE_PATH_LENGTH TYPE I,         ” length of the file_path
        FILE_CONTENT_LENGTH TYPE I,       ” length of the file_CONTENT
        FILE_NAME TYPE SAPB-SAPFILES,
        FILE_EXTENSION TYPE TOADD-DOC_TYPE.     ” file-extension.


  DATA: I_FILE_CONTENT TYPE TABLE OF BAPICONTEN.
  DATA: WA_FILE_CONTENT TYPE  BAPICONTEN.


  DATA:
        I_FILES TYPE TABLE OF BAPIFILES ,
        I_SIGNATURE TYPE TABLE OF BAPISIGNAT ,
        I_COMPONENTS TYPE TABLE OF BAPICOMPON ,
        COMPONENTS TYPE BAPICOMPON ,
        I_PROPERTIES TYPE TABLE OF SDOKPROPTY ,
        I_PROPERTIES2 TYPE TABLE OF BAPIPROPER ,
        I_PROPERTIES_DEL TYPE TABLE OF BAPIDPROPT ,
        MIMETYPE TYPE TOADD-MIMETYPE,
        COMPID TYPE BAPICOMPON-COMP_ID,  ” compid for popup
        COMPID_FLAG(1) TYPE C,           ” compid can’t be changed
        MODIFY_FLAG(1) TYPE C,           ” document is modified
        ATTRI_FLAG(1) TYPE C,            ” attri can’t be changed
        TITLE_FLAG(1) TYPE C,            ” titel of the popup
        CANCEL_FLAG(1) TYPE C,           ” cancel button Yes/No
        BUTTON_TEXT(10) TYPE C,
        INFO_TEXT(100) TYPE C,
        VERSION_NR TYPE BDN_CON-DOC_VER_NO,     ” version_nr for popup
        VERSION_LANGU2 TYPE T002T-SPTXT, ” language field in POPUP
        VERSION_LANGU TYPE BDN_CON-LANGU,” SPRAS language field
        TITLE_TEXT(100) TYPE C,          ” title of the popup
        DESCRIPTION TYPE BDN_CON-DESCRIPT.      ” descr. for popup


  FILE_CONTENT_LENGTH = XSTRLEN( STR_FILE_CONTENT ).


  FILENAME_ALL = FILE .


  DATA: OUT_LINES TYPE TABLE OF XSTRING.


  CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
    EXPORTING
      BUFFER     = STR_FILE_CONTENT
    TABLES
      BINARY_TAB = I_FILE_CONTENT.


  1. — split filename                                                 — *


  DATA: LENGTH TYPE I.
  DATA: PATH2(1200).  ” LIKE sapb-sapfiles.


  DATA: L_FILE(1200),
        L_DIR(1200),
        L_DELIMITER        TYPE C,
        L_OFFSET           TYPE I,
        L_BYTES            TYPE I,
        L_DELIMITER_OFFSET TYPE I,
        L_DELIMITER_BYTES  TYPE I,
        L_CHAR             TYPE C,
        L_PREVIOUS_CHAR    TYPE C,
        L_LAST_CHAR        TYPE C.


  PATH2 = FILENAME_ALL.
  LENGTH = STRLEN( PATH2 ).
  L_OFFSET = 0.
  WHILE L_OFFSET < LENGTH.
    L_LAST_CHAR = L_CHAR. “remember last character
    L_BYTES = CHARLEN( PATH2+L_OFFSET ).
    L_CHAR = PATH2+L_OFFSET(L_BYTES). “get current character
    IF L_CHAR = ‘/’ OR L_CHAR = ‘\’ OR L_CHAR = ‘:’.
      IF L_DELIMITER IS INITIAL OR L_DELIMITER = ‘:’.
        L_DELIMITER = L_CHAR.
      ENDIF.
      IF L_CHAR = L_DELIMITER.
        L_DELIMITER_OFFSET = L_OFFSET.
        L_DELIMITER_BYTES = L_BYTES.
        “remember character in front of delimiter:
        L_PREVIOUS_CHAR = L_LAST_CHAR.
      ENDIF.
    ENDIF.
    L_OFFSET = L_OFFSET + L_BYTES.
  ENDWHILE.


  IF NOT L_DELIMITER IS INITIAL.
    L_OFFSET = L_DELIMITER_OFFSET + L_DELIMITER_BYTES.
    L_FILE = PATH2+L_OFFSET.
    IF L_PREVIOUS_CHAR = ‘:’ OR L_DELIMITER = ‘:’.
      “path ends with or after drive identifer, include the delimiter
      L_DIR = PATH2(L_OFFSET).
    ELSEIF NOT L_DELIMITER_OFFSET IS INITIAL.
      “do not include a trailing delimiter for compatibility reasons
      L_DIR = PATH2(L_DELIMITER_OFFSET).
    ELSE.
      CLEAR L_DIR.
    ENDIF.
  ELSE.
    L_DIR = SPACE.
    L_FILE = PATH2.
  ENDIF.


  FILE_PATH = L_DIR.
  FILE_NAME = L_FILE.


**—-


  1. — set new file_path to SAP memory                                — *

  FILE_PATH_LENGTH = STRLEN( FILE_PATH ).


  IF FILE_PATH <> SPACE AND FILE_PATH_LENGTH < 250.
    FILE_PATH_MEMORY = FILE_PATH.
    SET PARAMETER ID ‘OAP’ FIELD FILE_PATH_MEMORY.
  ELSE.
    FILE_PATH_MEMORY = SPACE.
    SET PARAMETER ID ‘OAP’ FIELD FILE_PATH_MEMORY.
  ENDIF.


  1. — data declaration ———————————————— *

  DATA:
        SINGLE_C TYPE C.

  1. ——————————————————————– *

  CLEAR: SINGLE_C.


  LENGTH = STRLEN( FILE_NAME ).
  IF LENGTH > 0.
    WHILE LENGTH > 0.
      SINGLE_C = FILE_NAME+LENGTH(1).
      IF SINGLE_C CO ‘.’.
        LENGTH = LENGTH + 1.
        EXIT.
      ELSE.
        LENGTH = LENGTH – 1.
      ENDIF.
    ENDWHILE.
    IF LENGTH > 0.
      FILE_EXTENSION = FILE_NAME+LENGTH.
    ELSE.
      FILE_EXTENSION = SPACE.
    ENDIF.
  ELSE.
    FILE_EXTENSION = SPACE.
  ENDIF.


  IF FILE_EXTENSION <> SPACE.
    SET LOCALE LANGUAGE SY-LANGU.
    TRANSLATE FILE_EXTENSION TO UPPER CASE.              “#EC TRANSLANG
    SET LOCALE LANGUAGE SPACE.
  ENDIF.


  DATA: I_TOADV TYPE TABLE OF TOADV.
  DATA: WA_TOADV TYPE TOADV.
  DATA: OBJECT TYPE SAEOBJART.


  SELECT * FROM TOADV INTO CORRESPONDING FIELDS OF TABLE
          I_TOADV WHERE STANDARD = ‘X’.


  DELETE I_TOADV WHERE AR_OBJECT CS ‘BDS’ OR AR_OBJECT CS ‘ZSEST’.


  READ TABLE I_TOADV INTO WA_TOADV WITH KEY DOC_TYPE = FILE_EXTENSION.
*

  1. — get name and description for the document                      — *

  CLEAR: COMPID,
         DESCRIPTION,
         VERSION_NR,


         MODIFY_FLAG,
         COMPID_FLAG,
         ATTRI_FLAG,
         TITLE_FLAG.


  COMPID_FLAG    = ‘ ‘.              ” -> compid can’t be changed
  MODIFY_FLAG    = ‘S’.              ” -> popup in save modus
  ATTRI_FLAG     = ‘X’.              ” -> attri’s can be changed
  COMPID         = FILE_NAME.
  VERSION_NR     = ‘00000001’.
  VERSION_LANGU2 = SY-LANGU.
  TITLE_FLAG     = ‘1’.              ” -> BDN


  1. — get the mimetype of the docuclass                              — *


  DATA: I_DOCUMENTCLASS TYPE TOADD-DOC_TYPE.
  MOVE FILE_EXTENSION TO I_DOCUMENTCLASS.


  SET LOCALE LANGUAGE SY-LANGU.
  TRANSLATE I_DOCUMENTCLASS TO UPPER CASE.               “#EC TRANSLANG
  SET LOCALE LANGUAGE SPACE.


  SELECT SINGLE * FROM TOADD INTO I_TOADD WHERE
                             DOC_TYPE EQ I_DOCUMENTCLASS.
  IF SY-SUBRC NE 0.                    ” nothing found -> default!
    I_TOADD-DOC_TYPE = I_DOCUMENTCLASS.
    I_TOADD-MIMETYPE = ‘application/x-docType’.             “#EC NOTEXT
  ENDIF.


  MOVE I_TOADD-MIMETYPE TO MIMETYPE.


  1. — fill file and signature structure                              — *

  CLEAR: I_FILES, I_SIGNATURE.
  REFRESH : I_FILES, I_SIGNATURE.


  DESCRIPTION = FILE_NAME.


  FILES-DOC_COUNT = 1.
  FILES-DIRECTORY = FILE_PATH.
  FILES-FILENAME  = FILE_NAME.
  FILES-MIMETYPE  = MIMETYPE.


  APPEND FILES TO I_FILES.


  1. — fill components and signature structure                        — *

  COMPONENTS-DOC_COUNT  = 1.
  COMPONENTS-COMP_COUNT = 1.
  COMPONENTS-MIMETYPE   = MIMETYPE.
  COMPONENTS-COMP_ID         = FILE_NAME.


  APPEND COMPONENTS TO I_COMPONENTS.


  SIGNATURE-DOC_COUNT  = 1.
  SIGNATURE-PROP_NAME  = ‘BDS_DOCUMENTCLASS’.
  SIGNATURE-PROP_VALUE = FILE_EXTENSION.
  APPEND SIGNATURE TO I_SIGNATURE.


  SIGNATURE-PROP_NAME = ‘BDS_CONTREP’.
  SIGNATURE-PROP_VALUE = ‘ ‘.                               “#EC NOTEXT
  APPEND SIGNATURE TO I_SIGNATURE.


  SIGNATURE-PROP_NAME  = ‘BDS_DOCUMENTTYPE’.
  SIGNATURE-PROP_VALUE = WA_TOADV-AR_OBJECT. “‘ZSRVCXLS’.”TR001+


  APPEND SIGNATURE TO I_SIGNATURE.


  SIGNATURE-PROP_NAME  = ‘DESCRIPTION’.
  SIGNATURE-PROP_VALUE = DESCRIPTION.


  APPEND SIGNATURE TO I_SIGNATURE.


  SIGNATURE-PROP_NAME  = ‘LANGUAGE’.
  SIGNATURE-PROP_VALUE = VERSION_LANGU.


  APPEND SIGNATURE TO I_SIGNATURE.


  CALL FUNCTION ‘BDS_BUSINESSDOCUMENT_CREA_TAB’
    EXPORTING
      CLASSNAME       = BUS_OBJECT
      CLASSTYPE       = ‘BO'” classtype_select
      CLIENT          = SY-MANDT
      OBJECT_KEY      = OBJECT_KEY
      BINARY_FLAG     = ‘X’
    TABLES
      SIGNATURE       = I_SIGNATURE
      COMPONENTS      = I_COMPONENTS
      CONTENT         = I_FILE_CONTENT
    EXCEPTIONS
      NOTHING_FOUND   = 1
      PARAMETER_ERROR = 2
      NOT_ALLOWED     = 3
      ERROR_KPRO      = 4
      INTERNAL_ERROR  = 5
      NOT_AUTHORIZED  = 6
      OTHERS          = 7.


  COMMIT WORK AND WAIT.
  IF SY-SUBRC <> 0.

PERFORM BAPI_MESSAGE TABLES RETURN
USING SY-MSGTY
SY-MSGID
SY-MSGNO
SY-MSGV1
SY-MSGV2
SY-MSGV3
SY-MSGV4
SPACE
0
SPACE.

ENDIF.

ENDFUNCTION.

Subroutines used:


&—-



*&      Form  bapi_message
&—-



  1.       Add message to BAPI message table

—-



  1.      <–P_RETURN     Error table
  2.      –>P_MSGTY      Message type
  3.      –>P_MSGID      Message id
  4.      –>P_MSGNO      Message number
  5.      –>P_MSGV1      Message variable 1
  6.      –>P_MSGV1      Message variable 2
  7.      –>P_MSGV1      Message variable 3
  8.      –>P_MSGV1      Message variable 4
  9.      –>P_PARAMETER  Parameter name
  10.      –>P_ROW        Line no in case the parameter is a table
  11.      –>P_FIELD      Field name within parameter structure

—-



FORM BAPI_MESSAGE TABLES   P_RETURN     STRUCTURE BAPIRET2
                  USING    P_MSGTY      LIKE SY-MSGTY
                           P_MSGID      LIKE SY-MSGID
                           P_MSGNO      LIKE SY-MSGNO
                           P_MSGV1
                           P_MSGV2
                           P_MSGV3
                           P_MSGV4
                           P_PARAMETER  LIKE BAPIRET2-PARAMETER
                           P_ROW        LIKE BAPIRET2-ROW
                           P_FIELD      LIKE BAPIRET2-FIELD.


  1. Enter message into system variables


  SY-MSGTY = P_MSGTY.
  SY-MSGID = P_MSGID.
  SY-MSGNO = P_MSGNO.
  SY-MSGV1 = P_MSGV1.
  SY-MSGV2 = P_MSGV2.
  SY-MSGV3 = P_MSGV3.
  SY-MSGV4 = P_MSGV4.


  CALL FUNCTION ‘BALW_BAPIRETURN_GET2’
    EXPORTING
      TYPE      = SY-MSGTY
      CL        = SY-MSGID
      NUMBER    = SY-MSGNO
      PAR1      = SY-MSGV1
      PAR2      = SY-MSGV2
      PAR3      = SY-MSGV3
      PAR4      = SY-MSGV4
      PARAMETER = P_PARAMETER
      ROW       = P_ROW
      FIELD     = P_FIELD
    IMPORTING
      RETURN    = P_RETURN.


  APPEND P_RETURN.


ENDFORM.                    ” bapi_message
</textarea><br><br>

Let’s look at the BSP Application which we used to upload the documents:<br><br>
Note: I didn’t handle any validation in my BSP Application. Just kept in mind to attach the document & developed quick BSP Application :).<br><br>
<ol>
<li>Create below BSP Application & Pages as shown below:</li><br><br>
!https://weblogs.sdn.sap.com/weblogs/images/11786/BSP_attach_appl1.JPG|height=152|alt=image|width=357|src=https://weblogs.sdn.sap.com/weblogs/images/11786/BSP_attach_appl1.JPG|border=0!<br><br>
<li>Layout:</li><br>
<textarea rows=”10″ cols=”79″>
<%@page language=”abap” %>
<%@extension name=”htmlb” prefix=”htmlb” %>
<htmlb:content id               = “content”
               design           = “classicdesign2002design2003″
               controlRendering = “sap”
               rtlAutoSwitch    = “true” >
 

  1. event handler for checking and processing user input and
  2. for defining navigation


DATA: FILE        TYPE REF TO CL_HTMLB_FILEUPLOAD,
      FILE_DETAIL TYPE SAPB-SAPFILES,
      OBJKEY TYPE BAPIBDS01-OBJKEY,
      RETURN  TYPE  BAPIRET2_T.


FILE ?= CL_HTMLB_MANAGER=>GET_DATA( REQUEST = REQUEST ID = ‘uploadID’ NAME = ‘fileUpload’ ).


V_KEY = REQUEST->GET_FORM_FIELD( ‘keyid’ ).
V_BO = REQUEST->GET_FORM_FIELD( ‘busid’ ).


CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
  EXPORTING
    INPUT  = V_KEY
  IMPORTING
    OUTPUT = V_KEY.


OBJKEY = V_KEY.


FILE_DETAIL = FILE->FILE_NAME.


CALL FUNCTION ‘ZATTACH_CREATE’ DESTINATION ‘NONE’
  EXPORTING
    FILE             = FILE_DETAIL           ” file Name
    STR_FILE_CONTENT = FILE->FILE_CONTENT    “File content in XSTRING format
    OBJECT_KEY       = OBJKEY                “Key Field
    BUS_OBJECT       = V_BO                  “Business object
  TABLES
    RETURN           = RETURN.

    1. Now we are done with the uploading documents to Business transaction.
To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Shravan Kumar S
    Hi Raja,

      The Function module is great.  i have used it to attach a ‘bmp’file to the service order ‘BUS200016’.  The function module is not returning any error messages in the return table, but even then the document is not getting attached to the Service Order. Can u help on this.

    (0) 
      1. Shravan Kumar S
        Hi Raja,

        Yes i tried even attaching “.txt” file, the same situation no error is returned in Return table but even then the Service Order is not updated with the attachement.

        (0) 
        1. Shravan Kumar S
          Hi Raja,
           
              Here is one more thing in this context, i tried with attaching “.bmp” and “.txt” file to the service order. When i checked in “CRMD_ORDER” for the documents in the “Transaction Data -> Documents” tab for the same service order, no documents were attached.  But when i check in “OAOR” transaction with the same Obj_key and Bus object, then in the “Business Document Navigator”, in the first window ‘Crm Service Process’, the documents i attached were seen under the ‘General Documents’. What does it mean, they are seen in the document navigator, but not in the service order.   Can u help me out.
          Thanks in Advance
          (0) 
          1. Narayana Raju Sampathirao
            Hi Shravan,
            Could you able to solve this issue? Iam facing the same problem. I wanted to attach the document in the Attachments Tab of Business Activity. Everything is done oorrectly. I can see the document attached in OAOR transaction, but not in the Business Activity.
            Can you please suggest me how to solve this.
            (0) 
  2. Nitesh Jain
    Hi Raja,

    FYI, I am using this to attach document to an existing Sales Order.

    What entries are there in the table TOADV? In my case, the statement which deletes entries in the table i_toadv in the custom FM is returning a table with no entries to me.

    I am being returned an error CMS 002 i.e. “Content repository does not exist” by the FM BDS_BUSINESSDOCUMENT_CREA_TAB.

    Do we need to do any customizing in BDS?

    regards
    Nitesh

    (0) 

Leave a Reply