Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
harikrishna_malladi
Active Participant
17,565

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.

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


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.

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


Testing:


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

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.

SRGBTBREL table stores the information of Relationships in GOS Environment.


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

Thanks&Regards,

Harikrishna M.

12 Comments
Labels in this area