Tutorial Odata Services in SAP ABAP

Tutorial Odata services in SAP ABAP

Go to SEGW TCODE:
Once you been there, click on New

SEGW SAP TCODE

Then in the new window fill the information as i have and then you can save it as a local or in a transport request, I will save as a local.

New Odata Project

Now we will create the Entity Type.

In SAP Gateway, within the OData modeler (SEGW transaction), creating an Entity Type is one of the first fundamental steps to define the data model that will be exposed through the OData service.

What is an Entity Type in SAP OData?

In the SEGW transaction (SAP Gateway Service Builder), an Entity Type represents the structure of a business object — similar to a structure or table in ABAP. It defines the properties (fields) that make up one record in your OData service.

Why do you need to create it?

  1. To define the data structure you want to expose (e.g., a customer, a sales order, a product).
  2. It serves as the foundation for:
    • Entity Sets (collections of those entities, like rows in a table)
    • Associations (relationships between entity types)
  3. So that the OData service knows what fields to return to the frontend and how the data is organized.

Practical example:

Let’s say you want to expose customer data from SAP.

You would create an Entity Type called ZCustomer with the following properties:

  • KUNNR – Customer ID
  • NAME1 – Customer Name
  • ORT01 – City
  • LAND1 – Country

Then, you would create an Entity Set named CustomersSet, which represents a list of ZCustomer entities (like a SELECT * FROM KNA1).

Ten, right click on Data Model > Create > Entity Type

Entity type

Click on OK.

What is an Entity Set in OData?

An Entity Set represents a collection of records (rows) of a given Entity Type (structure).
So:

  • The Entity Type defines what a single record looks like (like a row in a table).
  • The Entity Set is the collection of those records — it’s what you query in the OData URL.

Why do you need to create a related Entity Set?

If you don’t create the Entity Set:

  • You can’t expose the data via OData properly.
  • You won’t be able to call the URL like this:

✅ What happens if you do not check "Create Related Entity Set"?

If you don’t check that box, you’re only creating the Entity Type, which defines the structure of a single record — for example, one material description.

But you won’t be able to retrieve a collection of records (multiple entries) because the Entity Set is missing.

So what does that mean?

Without the Entity Set:

  • Your OData service cannot support queries like GET_ENTITYSET
  • You can only implement GET_ENTITY, which returns a single record by key
  • You won’t have a URL like:

    /sap/opu/odata/sap/ZYOUR_SERVICE_SRV/ety_getMaterialSet

Summary

Option CheckedResult
✅ YesYou can fetch a collection of records (Entity Set)
❌ NoYou can only fetch a single record (Entity)

Click on OK

Then in ABAP Structure put: MAKT

Why do you enter the ABAP Structure (e.g., MAKT) here in the Entity Type?

In the SAP Gateway Service Builder (SEGW), when creating an Entity Type, specifying an ABAP structure or table like MAKT serves a very important purpose:

Reason: To Automatically Import Fields

When you provide an ABAP structure (like MAKT), the system can:

  1. 🔄 Auto-generate the properties (fields) of the Entity Type based on that structure or table.
  2. 🧱 Ensure that the field types, lengths, and domains match the SAP Data Dictionary (DDIC).
  3. ⏱️ Save time – you don’t need to manually define each property like MATNR, SPRAS, MAKTX, etc.

Then, on left bar, click expand ety_getMaterial and double click on Properties.

Here you have to do:

  • Add 2 rows
  • Named: material and description
  • Check Is Key for the first row
  • Check Filtrable for first row
  • Add Labels
  • Edm Type: edm.string

Your screen should looks as below:

Now on the ABAP Field column open the match-code and select properly name, for this example, MATNR and MAKTX.

Then, on Max Length Column, add a length of 40 for each field, after that, save and check and should looks like below:

If everything is good, now, click on Generate Services Button and then Click on OK

Now the systen will ask for a Transport request, select Local Object, or if you want to transpot this you can select a a Package and transport request.

On left side, expand Service implementation folder, select GetEntity Right Click > Go to ABAP Workbench. Click OK on the message window.

Then SAP will show you a Class called ZCL_ZMM_ODATA_TEST_DPC_EXT or something similar on left side, expand Methods and double click on ETY_GETMATERIALS_GET_ENTITY.

Right Click on ETY_GETMATERIALS_GET_ENTITY and Click on Redefine.

Then write follow code (and understand), also forget the color of my ABAP editor, in your case should be withe, I use this configuration to help my eyes...

When you finish Click on: Save > Check > Activate > OK.

Once the program get activated, press Back 3 times until you arrive to SEGW tcode, once you arrive, copy the service name and paste in a noteapad, in this case: ZMM_ODATA_TEST_SRV

Then go to Tcode: /IWFND/MAINT_SERVICE

Publish service

Now we have publish our Odata service, for that you have to go to Tcode: /IWFND/MAINT_SERVICE
Click on Add Service.

On the follow Screen fill follow fields:
System Alias: LOCAL
Technical service name: ZMM_ODATA_TEST_SRV
When service appears, select it and click on Add Selected Services

add selection odata service

On the next screen write your package, or Click on Local Object button:

Will appears a windows that will say the services was added successfully, click OK and then Click on Back button to return to main windows of : /IWFND/MAINT_SERVICE

Filter by your servies name and Click OK.

Click on metadata

Testing our ODATA Service

On the same transaction (/IWFND/MAINT_SERVICE) we can select our service and Click onn SAP Gateway Cliente

Test Odata service ABAP

On the next screen, Click on EntitySets and Select your EntityName wich is the same that we created on SEGW transaction:

Now we need to past a parameter in this case a Material number, in my case i will pass this material:

Material number MAKT


Then i will pass the material: ('000000000000002018')

Then we will select the format to display the information, click on Add URI Option button just beside Entity Set Button and select JSON format:

Then press Execute button to invoke the Odata Service and you will fin the result:

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up