INSERT operation with oData Service.
This post describes how to use INSERT record into table using oData service.
Please give a look at my OData Wildcard Character Search which describes about how to create Project, Data Model, Entity & Entity set.
Goto T Code: SEGW and Create new project with desired name.
Created Project will appear in left panel of SEGW TCode. Now Right click on Data model → Import → DDIC structure.
Provide the Entity name and Structure name and select the fields you want to use in oData service. At least one field of Entity should be Key field, selecting CARRID here as Key.
Create Runtime Artifacts by clicking on button
Now navigate to Data Provider Extension (DPC_EXT) class
To fetch a single record, we need to write code inside GET_ENTITY method. Redefine GET_ENTITY method by clicking on button.
In GET_ENTITY method: Parameter IT_KEY_TAB Captures input from froentend & ER_ENTITY sends output to frontend.
Write below code in GET_ENTITY method:
METHOD scarrset_get_entity.
DATA : wa_keytab TYPE /iwbep/s_mgw_name_value_pair.
Capture CARRID value
READ TABLE it_key_tab INTO wa_keytab INDEX 1.
IF sy–subrc IS INITIAL.
SELECT SINGLE
FROM scarr
INTO CORRESPONDING FIELDS OF er_entity
WHERE carrid EQ wa_keytab–value.
ENDIF.
ENDMETHOD.
To INSERT a single record in table, we need to write code in CREATE_ENTITY method. Redefine CREATE_ENTITY method.
In CREATE_ENTITY method: Parameter IO_DATA_PROVIDER Captures input from froentend & ER_ENTITY sends output to frontend.
Write below code in CREATE_ENTITY method:
DATA : wa_iodp TYPE zcl_zmtest5_insert_mpc=>.
* Capture Input record from frontend into WA_IODP
io_data_provider->read_entry_data( IMPORTING es_data = wa_iodp ).
* Insert into table SCARR
INSERT INTO scarr VALUES wa_iodp.
IF sy–subrc IS INITIAL.
COMMIT WORK.
* Transfer Inserted records to frontend
er_entity = wa_iodp.
ENDIF.
Now add the Service in T Code: /IWFND/MAINT_SERVICE. Please give a look to my previous post about how to add a service.
You can find the Service name in Runtime Artifacts. Here service name is: ZMTEST5_INSERT_SRV.
After adding the Service, Goto T Code: /IWFND/GW_CLIENT, You’ll get the below scree:
Select your EntitySet and add the filter in Generated URL: (Carrid=’AZ’)
Url will become: /sap/opu/odata/sap/ZMTEST5_INSERT_SRV/SCARRSet(Carrid=’AZ’)
Select HTTP Method: GET and execute, You’ll get the output.
We need to use XML format same like above screenshot to send HTTP request. Click on ‘Use as Request’ button. It’ll copy the contents of HTTP Response to HTTP Request.
Change the KeyValue from ‘AZ’ to whatever you want to update in table. Select POST radio-button and modify the fields values as shown below, then execute.
If everything goes fine then you’ll get the Inserted records in HTTP Response window.
Record inserted in Table SCARR
It’s really very helpful..
LikeLike
Very Good
LikeLike