Wednesday, 9 October 2019

Creation of Odata services for beginners

In this series of Odata and UI5 blog posts I will be sharing my development experiences, challenges and different tips and tricks which I learned through a lot of internet searches.

My only motto will be to save time and efforts of many developers who are searching for a solution for their problem, which has either already appeared/solved by someone else.

If you have any question/query regarding this blog post or anything related to Odata, ABAP, JAVA, Android or any other technology, post a comment I will try to answer it.

Let’s start Odata creation now –

Steps to create first Odata Service –

1. Open T-Code SEGW Click on create a new project.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Provide the name of your project and a project with the following structure gets created.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Data Model: It contains three subfolders.

Entity types: Entity types are like ABAP structures. We add all fields which are required in Entity Types.

Associations: We will come to this later.

Entity Sets: This is similar to ABAP internal tables, used to hold data for our Odata services. Now we will create a new entity type and add fields to this entity.

Right-click on entity type folder and choose to create. Provide the name of the entity type in the popup.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Select checkbox “Create Related Entity Set”, this will create a related entity set (internal table).

Unselect this checkbox only when you are sure that we won’t be requiring an entity set (internal table) in our Odata.

Two new subfolders are created under our entity type. Properties and Navigation.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Double click on properties, here we add the fields to our entity type. Click on below button and create, add and delete new fields as per requirement.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Now you must be wondering when we are developing any OData service in our project, are we going to add fields in entity types one by one. The answer is NO.

We have the option to import structures from DDIC, File, RFC or search help. Instead of having to add fields one by one we will import the required structure from DDIC for the scope of this blog.

Let’s create our Entity type again, this time we will choose the import option.

Now we have a pop-up with 3 steps as below.

Step1:

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Provide name of the entity type and the structure from which we are importing fields.

Click on Next.

Step 2: In this pop up we can see EKKO structure and all of its fields.

We can now select fields which are required in our service and click next.

Step 3:

In the 3rd screen of the wizard all the selected fields are displayed.

Now we have to choose the key fields for this structure. This is same as choosing the primary key for our table.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

I choose EBELN as key and click Finish.

Now our entity type POHeader has been created.

Now have a look at our project structure.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

We have our entity structure under Properties of the entity type.

And under the service maintenance folder, we have all our CRUD operations.

Now let’s create one more entity type following the same procedure, this time we will create POItem from EKPO.

Now if you will go through the project structure you will find that each entity type has separate Properties folder and each entity set has its own CRUD operation under service maintenance.

Now, we have Purchase Order header and Purchase Order Item entity type i.e, structure and entity set i.e, internal table ready.

So let’s generate and register our service. Click on generate button and below pop up comes.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

These are our generated classes, in which we will write our custom code to fetch data according to our requirement.

Let’s skip that for now and click OK.

Check for successful generation of service and model.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Now our Odata Service has been created successfully.

Now we need to register this service so that other application can use this service.

For this first, we will have to check our system configuration for embedded or hub deployment.

In case of embedded deployment, front end server (ex: FIORI server) and backend ABAP server are on the same system.

And in the hub system, they are on a different system.

We register our Odata in the system where it will be used, so in embedded it will the same system and in the hub, there will be another system.

In embedded system Go to your Gateway Hub (Front-end system) and execute t-code /n/IWFND/MAINT_SERVICE. Click on Add services:

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Provide system alias name (if you do not know the system alias name please check from SPRO) and click on Get Services

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Now all the services which are available in the given system will be displayed here.

You can provide the service name in External Service Name to directly go to your service.

Now select your service and click on Add selected Services

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Provide package name or choose local on next screen based on your transport requirement and click OK

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Your service has now been created.

Now if you will go back to service catalog screen you will find your service has been added there.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Now click on SAP Gateway Client after selecting your service to go to gateway client and actually test your service.

Click on execute button and you can see your first created Odata in action.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Note: If you do not get status 200, check status number
403 – Forbidden
500 – Internal Server Error
Let me know in comments and we will try to resolve them.

If you want to see your output in JSON, just $format=xml to $format=json.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

PS: How to check if you get any errors while executing Odata.

Go to /IWFND/ERROR_LOG and here you will find a detailed description of the error, google them and try to resolve. Or post them in the comment below and we will have a look together.

Now the last part: We only got the structure of our Odata, you must be thinking about how do we fetch data from our database tables.

Now, let’s go to the part which we skipped earlier, to our auto-generated MPC and DPC classes. Now go to Service Implementation folder on created service.

Right-click on GetEntitySet and Goto ABAP Workbench. We will only edit classes which have _EXT at the end, not the base classes, this is because base classes get overwritten whenever you regenerate the service.

ABAP Connectivity, ABAP Development, SAP ABAP Study Materials, SAP ABAP Tutorials and Materials, SAP ABAP Guides

Now click on Entity type Get EntitySet Method and redefine it.

Write below code in your method and activate.

SELECT *  from ekko into CORRESPONDING FIELDS OF TABLE et_entityset up to 5 rows. 

Now again run your service from now you should have the data.

We have our first Odata created and we fetched data from the database table.

No comments:

Post a Comment