How to perfrom CRUD operations in OData


In previous blog, we discussed Introduction to OData and how to create an OData service in SAP NetWeaver Gateway. We have also shown how to test an OData if it is working fine or not. Now, we need to learn how to perform operations like Insert, Update and Delete. In this article we will also learn basic queries of reading data.

How it works

In case of OData, there are three phases of data transfer. These are:

1. Sending Data from Frontend

Data is sent from UI (Client) to OData (Server) in form of query

2. Taking Data from Frontend

At OData we receive data from Frontend using data provider (Code implemented in CRUD Operation section)

io_data_provider->read_entry_data(IMPORTING es_data= ls_entity).

3. Sending Data to Frontend

From OData we send data to UI using et_entityset or er_entity after data manipulation (Code implemented in CRUD Operation section).

Operations in ODATA

In this section, we will explore all the methods generated automatically, once you generate an OData. In last article, we have already instructed to write all your CRUD operations related code in DPC_EXT class. Hence, we will follow the same concept here.

SAP ABAP OData provides different methods for CRUD operations, these are:

Method

SQL Operation

Description

GET_ENTITY

Select

This method is used to read a single data based on table keys

GET_ENTITYSET

Select

This method is used to read entire data of a table

CREATE_ENTITY

Insert

This method is used to create/insert a new data in table

UPDATE_ENTITY

Update/Modify

This method is used to update an existing data in table

DELETE_ENTITY

Delete

This method is used to delete an existing data in table

 

Query in SAP OData

In this section, we will explore all the Query we can perform while reading data from OData. In above section, we have learnt how to do read calls using GET_ENTITY and GET_ENTITYSET. Sometimes, we need to filter out data according to our requirement, or to get total number of data counts or to get data in specific order. These all SAP OData Queries are explained below:
Here, let us suppose <your service name>  = https://isd.sap.com/<OData Service Name>

Query

Description

Example

$metadata

It gives the metadata detail of your service. By metadata we mean it will provide information about all the entity sets with their field names and their attributes

<your service name>/$metadata

$FILTER

It is mainly used during Read Entity Set call. During Read, we can send some filter value in backend that can be later used in where condition of Select queries

<your service name>/$FILTER

$top and $skip

It is mainly used to get limited data in the UI, in case you are reading all the data, and the table is having a very large data

<your service name>/$top and $skip

$orderby

This is used to order the data in ascending or descending order for a specific key

<your service name>/$orderby

$format=json

This returns the data in form of JSON format. By default, the result is in XML format.

<your service name>/$format=json

$inlinecount

This returns the number of data of a table that will appear in the UI

<your service name>/$inlinecount

$expand

This is used to bind the association and navigation data together

<your service name>/$expand

$value

This is used to return the media data

<your service name>/$value


In the following steps, I will list down steps to create an entry through OData service. Similar steps are required for other CRUD operations.

To configure a well-defined SAP Gateway OData service, complete the following steps in your SAP system. These steps cover the general procedure, but need some consideration for your own SAP OData system and your preferred techniques for configuring SAP.

Display the SAP Gateway Service Builder (transaction SEGW)

Create a project

Figure 1. Select Create Project


Configure details for the project

Chose a project name, give your project a description, select Standard generation strategy, and specify the package. The project will be saved as a local object (Package: $TMP, or click Local Object.)

Figure 2. Create Project, specify details


Create the Entity type by importing a pre-defined DDIC Structure for the Order header.

Right-click Data Model, and then select Import > DDIC Structure.

Figure 3. Data Model, Import > DDIC Structure


Choose the required structure.

If the Sales order structure is not available in your SAP system, select the ABAP structure ZST_VBAK. Click Next.

Figure 4. Import from DDIC Structure, step 1 of 3


Choose the required fields, and then click Next.

Figure 5. Import from DDIC Structure, step 2 of 3


Select the key field, VBELN, and then click Finish.

Figure 6. Import from DDIC Structure, step 3 of 3 Result: You should now see the new Entity that has been created.


Repeat the same sequence of steps from 4 to 7 to import the next structure, OrderStatus.

This may be an Item structure. The following example shows the import of another custom structure called ZST_SO_STATUS.

