You are on page 1of 24

volker.

drees: Step-by-step guide to build


an OData Service based on RFCs Part
1
Posted by Volker Drees Oct 26, 2012
Hello fellow OData Service developers out there.
When it comes to OData Service development with SAP NetWeaver Gateway a very typical use
case is to build a service that is based on an RFC module (or a set of RFC modules) e.g. for a
List / Detail application. For such a scenario there are dozens of RFC modules (BAPIs) available
in the SAP standard that can be used. And of course you can also build and consume your own
(Z-)RFC modules if the existing modules dont satisfy your needs...
For additional information regarding the Service Builder please also see Thomas Meigens Blog: The new SAP
NetWeaver Gateway Service Builder: Build new OData Services in 3 Quick Steps
In this Step-by-step Guide we will build an OData Service that consumes two RFC modules from the EPM
(Enterprise Procurement Model) demo content. From this EPM demo content we pick two BAPIs that retrieve
the list and detail of Sales Orders / Line Items. The data model is pretty simple. We have one entity for the
Sales Order header data, and one entity for the Sales Order Line Item.
The two BAPIs that we will use in our service are: BAPI_EPM_SO_GET_LIST and
BAPI_EPM_SO_GET_DETAIL
This guide is based on SAP NetWeaver Gateway 2.0 SP5. We will be mainly using the SAP
NetWeaver Gateway Service Builder for the service creation. The Service Builder allows us to build
our service without writing a single line of code (although you still can do coding if you want).
In this first part (1) we will build a Meta Model of the service. In the second part (2) we will perform the service
implementation.
So lets get started.
1. Create a new project ZGW100_XX_SO (SO stands for Sales Order). XX is just a placeholder for
a group number and GW100 indicates the course number as the screen shots were taken from
the GW100 training course material that I have created.

Generated by Jive on 2015-11-14+01:00


1

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

2. Create the first entity by importing an RFC interface. For this right-click on Data Model and choose Import ->
RFC/BOR Interface

Generated by Jive on 2015-11-14+01:00


2

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

3. Enter the following values in the wizard and then choose Next:
Entity Type Name

SalesOrder

Target System

Local

Data Source Type

Remote Function Calls

Data Source Name

BAPI_EPM_SO_GET_LIST

Generated by Jive on 2015-11-14+01:00


3

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

4. Expand the SOHEADERDATA node and select the following fields:


SO_ID, NOTE, BUYER_ID, BUYER_NAME, CURRENCY_CODE, GROSS_AMOUNT, NET_AMOUNT,
TAX_AMOUNT
Note that SOHEADERDATA is not selected. Choose Next.

Generated by Jive on 2015-11-14+01:00


4

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

5. In the first line, SO_ID, select the field Is Key and choose Finish:

Generated by Jive on 2015-11-14+01:00


5

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

6. Create the second entity again by importing an RFC interface. Right-click Data Model and choose Import ->
RFC/BOR Interface

Generated by Jive on 2015-11-14+01:00


6

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

7. Enter the following values in the wizard and choose Next:


Entity Type Name

SalesOrderItem

Target System

Local

Data Source Type

Remote Function Calls

Data Source Name

BAPI_EPM_SO_GET_LIST

Generated by Jive on 2015-11-14+01:00


7

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

8. Expand the SOITEMDATA node and select the following fields:


SO_ID, SO_ITEM_POS, PRODUCT_ID, NOTE, CURRENCY_CODE, GROSS_AMOUNT, NET_AMOUNT,
TAX_AMOUNT, QUANTITY, QUANTITY_UNIT
Note that SOITEMDATA is not selected. Choose Next.

Generated by Jive on 2015-11-14+01:00


8

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

9. In the first and second line, SO_ID, SO_ITEM_POS, select the field Is Key and choose Finish:

Generated by Jive on 2015-11-14+01:00


9

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

Now our project has 2 entities one for the Sales Order and one for the Sales Order Line Item. As
a next step we create entity-sets out of these entities.
1. Expand the node Data Model and double-click Entity Sets:

Generated by Jive on 2015-11-14+01:00


10

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

2. Click the Create button twice to add two new lines to the table:

3. Enter the following values:

Generated by Jive on 2015-11-14+01:00


11

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

Name

Entity Type Name

SalesOrderCollection

SalesOrder

SalesOrderItemCollection

SalesOrderItem

4. Choose Save:

Now the basic definition of the Model is done. As a next step we can generate the necessary
runtime artifacts.
1. Choose the Generate pushbutton:

Generated by Jive on 2015-11-14+01:00


12

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

2. Leave the default values and choose Enter:

Please note the Technical Service Name ZGW100_XX_SO_SRV is equal to the External Service Name
required to consume this service later on.
3. Choose Local Object:

Generated by Jive on 2015-11-14+01:00


13

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

4. Verify that the runtime objects have been generated successfully:

