In this first part of tutorial, I am going to show how to prepare a data model using SEGW transaction. If you got stuck, you can try to look at this official document that describes the process in more details.
Steps needed to create your first oData service
1. You need to prepare a database table that will be used for storing data. Below you can see the specific database table which is used in this example.
2. You need to run the transaction (t-code) SEGW and create your first project.
3. Click on Data Model > Import from DDIC structure. Follow all the steps using your created database table. At the end you should be able to see following table. Then select relevant keys and finish this process.
4. Creatable, updatable, deletable and addressable parameters need to be checked for your entity.
5. Now, you need to generate a runtime object. Select a property panel on your project by right click and choose Generate runtime action.
6. In Service implementation > UserSet > GetEntitySet click by right and choose Go to ABAP Workbench.
7. The main goal now is to redefine relevant methods in USERS_DPC_EXT class. See content of redefined methods below.
Implementation of redefined methods
USERSET_CREATE_ENTITY – CREATE odata method
1 2 3 4 5 6 7 8 |
METHOD userset_create_entity. DATA: ls_user TYPE zsa_users. io_data_provider->read_entry_data( IMPORTING es_data = ls_user ). INSERT INTO zsa_users VALUES ls_user. er_entity = ls_user. ENDMETHOD. |
USERSET_GET_ENTITY – GET odata method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
METHOD userset_get_entity. DATA: lt_keys TYPE /iwbep/t_mgw_tech_pairs, ls_key TYPE /iwbep/s_mgw_tech_pair, lv_email TYPE char100, ls_user TYPE zsa_users. lt_keys = io_tech_request_context->get_keys( ). READ TABLE lt_keys WITH KEY name = 'EMAIL' INTO ls_key. lv_email = ls_key-value. SELECT SINGLE * FROM zsa_users INTO CORRESPONDING FIELDS OF ls_user WHERE email = lv_email. er_entity = ls_user. ENDMETHOD. |
USERSET_GET_ENTITYSET – GET odata method
1 2 3 4 5 6 |
METHOD userset_get_entityset. DATA lt_users TYPE TABLE OF zsa_users. SELECT * FROM zsa_users INTO TABLE lt_users. et_entityset = lt_users. ENDMETHOD. |
USERSET_UPDATE_ENTITY – PUT odata method
1 2 3 4 5 6 7 8 9 10 11 12 13 |
METHOD userset_update_entity. DATA: ls_user TYPE zsa_users. io_data_provider->read_entry_data( IMPORTING es_data = ls_user ). UPDATE zsa_users SET firstname = ls_user-firstname lastname = ls_user-lastname age = ls_user-age address = ls_user-address WHERE email = ls_user-email. er_entity = ls_user. ENDMETHOD. |
USERSET_DELETE_ENTITY – DELETE odata method
1 2 3 4 5 6 7 8 9 10 11 |
METHOD userset_delete_entity. DATA: lt_keys TYPE /iwbep/t_mgw_tech_pairs, ls_key TYPE /iwbep/s_mgw_tech_pair, lv_email TYPE char100. lt_keys = io_tech_request_context->get_keys( ). READ TABLE lt_keys WITH KEY name = 'EMAIL' INTO ls_key. lv_email = ls_key-value. DELETE FROM zsa_users WHERE email = lv_email. ENDMETHOD. |
Testing your oData service
There is a gateway client which can be used for testing running services. Just run transaction(t-code) /IWFND/GW_CLIENT and you can find out, if your service runs correctly.
Your oData service is running, now you can start to consume your data!
If you miss anything in the article, let me know in the comments.
Thanks Peter, this is exactly what I’m looking for ! I followed each step carefully twice, but when I test things out in /IWFND/GW_CLIENT, I keep getting 403 as HTTP response, “No service found for namespace , name ZWJ_USERS_SRV, version 0001”, ZWJ_USERS_SRV is the technical service name, and /sap/opu/odata/sap/ZWJ_USERS_SRV is the URI requested.
Another information, after I completed all the steps described here, I went to /IWFND/MAINT_SERVICE – Activate and Maintain Services, and I couldn’t find ZWJ_USERS_SRV in the service catalog, was I missing anything?
I’m still very new to this, thanks for your patient and help 🙂
Found the fix, you will need to go to /IWFND/MAINT_SERVICE to add the service 🙂
HI,
As I am beginner to SAP. I am learning SAPUI 5 and I had setup the environment for developing SAPUi 5 front end using Eclipse.
But I need to write my own oData service and consume that service in my practice application.
For this purpose , What is the environment ( i mean backend ) do I need ? and also please suggest whether their is any free environment for practice?
Please guide me on this.
Hi Peter,
thank you so much….. Really good one for learning OData Services…
thank u peter
thank u peter