Second Import from DDIC Structure, step 1 of 3

Second Import from DDIC Structure, step 2 of 3

Second Import from DDIC Structure, step 3 of 3


When you have completed the preceding steps, you should see the two newly-created Entity Types: OrderStatus and SalesOrder:

Figure 7. Result of Import from DDIC Structures – The two newly-created Entity Types: OrderStatus and SalesOrder


Create the Entity Sets for the entity types that you have just created.

Right-click the folder Entity Sets, and then select Create.

Figure 8. Create Entity Sets


Create an Entity Set SalesOrderSet for the entity type SalesOrder.

(A naming convention is to make the Entity Set name by appending ‘_Set’ to the Entity Type name.)

Figure 9. Create Entity Set SalesOrderSet for the entity type SalesOrder

Create an Entity Set OrderStatusSet for the entity type OrderStatus:

Figure 10. Create Entity Set OrderStatusSet for the entity type OrderStatus


You should now see the newly-created entity sets:

Figure 11. The newly-created Entity Sets


Create the Associations which sets the cardinal rules, and then click Next.

Figure 12. Create Association, step 1 of 3


Set the Dependent Property, Vbeln, and then click Next.

Figure 13. Create Association, step 2 of 3


Click Finish.

Figure 14. Create Association, step 3 of 3

Click Save.

Figure 15. Save the changes made

Check Consistency. Right-click ZORDER_MAINTAIN and then select Check Consistency:

Figure 16. Check Consistency

Generate runtime objects

To use the service with the changes that you have made, you need to generate the runtime objects. This creates the underlying ABAP classes that contain all the information that you've specified in the service builder. Click the Generate Runtime Objects icon or right-click ZORDER_MAINTAIN and then select Generate Runtime:

Figure 17. Generate Runtime Objects


While generating the runtime objects, the class and service details are generated as shown below.

Figure 18. Generate Runtime Objects – Model and Service Definition


Choose the package for the objects.

Figure 19. Generate Runtime Objects – Create Object Directory Entry


Now, you should see that the runtime objects were created successfully:

Figure 20. Messages confirming that the runtime objects were created


This completes the design stage. Next, you need to develop the code as part of the implementation.

Expand the Runtime Artifacts folder.

Figure 21. Expand the Runtime Artifacts folder


Double-click the Data Provider Class Extension (DPC_EXT)

Figure 22. Double-click the Data Provider Class Extension (DPC_EXT)


Implement the insert method

Because more than one structure or table is involved, you need to implement the method '/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY' for implementing CREATE (POST) functionality for deep insert. However, for single insert, you can use the CREATE_ENTITY method.

To implement the method, first click the Change Mode icon:

Figure 23. Click the Change Mode icon


Select the method:

Deep Insert (Nested)

Figure 24. Select the Deep Insert method


Independent Insert Implement separate CREATE ENTITY methods as shown below:

Figure 25. Select the Independent Insert method


After selecting the method, click the Redefine icon.

Figure 26. Click the Redefine icon to redefine and edit the method


The method has been redefined and is available to code the business logic.

Add the business logic for your requirements.

Figure 27. Add business logic to the method

After completing the logic development, the CREATE functionality is complete.

Register the service Use Service Registration: Transaction: /IWFND/MAINT_SERVICE

In the Activate and Maintain Service window, click Add Service

Figure 28. Activate and Maintain Service window – Add Service


Get the Technical service name from the transaction SEGW.

The Runtime artifact which ends with 'SRV' in SEGW refers to the Technical Service; for example:

Figure 29. Example runtime artifact in SEGW that refers to the Technical Service


In the next screen, specify the System Alias (created by Basis) and the new service which has been created.

After that, click the Get Services icon. The result is displayed:

Figure 30. Result: New service has been added


Click Add Selected Services.

Our service is now registered and ready to be exposed in Fiori application. In the above steps, we saw how to create an entry in table using create OData service. Similarly, if we are to read, update or delete entries we can redefine respective methods and add appropriate code.

In the next blog, we will look deeply into how to register service SAP OData service through /iwfnd/maint_service




Previous
Next Post »