oData Service – with INSERT statement

light

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.

1.jpg

Created Project will appear in left panel of SEGW TCode. Now Right click on Data model → Import → DDIC structure.

2

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.

3

Create Runtime Artifacts by clicking on 5 button

4

Now navigate to Data Provider Extension (DPC_EXT) class

5

To fetch a single record, we need to write code inside GET_ENTITY method. Redefine GET_ENTITY method by clicking on 2 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 sysubrc IS INITIAL.
           SELECT SINGLE 

           FROM scarr
           INTO CORRESPONDING FIELDS OF er_entity
            WHERE carrid EQ wa_keytabvalue.
       ENDIF.
ENDMETHOD.


6.jpg

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_dataIMPORTING es_data wa_iodp ).

       * Insert into table SCARR
        INSERT INTO scarr VALUES wa_iodp.
        IF sysubrc IS INITIAL.
         COMMIT WORK.
        *   Transfer Inserted records to frontend
        er_entity wa_iodp.
        ENDIF.


create_entity-code

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:

1

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.

1.jpg

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.

2

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.

create_entity-post

If everything goes fine then you’ll get the Inserted records in HTTP Response window.

Created.jpg

Record inserted in Table SCARR

table


 

 

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s