Scenario:

Many times there is a business requirement of linking documents, entering notes, sending notes or linking an internet address to various SAP objects. These external  attachments can be reference documents, pictures, Email attachments, design , diagrams or related spreadsheets. To meet this requirement SAP has provided a tool bar called ‘Generic Service Toolbar'(GOS).

Recently, I came across a requirement where i had to create a attachment for existing sales order (va02) through report. in this requirement i have created a Custom report.

By using this report, You can attach a wide range of documents like Word Document, Excel Sheets, PDF and Text files and many more, including pictures.

Go to transaction SE38 and create report .

Here is the Source Code.

*Declarations

*Structure Declarations

TYPES : BEGIN OF ty_table,    “Structure for FileName

         fname(128) TYPE c,

  END OF ty_table.

*Data Declarations

DATA: w_prog                TYPE sy-repid,         “Current Program Name

             w_dynnr              TYPE sy-dynnr,        “Current Dynpro Number

             w_attachement   TYPE borident,         “Work Area for BOR object identifier

             ws_borident        TYPE borident,         “Work Area for BOR object identifier

             w_document       TYPE sood4,            “Interface for send screen and MOM

             folder_id             TYPE soodk,             “Definition of Object Key

             w_h_data           TYPE sood2,             “Object Definition Workarea

             w_fol_data         TYPE sofm2,             “Folder Contents Work area

             w_rec_data        TYPE soos6,             “Transfer Information of folder Work area

             ws_files              TYPE ty_table,

             wt_files               TYPE TABLE OF ty_table.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

*Parameter Declarations

PARAMETER: p_mandt TYPE sy-mandt,                                                      ” Client Number

            p_vbeln TYPE vbeln,                                                                            ” Sales Order Number

            p_path  TYPE ibipparms-path  MEMORY ID ad_local_path,               ” File Path

            p_name(30).                                                                                          ” Name of attachement.

SELECTION-SCREEN END OF BLOCK b1.

*Initialization Event

INITIALIZATION .

w_prog = sy-repid .

w_dynnr = sy-dynnr .

*/ Selection Screen  For File Path Selection

  AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path .

*/ F4 Help for file selection

  CALL FUNCTION ‘F4_FILENAME’

  EXPORTING

    program_name        = w_prog

    dynpro_number       = w_dynnr

    field_name               = ‘P_PATH’

IMPORTING

    file_name                 = p_path .

*Start of selection Evvent

START-OF-SELECTION .

*/Client Validations

IF sy-mandt NE p_mandt .

WRITE ‘Mandt Error’ .

EXIT .

ENDIF .

*/ Assign Object Keys to the Structure BOR

ws_borident-objkey      = p_vbeln.             “SalesOrder Number

ws_borident-objtype     = ‘EQUI’.               “Object Type

ws_borident-objtype     = ‘BUS2032’.        “BUS Number

*/ Filename Assign to the Structure

ws_files-fname = p_path .               “Path

APPEND ws_files TO wt_files .

*/ Folder Root

CALL FUNCTION ‘SO_FOLDER_ROOT_ID_GET’

EXPORTING

      region    = ‘B’

IMPORTING

      folder_id = folder_id

EXCEPTIONS

     OTHERS    = 1.

*/ Append data to the MOM Structure

  w_document-foltp   = folder_id-objtp.

  w_document-folyr   = folder_id-objyr .

  w_document-folno   = folder_id-objno .

  w_document-objdes  = p_name .           “Name of file

  w_document-objnam  = p_name .          “Name of file

*/ Attachment FileName Assignment

  w_h_data-objdes = p_name .                 “Name of file

*/ Using this function module to read FILE from Presentation server

CALL FUNCTION ‘SO_DOCUMENT_REPOSITORY_MANAGER’

EXPORTING

   method                  = ‘IMPORTFROMPC’

   ref_document        = w_document

TABLES

    files                       = wt_files

CHANGING

    document              = w_document

    header_data          = w_h_data

    folmem_data         = w_fol_data

    receive_data         = w_rec_data .

*/ File Creation OkCodes

       IF w_document-okcode = ‘CREA’ OR w_document-okcode = ‘CHNG’.

          w_attachement-objtype = ‘MESSAGE’.

          w_attachement-objkey  = w_document(34).

          CALL FUNCTION ‘BINARY_RELATION_CREATE_COMMIT’

             EXPORTING

                obj_rolea            = ws_borident

                obj_roleb            = w_attachement

                relationtype         = ‘ATTA’

             EXCEPTIONS

                no_model             = 1

                internal_error       = 2

                unknown              = 3

                OTHERS             = 4.

*/Error Handling

           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 .

Before Execution:


Go to the transaction VA03 (Sales Order Display ) in system as shown in the below screen shot.

/wp-content/uploads/2015/05/1_701794.png

There is no attachment existed for this sales order. Now go to report and execute it following selection screen will be display.


/wp-content/uploads/2015/05/2_701876.png

Give the inputs Client Number, Sales  order number , File path and file name as shown in below.

In this i have given a text document for attachment.

/wp-content/uploads/2015/05/3_701877.png

When click on execute button it will create a sales order attachment.


/wp-content/uploads/2015/05/4_701885.png

Testing:


Now go to transaction VA03 and check whether the attachment is added or not.

/wp-content/uploads/2015/05/5_701886.png

Attachment is added.


Table Storage:

The Content of file converted to binary and it stores in tables like SOOD and SRGBTBREL.

SOOD Table stores the information of attached Object info.

/wp-content/uploads/2015/05/6_701887.png

SRGBTBREL table stores the information of Relationships in GOS Environment.


/wp-content/uploads/2015/05/7_701888.png

I hope this Blog would help many ABAP’ers in attaching files.

Thanks&Regards,

Harikrishna M.

To report this post you need to login first.

11 Comments

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

    1. HariKrishna Malladi Post author

      Thankyou Hardika,you can attach only single attachment by using this function module.

      If you want to attach multiple files, then you can loop this function module.

      (0) 
      1. Hardika Chheda

        Sure Thanks, One more info i needed, the files attached are stored in the database server by using the above method. Is there a way we can store the files on the content server?

        Like Archiving the files?

        (0) 

Leave a Reply