Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
sanjanasatyan
Product and Topic Expert
Product and Topic Expert
0 Kudos
1,394
  • SAP Managed Tags:

Hi Reader,

In this blog post, I will explain the steps to create an OData service for custom tables. For first-time ABAP developers, creating a service can be challenging, so I have simplified the steps and included some tips to make the process easier. Happy reading! 📄

Step 1: Enter the transaction code - SEGWSAP Gateway Service Builder (transaction SEGW) is a design-time environment, which provides developers an easy-to-use set of tools for creating services. The Code-based OData Channel consumes it and supports developers throughout the development life cycle of a service.
sanjanasatyan_2-1726643426327.png
Step 2: In the SAP Gateway Service Builder, click on the highlighted 'Create' icon. The "Create Project" pop-up window will appear. Fill in the required details; I have attached a sample screenshot for reference.
Here are some tips to keep in mind:

  • The name of the project should start with 'Z' if you are trying out a sample. For actual services, please maintain the naming conventions according to your project.
  • Use the package name $TMP if you are creating local objects that will never be transported to any other system. Otherwise, maintain the name of the package you have created.

After filling in the details, click on the check icon or press Enter.sanjanasatyan_5-1726644527586.png

Upon clicking the check icon, the folder structure in the project will appear as shown below. 

sanjanasatyan_6-1726645729674.png
In the Data Model folder, you will see three subfolders:
Entity Type: Defines the schema of a single data object, including its properties (attributes), data types, and constraints. It is like a work area and resembles a structure or table type that we create in our ABAP reports. 
Associations: This defines the relationship of how different entity types are linked, such as one-to-one, one-to-many, or many-to-many relationships.
Entity Sets: Signifies a collection of related entity type instances, similar to a table in a relational database. It operates as an internal table, representing a table created in our reports by referencing the structure. 

Service Implementation:
This section outlines the services that can be performed with the entities. CRUDQ operations [Create, Read, Update, Delete, Query]

Runtime Artifacts: This node contains the following sections:

  • DPC (Data Provider Class): is responsible for implementing the business logic required to access and manipulate data. It serves as the backend interface between the OData service and the underlying data source (like a database).
  • DPC_EXT (DPC Extension): The DPC is extended here, and all custom logic is added in the DPC_EXT section.
  • MPC (Model Provider Class): defines the structure of the OData service by specifying entity types, entity sets, and associations, thereby providing the necessary metadata for clients consuming the service
  • MPC_EXT (MPC Extension): allows for the definition of extra entity types or properties. This separation facilitates easier maintenance and flexibility, ensuring that customizations can be made in a clean and manageable manner.
  • MDL: focuses on defining the data model, specifying how entities and their relationships are structured
  • SRV: refers to the service definition for an OData service, encapsulating the functionality that the service exposes to clients.  his service name will be utilized when accessing the project through Fiori or UI5 applications.

Service Maintenance: this allows to manage the various aspects of the OData services. We can activate or deactivate services, handle versioning for backward compatibility, and control how metadata is exposed to clients. It also lets us set up access controls for authorization and monitor the usage and performance of services. This section helps ensure that your OData services are efficiently maintained and secure.

Step 3: Now we will define the structure of the work area and internal table. Right-click on the Data Model and select "Import," and then choose the "DDIC Structure."

sanjanasatyan_9-1726648636823.png

On selecting the DDIC Structure, we get the below pop-up. Here we can define any name and select an ABAP Structure which you have created based on the Custom Table, and click on Next

sanjanasatyan_10-1726649416298.png

On the next page of the wizard, select the fields you want from the custom structure, and then click the 'Next Button'

sanjanasatyan_11-1726649914036.png

The selected fields will appear on the last page of the wizard. The "Is Key" column allows you to select any field that should be designated as a primary key. Once you have made your selections, you can click the 'Finish' button.sanjanasatyan_12-1726650238702.png

Step 4: Click on the 'Save' button at the top of the menu bar. Next, click on the 'Generate Runtime Objects' icon. A pop-up window will appear with class details; click on the checkmark icon to proceed. It will then prompt you for a package; enter the details and continue. 

sanjanasatyan_13-1726650590625.png

This step will generate all the runtime artifact classes, as shown here.

sanjanasatyan_14-1726651071268.png

Step 5: Enter the transaction code /o/IWFND/MAINT_SERVICE, this is used in the SAP Gateway framework, specifically for managing and maintaining OData services. Click on the 'Add services' button. 

sanjanasatyan_16-1726651641187.png

The Add selected service screen will appear, here enter an alias name and execute. Search for your project under the select backend services.sanjanasatyan_0-1726735755225.png

Select your project name and double-click it. A pop-up window will appear; enter the package details and then click the check icon. A confirmation message will indicate that the service has been successfully created and the metadata has been loaded.

Now click the Back button and return to the main screen of the transaction /IWFND/MAINT_SERVICE and locate your service. Then, click on the 'SAP Gateway Client' button.

sanjanasatyan_1-1726736748749.png

A new screen will appear, here Execute the transaction and check the response for Status Code=200 .

sanjanasatyan_2-1726737199586.png
Step 6: Once we have established the connection, we can write the code based on our requirements. Go to the transaction code /O/SEGW and expand the folder Service Implementation. We can make
 the necessary code changes based on the CRUD operations, select the 'Go to ABAP Workbench' option as seen here.sanjanasatyan_15-1726651311061.png

In the next screen, as shown below, expand the method dropdown and select the method for GetEntitySet. Right-click and select "Redefine." Write the necessary code in the method, then save and activate all the related objects for the classes. Similarly, write code for GetEntitySet for the Item and activate it.

sanjanasatyan_4-1726738112895.png

Step 7: Go to the transaction /O/IWFND/MAINT_SERVICE, find your service, select it, and click on  the 'Load Metadata'.sanjanasatyan_3-1726737423144.png

An information message will pop up stating that the metadata has been loaded successfully. Now, click on the SAP Gateway Client and select the entity set. Choose the entity and enter the required details, and click 'Execute' You will receive the data in response. You can also add filters based on your requirements.

Hope this was informative 😊

If you have any further queries, please feel free to drop a comment below.
You can also refer to OData for more information. 

Best Wishes,
Sanjana Satyan

 

 

1 Comment