Business Case: The business requirement is to attach any document while doing the goods receipt for material. The attachments can be any reference documents or image or any other document related to goods receipt of the material. To meet this requirement SAP has provided a tool bar called 'Generic Service Toolbar'(GOS).
Now a days since we are using mobile application so there is a requirement to create a attachment for existing Material document through Net-weaver Gateway. We can able to upload any document (PDF/DOC/JPG/XLS) and through OData service the document will be linked to the corresponding Material document.
The below are steps required to create an OData service in SAP NW Gateway.
Step-1: Create a project for Attachment using SEGW transaction.
Step-2: Create an Entity type, Entity sets. Remember the entity type should be defined as a Media type.
Step-3: Create a property for the Entity type.
Step-4: Generate the project. It should create all the back end classes for MPC, MPC_EXT, DPC and DPC_EXT.
Now go to DPC_EXT class and redefine the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM.
Inside the method write the below code to convert the XSTRING received from the OData service into Binary format and
then upload the binary data into SAP.
METHOD /iwbep/if_mgw_appl_srv_runtime~create_stream.
DATA: lt_objhead TYPE STANDARD TABLE OF soli,
lt_xdata TYPE solix_tab,
lt_data TYPE soli_tab,
ls_folmem_k TYPE sofmk,
ls_note TYPE borident,
ls_object TYPE borident,
ls_obj_id TYPE soodk,
ls_fol_id TYPE soodk,
ls_obj_data TYPE sood1,
ls_data TYPE soli,
ls_xdata TYPE solix,
lv_ep_note TYPE borident-objkey,
lv_extension TYPE c LENGTH 4,
lv_mblnr TYPE mblnr,
lv_mjahr TYPE mjahr,
lv_objkey TYPE char70,
lv_tmp_fn TYPE string,
lv_file_des TYPE so_obj_des,
lv_offset TYPE i,
lv_size TYPE i,
lv_temp_len TYPE i,
lv_offset_old TYPE i.
CONSTANT: lc_hex_null TYPE x LENGTH 1 VALUE '20'.
**Call function to convert XSRTING to Binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = is_media_resource-value
append_to_table = lc_x
TABLES
binary_tab = lt_content.
**Call function to get Folder id
CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
EXPORTING
region = 'B'
IMPORTING
folder_id = ls_fol_id
EXCEPTIONS
communication_failure = 1
owner_not_exist = 2
system_failure = 3
x_error = 4
OTHERS = 5.
**Get the document number and file name from SLUG
SPLIT iv_slug AT '/' INTO lv_mblnr lv_mjahr lv_file_des.
**Get the file extension
SPLIT lv_file_des AT '.' INTO lv_tmp_fn lv_extension.
CONCATENATE lv_mblnr lv_mjahr INTO lv_objkey.
ls_object-objkey = lv_objkey.
**For Goods movement BUS type is BUS2017
ls_object-objtype = 'BUS2017'.
ls_obj_data-objsns = 'F'.
ls_obj_data-objla = sy-langu.
ls_obj_data-objdes = lv_file_des.
ls_obj_data-file_ext = lv_extension.
TRANSLATE ls_obj_data-file_ext TO UPPER CASE.
**Calculate the length
lv_offset = 0.
lv_size = xstrlen( is_media_resource-value ).
ls_obj_data-objlen = lv_size.
WHILE lv_offset <= lv_size.
lv_offset_old = lv_offset.
lv_offset = lv_offset + 255.
IF lv_offset > lv_size.
lv_temp_len = xstrlen( is_media_resource-value+lv_offset_old ).
CLEAR ls_xdata-line WITH lc_hex_null IN BYTE MODE.
ls_xdata-line = is_media_resource-value+lv_offset_old(lv_temp_len).
ELSE.
ls_xdata-line = is_media_resource-value+lv_offset_old(255).
ENDIF.
APPEND ls_xdata TO lt_xdata.
ENDWHILE.
**Change Hex data to Text data
CALL FUNCTION 'SO_SOLIXTAB_TO_SOLITAB'
EXPORTING
ip_solixtab = lt_xdata
IMPORTING
ep_solitab = lt_data.
**Insert document
CALL FUNCTION 'SO_OBJECT_INSERT'
EXPORTING
folder_id = ls_fol_id
object_type = 'EXT'
object_hd_change = ls_obj_data
IMPORTING
object_id = ls_obj_id
TABLES
objhead = lt_objhead
objcont = lt_data
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 ls_object-objkey IS NOT INITIAL.
ls_folmem_k-foltp = ls_fol_id-objtp.
ls_folmem_k-folyr = ls_fol_id-objyr.
ls_folmem_k-folno = ls_fol_id-objno.
ls_folmem_k-doctp = ls_obj_id-objtp.
ls_folmem_k-docyr = ls_obj_id-objyr.
ls_folmem_k-docno = ls_obj_id-objno.
lv_ep_note = ls_folmem_k.
ls_note-objtype = 'MESSAGE'.
ls_note-objkey = lv_ep_note.
**Link the object inserted
CALL FUNCTION 'BINARY_RELATION_CREATE_COMMIT'
EXPORTING
obj_rolea = ls_object
obj_roleb = ls_note
relationtype = 'ATTA'
EXCEPTIONS
no_model = 1
internal_error = 2
unknown = 3
OTHERS = 4.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
IF sy-subrc EQ 0.
COMMIT WORK.
ENDIF.
ENDIF.
ENDMETHOD.
Step-5: Now go to gateway client /IWFND/MAINT_SERVICE and add a document. In the SLUG parameter
pass the Material Document number, Year and File name separated by '/'.
Now click on execute. While executing please keep in mind that the HTTP method "POST" must be selected.
Step-6: After execution of the OData service go to the transaction MIGO and provide the Material document number and year.
you can able to see the attachment.
I hope this will be helpful to many of you.
Regards,
Nitin