SAP OData Services Transaction codes

The four important SAP Transaction codes for ODATA Service

/nSEGW –> This Transaction is used to create an OData service in the Backend.

/n/IWFND/MAINT_SERVICE –> Register the OData service in the frontend.

/n/IWFND/GW_CLIENT –> Execute the HTTP Method in the frontend.

/n/IWFND/ERROR_LOG –> To view the Error logs in the OData Service.

How do you call a function import in SAP ODATA Service

Function imports:

They are useful in performing predefined actions in the system. Function imports are implemented when the implementation cannot be done using the CRUD-Q Methods

Steps to create a function import in ODATA Service

  1. Go to the Transaction SEGW ( Gateway service builder ) and open the Project.
  2. Expand the ‘Project’ and choose the ‘Data model’ and then right click to choose ‘Create’ and then select the function import.
  3. The Function import is uniquely identified based on its name. so give an unique name and select the tick mark.
  4. Maintain the critical parameters of the Function import such as Return type kind, Return Type, Return Cardinality and HTTP method type.
  5. If the Return type kind is entity type, then mention the name of the entity type in the column Return Type.
  6. HTTP Method type can be of two different types. a) GET : to get the data. b ) POST: to post the data in the backend.
  7. The next step would be to save the project and generate the Runtime objects. The function import is visible in the metadata of the ODATA service.
  8. Now open the SAP Gateway client using the Transaction /n/IWFND/GW_CLIENT and mention the ODATA Service name in the metadata URI option and finally execute. The function import looks like this <Functionimport Name = “Function1″ EntitySet=” ” HTTPmethod=” ” ReturnType=” “>
  9. Function import parameters can also be inserted in the ODATA Service
  10. Implementation of Function import in the class DPC_EXT. The method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~EXECUTE_ACTION is redefined. This method will implement the necessary functionality of the Functionimport
  11. Now the function import can be tested using the SAP Gateway client. Transaction: /n/IWFND/GW_CLIENT. The Request URI looks like /sap/opu/odata/SAP/service_name1/functionimport1?parameter1=’678cd’. Please set the HTTP Method to POST and execute the URI. The success message must appear on the HTTP response.

How to create an OData Service in SAP

  1. The OData service is developed on a new project in the SAP system. Therefore start with the Transaction SEGW.
  2. Click New in the toolbar.
  3. Please enter a new project name and a short description on the popup fenster. Define also a package and finally confirm the entries through tick mark.
  4. A new project is now created. There are four folders under the new project. Right click on the folder ‘DATA MODEL’ and choose import from DDIC structure. The already available ABAP Dictionary structure can be imported directly and it is not necessary to manually assign an OData-service to each field. In addition to the ABAP structures, you can also import Type definitions from BAPI-Function module parameters or search help.
  5. In the popup fenster, provide the entity type and also provide the structure SFLIGHT in the respective fields. Ensure that the option Create default entity set is selected. Finally click the ‘Next’.
  6. In the next window, you will see all the fields of the table SFLIGHT. Now click all the fields of the structure, except the field MANDT. Click ‘Next’.
  7. In the next window, you will see all the fields with their respective Datatype. The primary keys of this structure must be confirmed through the checkbox column ‘IS KEY’. So please select the checkbox for the respective primary key fields of the structure. In this case, let us choose the CARRID, CONNID, FLDATE as our primary keys. Once you are done with the selection, please confirm the entries by selecting the ‘FINISH’ Button.
  8. As you can see, the Data model consists of the Entity types and Entity sets. The Entity Type Flights contains the properties folder. The properties folder contains all the fields of the structure. It displays the Edm core type and Label which describes the fields. The entityset ‘Flightsset’ is created as we checked this option ‘Create Default Entity Set’.
  9. The CRUDQ Methods of the Entityset is available in the folder service implementation.

What are the different query option in ODATA-URL

The OData URL can be modified to represent the corresponding results. It provides various Filter options

$Filter: It is like the WHERE condition. The below URL in the browser returns the products with the price less than 100.

https://services.OData.org/v3/OData/OData.svc/Products?$filter=Price lt 100    

$Select: It is used to display only the respective columns of the table.

https://services.OData.org/v3/OData/OData.svc/Products?$select=ID,Categories

In the above example, it displays only the columns ID and Category.

$Orderby: It is used to sort the results. The below url displays the products with the price in decreasing order.

