Introduction To OData Services
Introduction To OData Services
OF
ODATA
SERVICES
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Introduction
1. Architecture of SAP Fiori Systems
We mainly have three tier architecture for any System.
1. Presentation Layer
Introduction
1
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
1. In our Presentation Layer { Earlier we used to do dialog programming on GUI i.e.
Module Pool ECC } but now we are using Fiori Apps on the Browser.
2. Earlier our Application Layer was ECC system where we use to do Screen/Dialog
Programming but now we are switching to WebServer services to expose data to Fiori
apps like CDS, AMDP, OData etc.
Introduction 2
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
3. Earlier we use to use Oracle, MSSQL, etc. as our Database layer but now we are
switching towards HANA Database.
Introduction 3
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
We can expose our existing ERP data to the browser via HTTP or HTTPS using OData
services.
For building OData Services we need basics of Object Oriented Programming language.
Frontend Developers are responsible for building Fiori Apps and consuming OData services
in the Fiori Application.
Or you can be SAP Full stack Developer and perform both kind of work.
4. Introduction to OData
OData stands for Open Data Protocol.
Microsoft made this open source i.e. why any body can use it.
OData is a protocol ( protocol means rules ) given by Microsoft to develop REST Service (
Representational State Transfer ) so that we can expose our data on internet using http or
https.
.NET
Java
Python
Node.js
ABAP
Fiori App only and only talk to OData service , it do not care about the programming
language which has been used to develop the OData.
Introduction 4
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
In this training we will learn how to build our OData Service using ABAP programming
language.
8. SE38 :- Reports
Introduction 5
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
And add the service.
Introduction 6
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 2 :- Click on the Filter button and check the service.
Press enter.
Introduction 7
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
This is already registered in our SAP System.
Step 2 :- In the System Alias give Local as we are using the Embedded System and then in
the External Service name we will use GWSAMPLE_BASIC.
Introduction 8
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Press enter, select the service and click on the Add Selected Services.
Press enter.
Step 3 :- Click on the back button and you will find the service name there.
Introduction 9
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
9. Testing our Service
Click on the Call Browser button and it will launch our service.
Introduction 10
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Click on the Call Browser.
It will ask for the credentials, so pass the credentials there and click on Sign in.
This is service
s4h2023.sapdemo.com:8023/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/?
$format=xml
Introduction 11
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
10. Important Points
1. Service Document
Service Document will tell us all the available end points which are basically known as
Entity Sets.
2. Entity Set
Entity Set is the end point to perform CRUD Operations.
3. Entity Type
Every Entity Set is made up of Entity Type
Introduction 12
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Entity Type is the blueprint or the skeleton or the structure of the Entity Set
Metadata will also tells us about all the Entity Sets and their relationships also.
Metadata is the complete information about the entities and the properties of the OData
service.
Introduction 13
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Properties of OData Services
1. Introduction
OData acts as a backend for Frontend Applications.
OData Services are mainly executed under SAP NetWeaver gateway component with
database operations ( Select, Insert, Delete, Update ).
SAP NetWeaver gateway support two formats mainly
1. XML format
1. Data Model
2. Service Implementation
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
3. Runtime Artifacts
4. Service Maintenance
3. Data Model
1. Entity Type
It is an option to maintain the collection of fields with data type and length.
2. Entity
Entity is an option to hold a single record at a time.
3. Entity Set
Entity Set is an option to hold the collection of records.
4. Association
Association is the relationship between two entity types.
5. Association Set
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Association set is a relationship between two entity sets.
Note :-
Under single ODATA project we can have n number of entity type and entity sets.
4. Service Implementation
It mainly contains CRUD methods.
i.e. It will contain all the methods that will be responsible for performing CRUD
operations
5. Runtime Artifacts
Runtime artifacts mainly contains runtime classes ( which are auto generated ).
Whenever you will click on generate button, then these custom classes will be generated on
the runtime automatically.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
2. _MPC_EXT → Model Provider Extension Class 3.
6. Service Maintenance
Error Log :- It is used to trace / find out the error occurred during service execution.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Developing our First OData Service
1. Introduction
We can develop OData services in two ways :-
1. Using BAPI/RFC
Output
We will get one output in FLIGHT_LIST.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
3. Implementation
Step 1 :- Go to SEGW transaction code.
Step 2 :- Click on create button, a pop up screen will open, provide the necessary details.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 3 :- We have to create a entity type and entity set by importing fields from ABAP
structure.
Step 4 :- Right click on data model → Import → DDIC structure.
Step 5 :- Provide the name of entity type and also give ABAP structure.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 6 :- Click on Next button, All the fields of the structure will get available from which
we need to select our required fields.
Step 7 :- Click on Next → A pop up screen will appear where you need to select at least one
primary key.
Click on Finish.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
We can see our Entity Type and Entity Set has been created.
Step 2 :- Right Click on the get Entity Set option and select Map to Data Source Option.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
A pop up screen will appear.
Here, we need to pass type as Remote function call, i.e. RFC and in the Name
section we will pass the name of BAPI.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 4 :- Here, We will drag and drop the fields from the BAPI.
Step 5 :- Now, Once mapping is done, We need to generate the runtime artifacts.
Click on the red circle button → It will generate the runtime artifacts.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Click on Okay button and assign the package and transport request.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Click on Okay button and assign the package.
You must get a green status and now our OData service is developed completely.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 2 :- Click on Entity Sets and select the particular Entity Sets.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
We can see, we are getting all the data in XML format.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
7. Filter Conditions in OData Service
Using Filter conditions, we can filter out the required data.
Suppose, I want to apply some filter conditions.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Performing CRUD Operations in
OData Services
These are the database operations which we can perform using OData services in SAP.
1. Create_Entity( ) Method :-
This method is mainly used for creating a record in the table.
IO_DATA_PROVIDER :- This will capture input record values from front end
application ( SAP/Ui5/Fiori ).
ER_ENTITY :- This will send output record values to front end app.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
2. Update_Entity( ) Method :-
This method is similar to create entity as it contains the same parameters as are there in
create entity method.
Used mainly to update a existing record in the table.
3. Delete_Entity( ) Method :-
This method is used to delete a particular record from the table.
IT_KEY_TAB :- This will capture input key field value from front end application.
4. Get_Entity( ) Method :-
This method is used to fetch a single record from the database.
IT_KEY_TAB :- This will capture input key field value from front end application.
5. Get_EntitySet( ) Method :-
This method is used to fetch out one or more than one record from table based on certain
filter conditions.
It contains mainly two parameters :-
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Implementation :-
Step 1 :- Create a OData project in SEGW transaction code.
Step 2 :- Right click on Data model → Import → DDIC Structure and create entity type and
entity set.
Step 3 :- Click on generate button to generate the runtime artifacts.
Step 4 :- Just Go to DPC Extension class and redefine the below methods and follows the
below steps.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
5. Get Entity Set :-
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
2. Get Entity Method :-
in HTTP Request section. Select the POST radio button and click
on execute.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
4. Update Entity :-
Select the Put radio button to update any entity in OData services .
5. Delete Entity :-
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Performing CRUD Operations in OData Services 8
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Data Model
1. Data Model
BAPI is just a function ( a piece or set of code ) which is ready made and we can use it.
Data Model 1
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
BAPI_EPM_*
2. following BAPIs
1. for Business Partner
BAPI_EPM_BP_CREATE
BAPI_EPM_BP_GET_LIST
BAPI_EPM_BP_GET_DETAILS
BAPI_EPM_BP_CHANGE
BAPI_EPM_BP_DELETE
2. for Products
BAPI_EPM_PRODUCT_GET_DETAILS
BAPI_EPM_PRODUCT_GET_LIST
BAPI_EPM_PRODUCT_CHANGE
BAPI_EPM_PRODUCT_CREATE
BAPI_EPM_PRODUCT_DELETE
Note
Data Model 2
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Implementing OData Service for
Our Project {GET}
1. Defining Entity and Entity Set for Products
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Click on save and assign the TR.
Step 2 :- Right Click on the Data Model and give a entity name and the structure for our
data.
Step 2 :- Click on Next and select the fields that you want to see.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 3 Click on next and select the primary key and click on the finish button.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 4 :- Now go to the properties and select the create, update, nullable, filtering for the
columns.
Click on save, then click on ProductSet and allow the creation, update, deletion etc.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 5 :- Click on Generate button to generate all the runtime classes which are mandatory
for our system.
Note
Write all the manual logic in the MPC_EXT class and DPC_EXT class, because they
are only generated once.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Assign the package and transport request and our service registration is done.
Output
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 3 :- We will create a internal table for BAPI_EPM_PRODUCT_HEADER structure.
Step 4 :- We will use the BAPI BAPI_EPM_PRODUCT_GET_LIST to get all the data from
the BAPI.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
TABLES
HEADERDATA LT_BAPI_PRODUCTS
* SELPARAMPRODUCTID =
* SELPARAMSUPPLIERNAMES =
* SELPARAMCATEGORIES =
* RETURN =
.
Step 5 :- Then we will pass the data from internal to exporting parameter.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Output
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
4. Implementing $top and $skip in Get Entity Set
Method
At this moment, if you will go and check &$top=1, then it will result you all the values.
We have a IS_PAGING parameter which receives the input given $top and $skip and
based on that we can implement our logic.
Step 1 :- We will create there variables, for top and skip and then for total also.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 2 :- Then we will pass the paging parameters into it.
LV_TOP IS_PAGINGTOP.
LV_SKIP IS_PAGINGSKIP.
LV_TOTAL LV_TOP LV_SKIP.
Step 3 :- Now for our BAPI, we will define a importing parameters for us and we will pass
our LV_TOTAL into that parameter.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 4 :- We will declare a work area for our ET_ENTITYSET parameter.
Step 5 :- Now, we will go for a check before passing the data to the ET_ENTITYSET
parameter.
IF LV_TOTAL 0.
LOOP AT LT_BAPI_PRODUCTS into data(ls_BAPI_PRODUCTS FROM LV_S
MOVECORRESPONDING LS_BAPI_PRODUCTS to LS_ENTITY.
APPEND LS_ENTITY to ET_ENTITYSET.
ENDLOOP.
ELSE.
ET_ENTITYSET CORRESPONDING # LT_BAPI_PRODUCTS .
ENDIF.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Output
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 1 :- We will define our internal table for category filter.
ENDIF.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Output
Pass the URL in the browser.
Press enter and you will get the filtered categories based on the given input.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
6. Implementing Get Entity Method
Step 1 :- Go to the DPC Extension class and redefine the Get Entity method.
Step 2 :- Read the input data given by user for the Employee Id.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 3 :- Define the importing parameter from the BAPI
BAPI_EPM_PRODUCT_GET_DETAIL and call the BAPI and pass the importing and
exporting parameter.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
MOVECORRESPONDING LS_HEADER TO ER_ENTITY.
Output
IF LV_PRODUCT_ID is INITIAL.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
ENDIF.
Press enter and pass some text that you want to display.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
* ENTITY_TYPE =
* MESSAGE =
MESSAGE_UNLIMITED | Hello Amrit, this Product Id do not exist
* FILTER_PARAM =
* OPERATION_NO =
.
Output
Now, if we pass an empty string in get entity call then we will get this error
message.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
8. Handling the Wrong Input in Get Entity
IF we will pass some wrong value to our BAPI, then BAPI will return us a message in its
return parameter.
Step 1 :- Define a internal table of type BAPIRET2 and pass it into the BAPI.
Step 2 :- We will check this internal table and if it is not empty we will raise the exception.
MEMO_CONTEXTGET_MESSAGE_CONTAINER ADD_MESSAGES_FROM
EXPORTING
IT_BAPI_MESSAGES LT_RETURN .
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Output
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
POST, PUT, DELETE in ODATA
For POST request we are required to send the payload.
Payload
Payloads are typically the data that we are sending and mostly they are in JSON format.
We will go to the DPC Extension class and we will implement the POST request for the
product.
We will receive the data via IO_DATA_PROVIDER provider importing parameter and then
we will return the value in ER_ENTITY parameter.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
*&Step 4 Insert the data using BAPI
*&Step 5 Prepare the response and send back
Step 3 :- Define the work area for BAPI input and store the input into work area.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
MOVECORRESPONDING LS_ENTITY to LS_HEADER.
CALL FUNCTION 'BAPI_EPM_PRODUCT_CREATE'
EXPORTING
HEADERDATA LS_HEADER " EPM Product header data of
* PERSIST_TO_DB ABAP_TRUE " Flag: Save data to DB (yes/no)
TABLES
* CONVERSION_FACTORS " EPM Product conversion factor da
RETURN LT_RETURN " Return Parameter
.
Step 4 :- We will check for the exception and we will pass the return of BAPI to exception.
ENDIF.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 5 :- Now we will do the move corresponding to pass the data to final ER_ENTITY
output.
Step 6 :- Now if we will try to do the POST operation, we will get a error, so to resolve that
we are required to pass a unit of measure also into the BAPI.
LS_HEADERMEASURE_UNIT 'EA'.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Now our POST operation is ready we can directly use it.
method PRODUCTSET_CREATE_ENTITY.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
raise EXCEPTION type /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
MESSAGE_CONTAINER me→mo_context→get_message_container(
ENDIF.
endmethod.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 1 :- Redefine the Update Entity method.
Step 2 :- Define the work area and receive the input values from IO_DATA_PROVIDER
parameter.
IO_DATA_PROVIDERREAD_ENTRY_DATA
IMPORTING
ES_DATA LS_ENTITY .
Step 3 :- Then we will define the importing parameter for our BAPI and we will pass its
value.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
MOVECORRESPONDING LS_ENTITY to LS_HEADER.
LS_HEADER_XNAME ABAP_TRUE.
LS_HEADER_XDESCRIPTION ABAP_TRUE.
LS_HEADER_XPRICE ABAP_TRUE.
LS_HEADER_XCURRENCY_CODE ABAP_TRUE.
CALL FUNCTION 'BAPI_EPM_PRODUCT_CHANGE'
EXPORTING
PRODUCT_ID LV_PRODUCT_ID " EPM Product Id
HEADERDATA LS_HEADER " EPM Product header data of
HEADERDATAX LS_HEADER_X " EPM Product header mod
* PERSIST_TO_DB ABAP_TRUE " Flag: Save data to DB (yes/no)
* TABLES
* CONVERSION_FACTORS " EPM Product conversion factor d
* CONVERSION_FACTORSX " EPM Prod. conv. fact. modif. data
* RETURN = " Return Parameter
.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
Step 5 :- Define the return parameter for BAPI.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
ENDIF.
method PRODUCTSET_UPDATE_ENTITY.
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
LV_PRODUCT_ID LS_KEYVALUE.
IO_DATA_PROVIDERREAD_ENTRY_DATA
IMPORTING
ES_DATA LS_ENTITY .
LS_HEADER_XPRODUCT_ID LV_PRODUCT_ID.
LS_HEADER_XNAME ABAP_TRUE.
LS_HEADER_XDESCRIPTION ABAP_TRUE.
LS_HEADER_XPRICE ABAP_TRUE.
LS_HEADER_XCURRENCY_CODE ABAP_TRUE.
DATA LT_RETURN TYPE TABLE OF BAPIRET2.
CALL FUNCTION 'BAPI_EPM_PRODUCT_CHANGE'
EXPORTING
PRODUCT_ID LV_PRODUCT_ID " EPM Product Id
HEADERDATA LS_HEADER " EPM Product header data of
HEADERDATAX LS_HEADER_X " EPM Product header mod
* PERSIST_TO_DB ABAP_TRUE " Flag: Save data to DB (yes/no)
TABLES
* CONVERSION_FACTORS " EPM Product conversion factor d
* CONVERSION_FACTORSX " EPM Prod. conv. fact. modif. data
RETURN LT_RETURN " Return Parameter
.
IF LT_RETURN is NOT INITIAL.
MEMO_CONTEXTGET_MESSAGE_CONTAINER ADD_MESSAGES_FRO
EXPORTING
IT_BAPI_MESSAGES LT_RETURN .
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
MESSAGE_CONTAINER MEMO_CONTEXTGET_MESSAGE_CONTA
.
ENDIF.
Output
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
IF LT_RETURN IS NOT INITIAL.
MEMO_CONTEXTGET_MESSAGE_CONTAINER ADD_MESSAGES_FROM_
EXPORTING
IT_BAPI_MESSAGES LT_RETURN .
RAISE EXCEPTION TYPE /IWBEP/CX_MGW_BUSI_EXCEPTION
EXPORTING
MESSAGE_CONTAINER MEMO_CONTEXTGET_MESSAGE_CONTAINE
.
ENDIF.
endmethod.
Output
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944
POST, PUT, DELETE in ODATA 14
Amrit Raj (Chief Operations Officer) | Srijan Institute & Training Center | +919220601944