CRUD Operations in OData

In this post, we will discuss on how to perform CRUD operations in OData

Basically, CRUD is an acronym which defines four actions in OData i.e., Create, Read, Update and Delete. To work with CRUD operations, we need to implement their specific methods in the extension class which we will learn in the later part of the post. Now we will create a project for performing basic CRUD operations.

This mainly involves 4 steps

  • Create Data model i.e., Entity type , Entity set
  • Generate run time objects
  • Implement the logic in the DPC_EXT class
  • Register the service and test the service

Step 1:

Create a OData project using T-code SEGW

CRUD Operations in OData

Enter the Project name, description, select the package as shown above and click on tick mark. Once project created, it will be shown as below with empty folders

CRUD Operations in OData

Create Entity Type and Entity set (Here, I will create entity type and entity set using DDIC structure which already exists)

Right click on Data model folder -> Import -> DDIC structure

CRUD Operations in OData

It will display a pop up screen. Here fill the Entity type name, DDIC Structure and check the Default Entity set checkbox. Click on Next

CRUD Operations in OData

Here all the fields of the DDIC structure will be displayed in the next screen.

CRUD Operations in OData

Choose the required fields which should be part of entity type by selecting the checkbox and click on Next.

CRUD Operations in OData

In the next screen, we need to select the key field for the Entity Type and Click on finish to create Entity Type and Entity Set

Note: At least one key field should be marked for Entity type

Once Entity Type and Entity Set are created, they look like below

CRUD Operations in OData

Step 2:

Generate the run time objects by clicking on red button

CRUD Operations in OData

It will suggest names for MPC and DPC classes by default. Click on enter to continue.

It will show successful message for run time objects generated like below by creating classes.

CRUD Operations in OData

Step 3:

Now we need to implement logic in the class ZCL*DPC_EXT by redefining the methods for required actions

a) Fetch all the entries (also called as QUERY)

To fetch all the entries from a table, we need to redefine *_GET_ENTITYSET method of the Entity. In this example, we should redefine AIRLINE_GET_ENTITYSET method

CRUD Operations in OData

Place the cursor on method name, click on redefine button and Implement the logic

CRUD Operations in OData

Step 4:

Register the service & Test it

Before testing, we need to register the service if it is not registered yet.

For that, click on service maintenance -> select the required system -> Click on register button

CRUD Operations in OData

And click on yes button in pop up window

CRUD Operations in OData

Click on yes

CRUD Operations in OData

Registration status will be turned to green once the service is registered

Note: Registering the service is one time step and if you want to maintain the service or view the service, we can either click on Maintain option or using T-code /n/IWFND/MAINT_SERVICE

Click on Gateway Client button or enter t-code /n/IWFND/GW_CLIENT to open the gateway and to test the service

CRUD Operations in OData

In Gateway, select the entity set(Shift+F11) on which you want to perform actions and select the appropriate Http method (GET in this scenario) and execute. If you look at the above , all the entries are listed in the Http response body.

Http method : GET

Request URI :

b) Fetch single entry based on key (Read)

Now I will fetch single entry based on the key mentioned in the Request URI

For this we need to redefine the AIRLINE_GET_ENTITY method and implement logic in it.

CRUD Operations in OData

In Request URI, pass the key name with its value as shown below.

CRUD Operations in OData

Http Method : GET

Request URI :

Note: If we have two keys for entity type, then we need to pass both the keys in the Request URI else it will throw error. The two keys are separated by comma (,) in URI

c)  Create a Record

Here we will create a new record by referencing old record. For this, we need to implement logic in *_CREATE_ENTITY method

CRUD Operations in OData

First, fetch single record and click on Use as Request option to copy the payload or record to HTTP Request body

CRUD Operations in OData

Once copied, give new values for new record and select POST method and execute

CRUD Operations in OData

Http method : POST

Request URI :

d)  Update the record

Here, we will be updating a existing record. For this we need to implement logic in *_UPDATE_ENTITY method

CRUD Operations in OData

First, fetch single record and click on Use as Request option to copy the payload or record to HTTP Request body

CRUD Operations in OData

Once copied, enter updated values for existing record and select PUT method and execute

CRUD Operations in OData

Http method : PUT

Request URI :

e) Delete a record

Here, we will delete an existing record. For this, we need to implement our logic in *_DELETE_ENTITY method

CRUD Operations in OData

Enter the key, value of the record in Request URI which need to deleted and execute

CRUD Operations in OData

Http method : Delete

Request URI :

 

Leave a Reply