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 Net weaver Gateway Service.

By Using this Blog, you can attach a wide range of documents like Word Documents,Excel Sheets, PDF, Text Files and Images and many more through Gateway service.

Procedure:

We have created a project in ECC system for create attachment to Sales Order through Gateway Service. As shown in below.

/wp-content/uploads/2015/11/2_824317.png

Right Click on the Data Model folder and select Import DDIC structure and Give the Entity Type Name as Attachment and select the Required properties for the Entity Type.

In the Entity Type Properties select the check-box: Media. Our  Entity Type Attachment and its properties look as below.

/wp-content/uploads/2015/11/3_824319.png

Then click on Generate Run time objects. It displayed “Generated Objects Successfully” , that time Generated all class automatically.

Then Redefine the DEFINE  method in the *MPC_EXT Class and add the below logic.

method DEFINE.

     super->DEFINE( ).


DATA: lo_entity     type REF TO  /IWBEP/IF_MGW_ODATA_ENTITY_TYP,

            lo_property type REF TO  /IWBEP/IF_MGW_ODATA_PROPERTY.

lo_entity = model->GET_ENTITY_TYPE( IV_ENTITY_NAME = ‘Attachment’ ).“Entity Name

IF lo_entity is BOUND.


lo_property = lo_entity->GET_PROPERTY( IV_PROPERTY_NAME = ‘Filename’ ).“Key Value(SLUG)

lo_property->SET_AS_CONTENT_TYPE( ).


ENDIF.


endmethod.

Then redefine the CREATE_STREAM method ( /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM ) in the *DPC_EXT class and implement the below logic to upload the file attachment into the Sales Order(VA02) based on Sales Order Number.

Code:


method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM.

*————————————————————-

* Constants

*————————————————————-

   CONSTANTS :

                            c_bus2032      TYPE swo_objtyp VALUE ‘BUS2032’,      ” Bus number for sale order

                            c_ext(3)          TYPE c                VALUE ‘EXT’,

                            c_atta(4)         TYPE c                VALUE ‘ATTA’,

                            c_b(1)             TYPE c                VALUE ‘B’,

                            c_x(1)             TYPE c                VALUE ‘X’,

                            c_o(1)             TYPE c                VALUE ‘O’.

*————————————————————-

*  Data declaration

*————————————————————-

   DATAit_content               TYPE STANDARD TABLE OF soli,     ” Content Of File Storage

               it_objhead               TYPE STANDARD TABLE OF soli,

               wa_folmem_k          TYPE sofmk,                                     ” Folder Content Data

               wa_note                  TYPE borident,                                   ” BOR object identifier

               wa_object                TYPE borident,

               wa_obj_id                TYPE soodk,                                     ” Definition of an Object (Key Part)

               wa_fol_id                 TYPE soodk,

               wa_obj_data             TYPE sood1,                                    ” Object definition and Change attributes

               lv_ep_note               TYPE boridentobjkey,                       ” BOR Object Key

               lv_extension             TYPE c LENGTH 4 value ‘TXT’,        ” File Extension only

               lv_so_num                TYPE vbeln_va,                               ” Sales order number

               lv_file_des                TYPE so_obj_des.                           ” File name

*/Refresh data

   REFRESH: it_content[], it_objhead[].

*/Field Symbol for SLUG

   FIELD-SYMBOLS:<fs_key> TYPE /iwbep/s_mgw_name_value_pair.

*/Read the SLUG Value and Name based on INDEX

   READ TABLE it_key_tab ASSIGNING <fs_key> INDEX 1.

*/Function module for  Xstring to Binary Conversion

   CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’

     EXPORTING

       buffer                = is_media_resourcevalue     “Xstring

      append_to_table        = c_x

* IMPORTING

*   OUTPUT_LENGTH         =

     TABLES

       binary_tab            = it_content[]                “Binary

             .

*/Get folder id

   CALL FUNCTION ‘SO_FOLDER_ROOT_ID_GET’

     EXPORTING

       region                = c_b

     IMPORTING

       folder_id             = wa_fol_id

     EXCEPTIONS

       communication_failure = 1

       owner_not_exist       = 2

       system_failure        = 3

       x_error               = 4

       OTHERS                = 5.

   CLEAR: lv_so_num,lv_file_des.

   IF iv_slug IS NOT INITIAL.

     SPLIT iv_slug AT ‘/’ INTO lv_so_num lv_file_des.

     IF lv_so_num IS NOT INITIAL.

       CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’

         EXPORTING

           input  = lv_so_num

         IMPORTING

           output = lv_so_num.

     ENDIF.

   ENDIF.

*/ Assigning Valuse to the Standard Strucuture Fields

   wa_objectobjkey     =   lv_so_num.              ” Sales Order Number

   wa_objectobjtype    =   c_bus2032.              ” Bus Number

   wa_obj_dataobjsns   =   c_o.                    ” Sensitivity of Object (O-Standard)

   wa_obj_dataobjla    =   sylangu.               ” Language

   wa_obj_dataobjdes   =   lv_file_des.            ” Slug Value – Description

   wa_obj_datafile_ext =   lv_extension.           ” File Extension

*/ Change Extension to UpperCase

   TRANSLATE wa_obj_datafile_ext TO UPPER CASE.

   wa_obj_dataobjlen lines( it_content ) * 255.