Now we can Register and Activate the Service.


1. Double-click Service Maintenance:

Generated by Jive on 2015-11-14+01:00


14

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

2. Select system ZME and choose the Register button. Please note that the entries listed here
depend on the System Alias configuration you have done in the SAP NetWeaver Gateway
Implementation Guide (IMG). In a local deployed environment (Backend and Hub components
deployed on the same box) you might also find LOCAL with the RFC destination NONE here.

3. Confirm the warning message displayed in the popup:

Generated by Jive on 2015-11-14+01:00


15

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

4. Press F4 to select the system alias. Select ERP_EMEA from the input help. Please note that you
wont get any popup if there is only 1 System Alias maintained. As mentioned above, depending
on the deployment method and IMG configuration you possibly see an alias LOCAL instead of
the ones shown in the screen shot:

5. Confirm the Select System Alias popup:

Generated by Jive on 2015-11-14+01:00


16

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

6. Leave the default values and enter $tmp as the package and choose Enter:

The External Service Name is defaulted with the Technical Service Name from the Generation Step.
7. Verify that the service has been registered and activated successfully:

Generated by Jive on 2015-11-14+01:00


17

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

Now we can run our service the first time. Please note that weve only maintained the basic
model data so far. As a consequence we can access the metadata of the service, but not yet any
business data (that will come in part 2).
1. Open a new window, start transaction /IWFND/GW_CLIENT.
2. Enter URI: /sap/opu/odata/sap/ZGW100_XX_SO_SRV/$metadata and choose Execute:

Generated by Jive on 2015-11-14+01:00


18

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

ZGW100_XX_SO_SRV is the External Service Name that was registered before.


So we are done for the moment. We have created a Service Builder Project with two entities and
two entity-sets. We have generated the runtime artifacts and registered and activated our OData
service. In part 2 we will map the data provider to bring life into our OData service.
72307 Views Tags: mobile, sap_netweaver_gateway, gateway, odata, netweaver_gateway, service_builder

Nishant Kumar
Sep 23, 2015 6:20 AM
Great Note on OData Services. Thank you so much.
Kavitha Fredrick in response to Volker Drees on page 19
Jul 15, 2015 8:00 AM
Done..
Thanks
Volker Drees in response to Kavitha Fredrick on page 19
Jul 14, 2015 3:33 PM
Hi Kavitha,
the mentioned buttons are typically greyed out if the project has not been generated. In that case no runtime
artifacts exist which can be used for e.g. registration.
Can you please try to generate the project and check if the buttons are still disabled?
Best Regards, Volker
Kavitha Fredrick
Jul 14, 2015 7:22 AM
Hi Volker,
This is an excellent document. Thank You.
During "Service Maintenance", after I select the system, I see all buttons like "Register,Maintain, Error Log and
Gateway Client" are disabled(greyed out). What cloud be the reason?
Please help. Is that role assignment issue?
Regards,
Kavitha
Archana Bellamkonda
Jun 2, 2015 12:23 AM
Thank you for the blog. It was really helpful to get started.

Generated by Jive on 2015-11-14+01:00


19

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

-Archana
Volker Drees in response to Arshad Ansary on page 20
Jan 23, 2015 5:22 PM
Hi Arshad,
I assume you are on 7.40? Have you tried to use "Add Service" to add the GWSAMPLE_BASIC service if it is
not yet present in the list of registered services?
As per the pagination in a SELECT statement, you can only use the "up to x rows" statement to reduce the
number of items to be read. As a consequence the efficiency is higher if you are browsing the first pages of
your entityset.
In my example I first prepare some helper varaibles (all of type i):
lv_top = io_tech_request_context->get_top( ).
lv_skip = io_tech_request_context->get_skip( ).
IF ( lv_top > 0 ).
lv_maxrows = lv_top + lv_skip.
ENDIF.
And in the SELECT statement I use the lv_maxrows variable to limit the number of records to be read. sydbcnt helps me to skip the records at the beginning (though they still need to be read from the DB). Sorting and
filtering takes place along with the SELECT statement.
SELECT
<...some fields...>
INTO (<...some fields...>)
FROM ( <...some joined tables...> )
UP TO lv_maxrows ROWS
WHERE (lv_where_clause)
ORDER BY (lv_orderby_clause).
IF ( sy-dbcnt > lv_skip ).
APPEND ls_entity TO et_entityset.
ENDIF.
ENDSELECT.
hth
Best Regards, Volker
Arshad Ansary in response to Volker Drees on page 21
Jan 23, 2015 5:07 AM
Hi Volker,

Generated by Jive on 2015-11-14+01:00


20

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