https://services.OData.org/v3/OData/OData.svc/Products?$orderby=Price desc    

$top :  It displays the initial records of the table.

https://services.OData.org/v3/OData/OData.svc/Products?$top=3

$skip: The Data records can be skipped through this option.

https://services.OData.org/v3/OData/OData.svc/Products?$skip=4

$expand: It is used to join two tables and display corresponding results.

https://services.OData.org/v3/OData/OData.svc/Categories(1)?$expand=Products

Multiple query options is possible through the & symbol

InitialURL/Entityset?$option1=……&$option2=….&$optionN

How to construct an OData service in SAP ABAP

ODATA SERVICE CONSTRUCTION

Entity container: It contains all the entity sets.

Entity set: It represents a table. It is based on the entity type.

Entity type: All the fields(properties) and the primary keys are mentioned.

Property: It represents the fields along with data type.

Entity: It describes a line of record in the data table.

Association: It connects the primary and foreign key fields. It represents the cardinality of the relationship (1:1, 1: n).

Navigation: The two different entity sets can be related and represented through the navigation.

What is an OData service document

OData service document represents a service which includes entity sets, uniform resource identifier (URI)

It can be opened through the browser with the URI / URL

For example, let us consider the following OData service.

http://services.odata.org/v3/OData/OData.svc/

If you open this URL in browser, it shows up the different entity sets.

Entitysets shown in Browser

As you can see in the above image, the entitysets in this OData service are Products, Productdetails, Categories, Suppliers, Persons, PersonDetails, Advertisements. So it includes totally seven entitysets. The entityset ‘Category’ includes information about all categories.

If the categories is added to the above URL like

http://services.odata.org/v3/OData/OData.svc/Categories

The above url in the browser would list down all the categories.

Similarly if you wish to see all the Products. Then follow this link

http://services.odata.org/v3/OData/OData.svc/Products

URL is case sensitive. So the first letter ‘P’ in Products must be written in Capital. Otherwise it does not work.

It is much similar to the Transaction SE11 (ABAP Dictionary) where we give the Table name. The Data contents can be displayed for this table.

The URLs mentioned above executes the HTTP-Get methods, which in turn calls the Read-OData method. In the background, the data would be retrieved and the result is sent back.

Let us look into an example of retrieving data records with single primary key.

Specific line of record can also be retrieved by specifying the primary key value in the URL.

http://services.odata.org/v3/OData/OData.svc/Products(5)

In the above URL, the products with the ID 5 will be retrieved.

Retrieving data records with two primary keys.

/Entityset( p_key1 = ‘Value1’, p_key2 = ‘Value2’ )

In the above case, both the primary key fields are of datatype string. So the value is mentioned in quotes. If the datatype is integer, then quotes should not be used.

Retrieving data records with WHERE clause.

All the products with a particular category ID can also be retrieved. To achieve this, it must be predefined in the services. We can also navigate from one entity set to another. For example, in the below URL, only the data from the product entity set are displayed.

https://services.OData.org/v3/OData/OData.svc/Categories(5)/Products

Retrieving data records from two different entity sets

This option is much like the JOIN query in SQL. The data from two different entity sets are displayed with a WHERE condition.

https://services.OData.org/v3/OData/OData.svc/Categories(2)?$expand=Products

How does OData service works

OData services works through entity sets and entities. Data is represented in the form of Tables.

Tables are referred as Entity sets and the lines of record are referred as entities.

The Data is accessed through the following OData methods. These OData methods are referred as CRUD methods.

OData MethodHttp MethodDescription and its relation to sql command
CreatePOSTIt creates a new record. It is similar to INSERT command in SQL
ReadGETIt reads the records. It is similar to SELECT command in SQL.
The Read method from OData can be further classified.
For example, the Read method is used to read only one record.
The Query method is used to read more lines of record.
UpdateUPDATEIt updates the records. It is similar to UPDATE command in SQL
DeleteDELETEIt deletes the records. It is similar to DELETE command in SQL

What is OData service in SAP

  • It is a http type protocol
  • The protocol is between the other applications (Web browser etc) and SAP system.
  • The OData-open data protocol supports two formats Atom and JSON. It can also be controlled in different ways through URLs.
  • It follows the REST Architecture (Client/Server).
  • The Data from the source side and the server side is identified through the uniform resource identifier.
  • OData service is a logical data model. The Data is stored as entities.
  • The OData service is identified through URL/URI.