*/ Insert data

   CALL FUNCTION ‘SO_OBJECT_INSERT’

     EXPORTING

       folder_id                  = wa_fol_id

       object_type                = c_ext

       object_hd_change           = wa_obj_data

     IMPORTING

       object_id                  = wa_obj_id

     TABLES

       objhead                    = it_objhead

       objcont                    = it_content

     EXCEPTIONS

       active_user_not_exist      = 1

       communication_failure      = 2

       component_not_available    = 3

       dl_name_exist              = 4

       folder_not_exist           = 5

       folder_no_authorization    = 6

       object_type_not_exist      = 7

       operation_no_authorization = 8

       owner_not_exist            = 9

       parameter_error            = 10

       substitute_not_active      = 11

       substitute_not_defined     = 12

       system_failure             = 13

       x_error                    = 14

       OTHERS                     = 15.

   IF sysubrc = 0 AND wa_objectobjkey IS NOT INITIAL.

     wa_folmem_kfoltp = wa_fol_idobjtp.

     wa_folmem_kfolyr = wa_fol_idobjyr.

     wa_folmem_kfolno = wa_fol_idobjno.

*/Please note: wa_fol_id and wa_obj_id are different work areas

     wa_folmem_kdoctp = wa_obj_idobjtp.

     wa_folmem_kdocyr = wa_obj_idobjyr.

     wa_folmem_kdocno = wa_obj_idobjno.

     lv_ep_note = wa_folmem_k.

     wa_noteobjtype = ‘MESSAGE’.

     wa_noteobjkey = lv_ep_note.

*/Link it

     CALL FUNCTION ‘BINARY_RELATION_CREATE_COMMIT’

       EXPORTING

         obj_rolea      = wa_object

         obj_roleb      = wa_note

         relationtype   = c_atta

       EXCEPTIONS

         no_model       = 1

         internal_error = 2

         unknown        = 3

         OTHERS         = 4.

     CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’

      EXPORTING

        WAIT           = c_x

*     IMPORTING

*       RETURN        =

               .

     IF sysubrc EQ 0.

*/Commit it

       COMMIT WORK.

     ENDIF.

   ENDIF.

   endmethod.

All input Values we have to get into the SLUG parameter from the UI Side. If you have multiple input parameter values then with concatenate of multiple parameter values with delimiter we have to get the values in SLUG parameter.

Testing Our Service:

Now we will test our service in Gateway Client transaction for that is /IWFND/MAINT_SERVICE

Upload a file through add file button which is on left side corner as shown in the below screen shot.

/wp-content/uploads/2015/11/4_824320.png

Pass SLUG values(mandatory) pass file name and sales order number as shown in the below screen shot.

In this example we passing Multiple parameter values like SalesOrder Number and File Description. These two values are separted by ‘ , ‘. shown in below screen shot.

/wp-content/uploads/2015/11/5_824336.png

Paste our URI in Request URI field and click on POST HTTP Method.

URI: /sap/opu/odata/sap/ZASC_ECOMM_SO_ATTACHMENT_SRV/AttachmentSet

Service Response:


Attachment 123 l.png

Successfully created the Attachment in GWS.

Result:

Go to Sales Order Display (VA03) Transaction and click on /wp-content/uploads/2015/11/1_824338.png Services for Objects in Title Bar then you will get the attachment list as shown in below.

You will find your attachment.

/wp-content/uploads/2015/11/7_824346.png

Attachment added successfully to the Sales Order.

Thanks&Regards,

Harikrishna Malladi

To report this post you need to login first.

14 Comments

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

  1. Imran Khan

    Nice blog. 🙂

    I have followed the same steps. But after attachment is done, if I open the attachment from GOS, the data in the attachment is incorrect or some junk values are there.

    Is there any parameters I need to change based on the document type?

    (0) 
      1. Imran Khan

        I have passed extension also based on document type.

        But while converting from xstring to binary, internal table it_content is filled with some junk values and the same is being displayed in the file when I open from GOS.

        (0) 
        1. HariKrishna Malladi Post author

          Imran are you sending file through gateway interface or any report?. In the above case i am sending file through Gateway, in gateway interface the file content should be in binary format thats why i am changing that format.If you use any other interface no need to convert the file format to Binary.

          (0) 
          1. Imran Khan

            Harish,

            I am sending through gateway only. If I send the file in CSV format, data reflecting correctly in attachment. But if I upload file in DOCX, PNG, TXT, etc., junk values are coming.

            (0) 
            1. HariKrishna Malladi Post author

              Imran, I think it is not supporting your system please contact your basis,if you need any configuration.

              By using SO_OBJECT_INSERT Function module you can send only 3 characters extension file names only like txt,pdf,jpg,png and it is not supporting 4 characters extension file names like xlsx,docx because the structure(sood1)  holds 3 characters only.

              (0) 
              1. Imran Khan

                Harish, If that is the case, it should work for TXT and PNG. But for these type of documents also I am getting junk values after uploading.

                Did you try opening the TXT, PNG files after uploading in the GOS??

                (0) 
    1. HariKrishna Malladi Post author

      Yes, I have checked its working fine for TXT file.(attached)

      /wp-content/uploads/2016/02/at1_881094.png

      Where as in PNG image files successfully upload the GOS bar, when try to open that image shows below.

      /wp-content/uploads/2016/02/at2_881095.png

      I have manually added the PNG image to the GOS Bar it will shows like this Information message.

      /wp-content/uploads/2016/02/at3_881099.png

      I think SAP System is not supporting PNG File format.

      (0) 

Leave a Reply