Updating records to the database table from a file using ODATA service

  • Requirement: I have a database table with 4 fields. Now I want to update the data available in a local file to that database table. 

DB table: “ZVK_T_EXAMPLE”

KavithaV_0-1721455041333.png

NOTE:   File should be saved as ‘.CSV’ format or the file should be text file 

KavithaV_1-1721455041335.png

  • Now I need to upload this data into the above db table (ZVK_T_EXAMPLE) using odata service. 
  • Goto SEGW and create a project. 
  • Create an entity type by using the above database table structure. 

KavithaV_2-1721455041338.png

  • Make sure that you enabled media checkbox for that entity type. 

KavithaV_3-1721455041342.png

  • Then generate the project. 

KavithaV_4-1721455041344.png

  • Then go to DPC extension class and redefine the create stream method.

 

METHOD /iwbep/if_mgw_appl_srv_runtime~create_stream.
DATA lo_msg_container TYPE REF TO /iwbep/if_message_container.
lo_msg_container = me->mo_context->get_message_container( ).
DATA:lt_data TYPE TABLE OF zvk_t_example,
ls_data TYPE zvk_t_example,
lt_string TYPE TABLE OF string,
lv_string TYPE string,
lo_obj TYPE REF TO cl_abap_conv_in_ce.

lo_obj = cl_abap_conv_in_ce=>create(
input = is_media_resource-value
).

lo_obj->read(
IMPORTING
data = lv_string ” Data Object To Be Read
).

SPLIT lv_string AT cl_abap_char_utilities=>cr_lf INTO TABLE lt_string.

LOOP AT lt_string INTO DATA(ls_string).
SPLIT ls_string AT ‘,’ INTO ls_data-name
ls_data-gender
ls_data-mobile.
IF ls_data IS NOT INITIAL.
“Name validations
IF ls_data-name = ‘ ‘.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘002’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF ls_data-name+0(1) CA ‘ ‘.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘003’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF strlen( ls_data-name ) <= 3.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘004’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSEIF NOT CONV string( ls_data-name ) CO ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz’.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘005’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ENDIF.

“Gender validations

IF ls_data-gender <> ‘Female’ AND
ls_data-gender <> ‘FEMALE’ AND
ls_data-gender <> ‘MALE’ AND
ls_data-gender <> ‘Male’.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘006’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ENDIF.

“Mobile number validations

IF ls_data-mobile = ‘ ‘.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘007’ ” Message Number
).

ELSEIF strlen( ls_data-mobile ) < 10.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘008’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.

ELSEIF ls_data-mobile+0(1) = ‘ ‘.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘009’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.

ELSEIF ls_data-mobile CA ‘ ‘.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘010’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.

ELSEIF NOT ls_data-mobile CO ‘0123456789’.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘011’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.

ELSEIF NOT ( ls_data-mobile CP ‘6*’ OR
ls_data-mobile CP ‘7*’ OR
ls_data-mobile CP ‘8*’ OR
ls_data-mobile CP ‘9*’ ).
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘012’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ENDIF.

” Generating SNRO

IF ls_data IS NOT INITIAL.
CALL FUNCTION ‘NUMBER_GET_NEXT’
EXPORTING
nr_range_nr = ’01’
object = ‘ZVK_EXAMPL’
IMPORTING
number = ls_data-id
EXCEPTIONS
interval_not_found = 1
number_range_not_intern = 2
object_not_found = 3
quantity_is_0 = 4
quantity_is_not_1 = 5
interval_overflow = 6
buffer_overflow = 7
OTHERS = 8.

ENDIF.

APPEND ls_data TO lt_data.
ENDIF.

ENDLOOP.

“Updating data to the DB table
IF lt_data IS NOT INITIAL.

CALL FUNCTION ‘ZVK_FM_INSERT_FILE’ IN UPDATE TASK
EXPORTING
lt_data = lt_data
EXCEPTIONS
ex_insert = 1
OTHERS = 2.
COMMIT WORK.

IF sy-subrc <> 0.

lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>error ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘013’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.
ELSE.
lo_msg_container->add_message(
EXPORTING
iv_msg_type = /iwbep/cl_cos_logger=>success ” Message Type
iv_msg_id = ‘ZVK_MSG_CLASS’ ” Message Class
iv_msg_number = ‘001’ ” Message Number
).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
message_container = lo_msg_container.

ENDIF.

ENDIF.

ENDMETHOD.

 

  • Then create ODATA service for this project. 
  • Goto SAP gateway client (/o/iwfnd/maint_service). 

KavithaV_5-1721456149270.png

  • Select the entity set and add your file by clicking on “Add File” button in request payload and select HTTP method as post. 

KavithaV_6-1721456149275.png

KavithaV_7-1721456149278.png

  • Then click on the execute button. 

KavithaV_8-1721456149283.png

KavithaV_9-1721456149285.png

KavithaV_10-1721456149288.png

KavithaV_11-1721456149292.png

KavithaV_12-1721456149297.png

KavithaV_13-1721456149299.png

 

 

Scroll to Top