I am in SP10 and I am not able to find your sample service GWSAMPLE_BASIC in /IWFND/MAINT_SERVICE.
Can you post the code of pagination handling in select
if we can limit the records at the select itself(instead of at the DPC classes),we can hugely improve the
performance.
Moreover how the pagination should work along with sort is also a question. Because ideally sort should
happen first and then top skip pagination should happen next.
In our project we are currently getting all records from DB and then doing sorting and pagination after that
which is affecting perfomance
Regards
Arshad
Martin Maruskin in response to Volker Drees on page 21
Jan 14, 2015 7:52 PM
Thanks for clarifying this, much appreciated!
Volker Drees in response to Martin Maruskin on page 21
Jan 14, 2015 4:04 PM
Hello Martin,
the BAPI BAPI_EPM_SO_GET_LIST unfortunately does not expose the parameter IS_PAGING_OPTIONS.
Therefore it is not possbile to use it when calling the BAPI.
In the generated DPC method (...GET_ENTITYSET) the paging is handled out of the box. But this is done
in a generic way - after the BAPI has provided the full list. You can adjust the generated coding to fill the
MAX_ROWS parameter based on the paging request. That can improve the performance when calling the
BAPI.
I'm not aware of any standard BAPI that supports full paging. If you have access to a NW 7.40 system with
SAP_GWFND SP8 installed you can check my sample service (
/sap/opu/odata/IWBEP/GWSAMPLE_BASIC) which implements the paging by performing a direct database
access (SELECT).
Best Regards, Volker
Martin Maruskin
Jan 14, 2015 2:42 PM
Hi Volker,
interesting blog! I have a question whether pagination can be done within your example - BAPI
BAPI_EPM_SO_GET_LIST. I see that within the code of the BAPI the pagination related parameter

Generated by Jive on 2015-11-14+01:00


21

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

(IS_PAGING_OPTIONS) of method QUERY_BY_HEADER are not passed. So I not think it is supported but
maybe you found a way for this...
In case you agree with me that no pagination supported by the above mentioned BAPI... Are you aware of any
other standard SAP BAPI which supports pagination functionality within the result set?
thanks
m./
Volker Drees in response to Arshad Ansary on page 22
Jun 13, 2014 3:49 PM
Hi Arshad,
the reason for not marking SOHEADERDATA was to keep the Entity Type name that you defined on the first
page of the wizard. The blog was created based on GW 2.0 SP5. Now with SP8 (and SP9 coming soon) the
behavior of SEGW has changed a bit. So it's not a problem if the SOITEMDATA or SOHEADERDATA nodes
are marked. You just need to make sure that you get the expected Entity Type name in the end.
Also the Entity-Sets you do not need to add manually anymore. This is done using the "Create Default Entity
Set" check-box automatically.
As per the 2nd error it's more tricky to say what is wrong. I learned that you e.g. need to have the certificates
generated in STRUSTSSO2. But there might be more steps necessary.
Best Regards,
Volker
Arshad Ansary
Jun 11, 2014 10:23 AM
Hi Volker,
Thanks for the blog. This is very helpful for beginners in Gateway.
I followed your steps and I registered the service using /iwfnd/maint_service in LOCAL server.
1 While creating data model for Gateway servcice , I am not able to select the individaul fields w/o selecting the
SOITEMDATA and SOHEADERDATA nodes.
2 I am not able to test the service in /IWFND/GW_CLIENT it syas some HTTP error. But I copied the URL and
paste it in chrome browser . I am able to get the result but the individual fields in Entity is not shown

Generated by Jive on 2015-11-14+01:00


22

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

Regards
Arshad
Matteo Somensi in response to Volker Drees on page 24
May 12, 2014 9:21 AM
hi, someone can help me?
I followed step by step your guide, but it does not work, the service when I call SalesOrderCollection or
SalesOrderItemCollection gives me an error like this:

Akhila M
Dec 4, 2013 6:14 AM
Thanks so much Volker, It is really helpful
Phillip Parkinson

Generated by Jive on 2015-11-14+01:00


23

volker.drees: Step-by-step guide to build an OData Service based on RFCs Part 1

Jul 24, 2013 4:36 PM


Excellent blog, thanks Volker.
Maybe worth mentioning the EPM Data Generator transaction is sepm_dg for those that are using a fresh
system without data yet.
Cheers
Phillip
Nick Archontis
Jul 11, 2013 8:35 PM
Thanks! Very clear and precise.
Nick.
Syam Babu in response to Volker Drees on page 24
Nov 15, 2012 1:31 PM
Hi Drees,
Thanks for the information.
Volker Drees in response to Syam Babu on page 24
Nov 15, 2012 1:11 PM
Hi Syam,
that is correct. The mentioned features are available as of GW2.0 SP5.
Best Regards,
Volker
Syam Babu
Nov 15, 2012 10:56 AM
Hi Drees,
Nice Blog.
When i am trying to create project through SEGW project is created success but in import menu option i am not
getting BAPI/RFC Interface tab.
Ref: Using SP04
Thanks,
Syam.

Generated by Jive on 2015-11-14+01:00


24

You might also like