Creating an OData Service with SAP

Creating an OData service with SAP is not complicated. The first step is to determine what data you want to work with. That is based on your application. For our purposes, we will use the sample data that is included with SAP.  This is the airport related data. If you do not have this data in your system, then please follow the tutorial located at http://help.sap.com/saphelp_erp60_sp/helpdata/en/db/7c623cf568896be10000000a11405a/content.htm.  The main way to get this data into your system is to run t-code SAPBC_DATA_GENERATOR.  It may be useful to you as well to review the Flight data model while you are designing your application. You can find the data model at https://help.sap.com/saphelp_nw70/helpdata/en/cf/21f304446011d189700000e8322d00/content.htm.

In short, here are a list of the main tables in the Flight data model:

T000 – Client table
SCURX –  Currencies (key: currency key)
SBUSPART – Business partner (key: client, partner number)
STRAVELAG – Travel agencies (key: client, travel agency number)
SCUSTOM – Customers (key: client, customer number)
SCARR – Carriers (key: client, carrier ID)
SCOUNTER – Sales counters (key: client, carrier ID, sales counter number)
SPFLI – Flight schedule (key: client, carrier ID, connection number)
SFLIGHT – Flights (key: client, carrier ID, connection number, date of flight)
SBOOK – Flight bookings (key: client, carrier ID, connection number, date of flight, booking number, customer number)

Let’s consider the customers table (SCUSTOM).

customers-screenshot

In this table we have basic data such as customer id, name, title and address information. This is a simple and straightforward set of data.

We then need to model this data. This is done through using t-code – SEGW. If you run it for the first time, it will take a moment to compile. You are then presented with the SAP NetWeaver Gateway Service Builder.

netweaver-service-gateway-builder

Click the new button to create a new project. Before creating this I created a package called ‘ZODATA’ that I could use to assign this project to. You can create a new package using SE80. Then you should see a screen like this when you are done creating your package.

gateway-service-overview

Here is where we will model our data using different modeling types. Next, click on Data Model > Import from DDIC structure.  Once you go through the setup, you will see this screen. That was super easy, wasn’t it.

odata-scustom-data-model

Next, then you have to create an entity set representing your customers object. You do this under the EntitySet area below. Your screen should look like this:

odata-customers-entity-set

Next, we are going to generate runtime objcts for our application. This action creates the code that runs behind the scenes. Right click on the project at the top and choose “Generate Runtime”.

generate-entity-set

After generating the runtime you will see this screen:

odata-generate-runtime

Hopefully this is all starting to make sense at this point. The system is modeling the data and then also generating the classes to handle the mapping of the data and updating as well. This is similar to what .Net does through some of its wizards. If all goes well after you pick your package, you will see the following:

odata-runtime-success

That will provide the basic architecture for the underlying service. However, in order to tie it all together, we need to update the generated classes that were created. We need to modify the ZCL_ZODATADEMO_DCP_EXT class. Using the runtime generator saves a lot of coding time but it doesn’t completely eliminate it in all circumstances. Just take a look at all of the methods that were generated in this class:

odata-generated-class

There were a ton more that I did not list. It makes it a bit easier if we just look at the methods on the object through this navigation:

odata-entity-methods

So now looking into these methods, if we pick the first one, CUSTOMERSET_CREATE_ENTITY, we can see it doesn’t have an implementation yet.

 

odata-customer-entity-not-implemented

We now have to implement the code to handle this. We do that with the following code:

Then let’s change the get entity method:

We won’t be able to see this service until we register the service. We do this with the t-code /IWFND/MAINT_SERVICE. When you run this, choose the system alias of ‘LOCAL’. I had to do that to get my service to show. Once you do, you will see this screen:

odata-service-register-screen

Next, click on the service and then you will see the following screen:

odata-service-register-screen

You will see the following screen:

odata-add-service-confirmation

Then we are pretty much done. Then if we go back to /IWFND/MAINT_SERVICE we can see our service listed at the bottom:

odata-service-success

And now we are ready to test the service. We can do that by going to this url: http://<server>:<port>/sap/opu/odata/sap/ZODATADEMO_SRV/?sap-client=001&$format=json.