Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
harikrishna_malladi
Active Participant

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.

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.

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 borident-objkey,                       " 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_resource-value     "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_object-objkey     =   lv_so_num.              " Sales Order Number

   wa_object-objtype    =   c_bus2032.              " Bus Number

   wa_obj_data-objsns   =   c_o.                    " Sensitivity of Object (O-Standard)

   wa_obj_data-objla    =   sy-langu.               " Language

   wa_obj_data-objdes   =   lv_file_des.            " Slug Value - Description

   wa_obj_data-file_ext =   lv_extension.           " File Extension

*/ Change Extension to UpperCase

   TRANSLATE wa_obj_data-file_ext TO UPPER CASE.

   wa_obj_data-objlen 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 sy-subrc = 0 AND wa_object-objkey IS NOT INITIAL.

     wa_folmem_k-foltp = wa_fol_id-objtp.

     wa_folmem_k-folyr = wa_fol_id-objyr.

     wa_folmem_k-folno = wa_fol_id-objno.

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

     wa_folmem_k-doctp = wa_obj_id-objtp.

     wa_folmem_k-docyr = wa_obj_id-objyr.

     wa_folmem_k-docno = wa_obj_id-objno.

     lv_ep_note = wa_folmem_k.

     wa_note-objtype = 'MESSAGE'.

     wa_note-objkey = 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 sy-subrc 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.

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.

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:


Successfully created the Attachment in GWS.

Result:

Go to Sales Order Display (VA03) Transaction and click on Services for Objects in Title Bar then you will get the attachment list as shown in below.

You will find your attachment.

Attachment added successfully to the Sales Order.

Thanks&Regards,

Harikrishna Malladi

19 Comments
Labels in this area