Creating an OData services in SAP system for Custom Tables

Estimated read time 12 min read

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.
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.

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


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 serviceMPC_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 structuredSRV: 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.”

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

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

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.

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. 

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

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. 

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

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.

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


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.

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.

Step 7: Go to the transaction /O/IWFND/MAINT_SERVICE, find your service, select it, and click on  the ‘Load Metadata’.

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

 

 

 

​ 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 – SEGW. SAP 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.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.Upon clicking the check icon, the folder structure in the project will appear as shown below. 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 serviceMPC_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 structuredSRV: 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.”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 NextOn the next page of the wizard, select the fields you want from the custom structure, and then click the ‘Next Button’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.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. This step will generate all the runtime artifact classes, as shown here.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. The Add selected service screen will appear, here enter an alias name and execute. Search for your project under the select backend services.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.A new screen will appear, here Execute the transaction and check the response for Status Code=200 .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.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.Step 7: Go to the transaction /O/IWFND/MAINT_SERVICE, find your service, select it, and click on  the ‘Load Metadata’.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     Read More Technology Blogs by SAP articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author