Você está na página 1de 23

10/31/2014 Lets code CRUDQ and Function Import oper...

| SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 1/23
Getting Started Newsletters Store

Products Services & Support About SCN Downloads
Industries Training & Education Partnership Developer Center
Lines of Business University Alliances Events & Webinars Innovation
Log On Join Us Hi, Guest Search the Community
Activity Communications Actions
Browse
SAP Gateway
Previous
post
Next
post
0 Tweet 1
Introduction
Scenario
Procedure
Coding
1) Query Operation
2) Read Operation
3) Create Operation
4) Update Operation
5) Delete Operation
6) Function Import
Closing Remarks
Introduction

In this blog I will explain creation of simple SAP NW GW OData service which will implement Create, Read, Update,
Delete, Query and Function Import operations.
Just to make it very simple, I will just have single entity and entity set. Here I will perform these operations on Z table. In
real scenarios, you will use BAPIs. RFCs to perform these operations.
I recommend to read below documents and SAP documentation for deep understanding.
How to Develop a Gateway Service using Code based Implementation by Andre Fischer
How to Develop Query Options for an OData Service Using Code-Based Implementationby Andre Fischer
How to Write an OData Channel Gateway Service. Part 2 - The Runtime Data Provider Class
SAP Help - SAP NetWeaver Gateway Foundation Developer Guide - SAP NetWeaver Gateway Foundation
(SAP_GWFND) - SAP Library
Code Snippet - 3.2 Data Provider Class (DPC) - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP Library

Scenario

We have User information table as ZUSERINFO containing below fields
Lets code CRUDQ and Function Import operations in
OData service!
Posted by Chandrashekhar Mahajan in SAP Gateway on Mar 6, 2014 7:42:38 AM
Share 1 0 Like
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 2/23
Now let's create OData service which will insert, read, delete, update and query this table along with one custom
operation (UsersByCountry) as function import.
From an ABAPer perspective, This is what meant by OData operations.

OData
Operation
HTTP
Method
What it meant to an ABAPer
Create POST Insert <table> from <workarea>
Read GET Select Single * From <table> into <workarea>
Update PUT/PATCH Update <table> set <workarea>
Delete DELETE Delete from <table>
Query GET Select * From <table> Into Table
Function
Import
GET/POST Everything covered by GET and POST. But only use if scenario does not
fit into CRUDQ operations.
We can correlate Entity Set as Table Type, Internal table and Entity to work area, structure!
Entity Set Table Type or Internal Table
Entity Structure or Work Area
Procedure
Lets go to transaction SEGW and create project as ZUSERINFO.
Now right click on Data Model and Import --> DDIC Structure option, it will display popup window. Provide DDIC
structure name. In this case table name ZUSERINFO. It will propose field and key mapping as well as object name
which will be your entity name.
We will ignore MANDT as key field and also overwrite object name. I want my field names in upper camel case format
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 3/23
so I will change it accordingly. Lets call entity type as User. It will look as below. Press enter.
finally our entity type User will look as below.
Now lets create Entity Set as UserCollection (or UserSet or Users). You can refer Creating High-Quality OData
Services - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP Library
I will go with UserCollection as my entity set name.
Right click folder name Entity Sets and click create. Provide entity set name as UserCollection and Entity Type name
as User. It will display as below.
Now lets generate runtime artifacts. Click on generate runtime objects button. It will display popup as below. Keep the
default class names as-is and click on enter button.
On successful generation, you will see this kind of message log and generated artifacts. 4 classes will get generated. 2
for Data provider and 2 for Model provider.
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 4/23
Now register your service under service Maintenance folder. Click on Register button. Keep default values as-is and
hit enter button.
On successful registration, click Maintain button. This will open service catalog window along with option to call
Gateway Client. Click on Gateway Client button to test the service. (you can also call transaction /IWFND/GW_CLIENT
to open SAP NW Gateway client)
Append $metatda to base service URL and press execute button. If everything is fine then you will HTTP Response as
below. Metadata provides information such as Entity type, key property, properties and Entity Set name.
So far we just defined single entity type and entity set. Now its time to code CRUDQ and function import methods.

Coding
There is no specific order to implement these methods but it is always good to 1st implement query and read operation
as for Create and Update, you will need request data which you will get if you already have query/read implemented.
1) Query Operation
First we will start implementing query operation. Before that, I will add one record in my ZUSERINFO table as
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 5/23
Now open Runtime artifacts folder and right click on Class ZCL_ZUSERINFO_DPC_EXT and select Go to ABAP
Workbench option. Select edit mode and redefine method USERCOLLECTION_GET_ENTITYSET.
In the simplest form, this is what minimal coding will look like in GET_ENTITYSET method.
METHOD usercollection_get_entityset.

DATA: lt_userinfo TYPE TABLE OF zuserinfo,
ls_userinfo LIKE LINE OF lt_userinfo,
ls_entity LIKE LINE OF et_entityset.

*Get data from ZUSERINFO table
SELECT * FROM zuserinfo INTO TABLE lt_userinfo.

*Fill ET_ENTITYSET
LOOP AT lt_userinfo INTO ls_userinfo .
ls_entity-userid = ls_userinfo-userid.
ls_entity-firstname = ls_userinfo-firstname.
ls_entity-lastname = ls_userinfo-lastname.
ls_entity-email = ls_userinfo-email.
ls_entity-phone = ls_userinfo-phone.
ls_entity-country = ls_userinfo-country.
APPEND ls_entity TO et_entityset.
ENDLOOP.

ENDMETHOD.

We are selecting all data from table ZUSERINFO and appending the result to exporting parameter ET_ENTITYSET.
Now you can go to GW client transaction and execute URI /sap/opu/odata/sap/ZUSERINFO_SRV/UserCollection
which will display one record which we already added into Z table.
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 6/23
Observe the method signature. put external breakpoint in method and execute query. You will find important
information in method parameters in debugging mode.
Below table will provide brief explanation of method parameters, alternative approach to get the value of those method
parameters. Last column specifies if we need to code to implement query operation.
Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITYSET
OData Query Method Parameter Alternative way to get the
value
Coding
required to
implement
Query
Operation
UserCollection?$filter=UserID
eq '123' and LastName eq
'Mahajan'
IT_FILTER_SELECT_OPTIONS
and IV_FILTER_STRING
DATA: my_filter_options TYPE
/iwbep/t_mgw_select_option,
my_filter_string TYPE string.
my_filter_options =
io_tech_request_context-
>get_filter( )-
>get_filter_select_options( ).
my_filter_string =
io_tech_request_context-
>get_filter( )->get_filter_string( ).
Yes
UserCollection?
$select=FirstName,LastName
No method parameter data: my_select_fields type
/iwbep/t_mgw_tech_field_names.
my_select_fields =
io_tech_request_context-
>get_select( ).
No
UserCollection?
$orderby=FirstName,LastName
OR
UserCollection?
$orderby=FirstName
desc,LastName desc
IT_ORDER data: my_orderby_fields type
/iwbep/t_mgw_tech_order.
my_orderby_fields =
io_tech_request_context-
>get_orderby( ).
Yes
UserCollection?search='test' IV_SEARCH_STRING data: my_search_string type
string.
my_search_string =
io_tech_request_context-
>get_search_string( ).
Yes
UserCollection?$top=1 IS_PAGING-TOP data: my_top type string.
my_top =
io_tech_request_context-
>get_top( ).

data: my_skip type string.
my_skip =
Yes
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 7/23
io_tech_request_context-
>get_skip( ).
In case if we had association
and navigation between 2
entities
IT_NAVIGATION_PATH DATA: my_nav_path type
/iwbep/t_mgw_tech_navi.
my_nav_path =
io_tech_request_context-
>get_navigation_path( ).
Yes

2) Read Operation
Now lets implement GET_ENTITY method. Go to method USERCOLLECTION_GET_ENTITY and redefine it. Below is
the minimal code that we need to have in this method.
We need to read the key values passed from query URI and then fill the export parameter ER_ENTITY.
METHOD usercollection_get_entity.

DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_userid TYPE zuserinfo-userid,
ls_userinfo TYPE zuserinfo.

*Get the key property values
READ TABLE it_key_tab WITH KEY name = 'UserID' INTO ls_key_tab.

lv_userid = ls_key_tab-value.

*Get the single record from ZUSERINFO and fill ER_ENTITY
SELECT SINGLE * FROM zuserinfo INTO ls_userinfo WHERE userid = lv_userid.
IF sy-subrc = 0.
er_entity-userid = ls_userinfo-userid.
er_entity-firstname = ls_userinfo-firstname.
er_entity-lastname = ls_userinfo-lastname.
er_entity-email = ls_userinfo-email.
er_entity-phone = ls_userinfo-phone.
er_entity-country = ls_userinfo-country.
ENDIF.

ENDMETHOD.

Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITY

OData Query Method Parameter Alternative way to get the
value
Coding
required
to
implement
Query
Operation
UserCollection(UserID='Test')
OR UserCollection('Test'
IT_KEY_TAB DATA: lt_keys TYPE
/iwbep/t_mgw_tech_pairs.
lt_keys =
io_tech_request_context-
>get_keys( ).
Yes
In case if we had association
and navigation between 2
entities
IT_NAVIGATION_PATH DATA: my_nav_path type
/iwbep/t_mgw_tech_navi.
my_nav_path =
io_tech_request_context-
>get_navigation_path( ).
Yes
UserCollection('Test')?
$select=FirstName,LastName
No DATA: my_select_fields TYPE
/iwbep/t_mgw_tech_field_names.
my_select_fields =
io_tech_request_context-
>get_select( ).
No
UserCollection('Test')?
$format=json
No No No
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 8/23

Also note that you cannot use system query options else you will get an error as System query options
'$orderby,$skip,$top,$skiptoken,$inlinecount,' are not allowed in the requested URI
3) Create Operation
Now we will focus on create operation by redefining method USERCOLLECTION_CREATE_ENTITY. Below is the
code that will perform POST operation.
Here we are reading the request data and then filling the exporting parameter ER_ENTITY.
METHOD usercollection_create_entity.
DATA: ls_request_input_data TYPE zcl_zuserinfo_mpc=>ts_user,
ls_userinfo TYPE zuserinfo.

* Read Request Data
io_data_provider->read_entry_data( IMPORTING es_data = ls_request_input_data ).

* Fill workarea to be inserted
ls_userinfo-userid = ls_request_input_data-userid.
ls_userinfo-firstname = ls_request_input_data-firstname.
ls_userinfo-lastname = ls_request_input_data-lastname.
ls_userinfo-email = ls_request_input_data-email.
ls_userinfo-phone = ls_request_input_data-phone.
ls_userinfo-country = ls_request_input_data-country.

* Insert Data in table ZUSERINFO
INSERT zuserinfo FROM ls_userinfo.
IF sy-subrc = 0.
er_entity = ls_request_input_data. "Fill Exporting parameter ER_ENTITY
ENDIF.
ENDMETHOD.
To test POST operation, 1st execute GET operation and then press Use as Request button which will copy the
response to request window and then select operation POST and execute.
In case you execute GET operation i.e. GET_ENITITYSET and then try to perform POST operation then you will get
below kind of error. Hence make sure that you execute GET to read single entity i.e. GET_ENTITY operation and then
perform POST.
So correct steps are,
1) Execute GET to read single entity /sap/opu/odata/sap/ZUSERINFO_SRV/UserCollection('Test')
2) Click Use as Request button
3) Update your request properties.
4) Select HTTP method as POST
5) Query as /sap/opu/odata/sap/ZUSERINFO_SRV/UserCollection and execute
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 9/23
Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITY_C
OData Query Method Parameter Coding
required
to
implement
Query
Operation
/sap/opu/odata/sap/ZUSERINFO_SRV/UserCollection DATA:
ls_request_input_data TYPE
zcl_zuserinfo_mpc=>ts_user.
io_data_provider-
>read_entry_data(
IMPORTING es_data =
ls_request_input_data ).
Yes
In case of Create operation, you cannot use system query options else you will get an error as
The Data Services Request contains SystemQueryOptions that are not allowed for this Request Type
This error message comes from method PROCESS_ENTITY_SET (/IWCOR/CL_DS_PROC_DISPATCHER). Just in
case if you are curious and want to know

4) Update Operation
Now we will implement update operation by redefining method USERCOLLECTION_UPDATE_ENTITY. We need to
put below code in this method.
METHOD usercollection_update_entity.
DATA: ls_request_input_data TYPE zcl_zuserinfo_mpc=>ts_user,
ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_userid TYPE zuserinfo-userid,
ls_userinfo TYPE zuserinfo.
* Get key values
READ TABLE it_key_tab WITH KEY name = 'UserID' INTO ls_key_tab.
lv_userid = ls_key_tab-value.
IF lv_userid IS NOT INITIAL.

* Read request data
io_data_provider->read_entry_data( IMPORTING es_data = ls_request_input_data
).

* Update fields of table ZUSERINFO
UPDATE zuserinfo SET firstname = ls_request_input_data-firstname
lastname = ls_request_input_data-lastname
email = ls_request_input_data-email
phone = ls_request_input_data-phone
country = ls_request_input_data-country
WHERE userid = lv_userid.
IF sy-subrc = 0.
er_entity = ls_request_input_data. "Fill exporting parameter ER_ENTITY
ENDIF.
ENDIF.
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 10/23
ENDMETHOD.
Here also first we need to execute GET operation to read the entity and then copy the response to request using Use
as Request button and execute PUT operation after editing required data. Successful HTTP response will look as
below with status code as 204.
Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITY_U
OData Query Method Parameter Alternative way to get the
value
Coding required
to implement
Query Operation
UserCollection('Test1') IT_KEY_TAB DATA: lt_keys TYPE
/iwbep/t_mgw_tech_pairs,
ls_key TYPE
/iwbep/s_mgw_tech_pair.
lt_keys =
io_tech_request_context-
>get_keys( ).
Yes
UserCollection('Test1') IO_DATA_PROVIDER io_data_provider-
>read_entry_data(
IMPORTING es_data =
ls_request_input_data ).
Yes

5) Delete Operation
To implement Delete operation, you need to execute DELETE HTTP method for particular key. Below is the code for
method USERCOLLECTION_DELETE_ENTITY.
Here we are reading key value of the record to be deleted and executing Delete statement.
METHOD usercollection_delete_entity.
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_userid TYPE zuserinfo-userid.
* Read key values
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'UserID'.
lv_userid = ls_key_tab-value.
IF lv_userid IS NOT INITIAL.
* Delete record from table ZUSERINFO
DELETE FROM zuserinfo WHERE userid = lv_userid.
ENDIF.
ENDMETHOD.
On successful record deletion, you will see HTTP response code as 204.
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 11/23
Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITY_D

OData Query Method
Parameter
Alternative way to get the
value
Coding required to
implement Query Operation
UserCollection('Test1') IT_KEY_TAB DATA: lt_keys TYPE
/iwbep/t_mgw_tech_pairs,
ls_key TYPE
/iwbep/s_mgw_tech_pair.
lt_keys =
io_tech_request_context-
>get_keys( ).
Yes

6) Function Import

As per SAP documentation, Function Imports - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP Library
The Open Data Protocol (OData) includes standard CRUD (Create, Retrieve, Update, and Delete)
operations that map to the HTTP methods POST, GET, PUT/MERGE, and DELETE.
In addition, OData supports further service operations (function imports) that can be invoked by the HTTP
methods GET or POST for anything that cannot be mapped to the standard CRUD operations. You can
implement such additional service operations in the Service Builder by creating function imports within
your data model.
For example, you could create function imports for the following custom operations:
Confirm Work Item
Check Flight Availability
While it is simple to create new function imports to invoke custom operations, if the operation you want to
use can be invoked using a standard CRUD operation, you should not create a function import. That is,
you should only create function imports for custom operations that cannot be invoked using a standard
operation.

In simple terms, if an operation cannot fit into CRUD scenario then you can perform it by function import.
Suppose our ZUSERINFO table looks like below,
And we want to get users by specific country then we can implement function import. Lets call our function import as
UsersByCountry!
Right click on Data model and create function import. Provide proper name. Again refer Creating High-Quality
OData Services - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP Library
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 12/23
Provide required details such as mentioned below. Remember here we want to return collection of users and hence
we selected Return cardinality as 0..n with return entity set and HTTP method type as GET.
Now click on Function Import parameters and create import parameters as shown below. In this case we just want to
pass value of country and hence we will have one parameter as Country.
Finally save project, check project consistency and generate runtime objects. To check if everything is fine, in GW
client execute service metadata URL as /sap/opu/odata/sap/ZUSERINFO_SRV/$metadata which should show you
function import definition in metadata as below,
Now we will implement function import operation. Go to DPC_EXT class and redefine method
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~EXECUTE_ACTION.
Put below code to implement function import.
METHOD /iwbep/if_mgw_appl_srv_runtime~execute_action.
DATA: ls_parameter TYPE /iwbep/s_mgw_name_value_pair,
lv_country TYPE string,
lt_userinfo TYPE TABLE OF zuserinfo,
ls_userinfo TYPE zuserinfo,
ls_entity TYPE zcl_zuserinfo_mpc=>ts_user,
lt_entityset TYPE zcl_zuserinfo_mpc=>tt_user.


IF iv_action_name = 'UsersByCountry'. " Check what action is being requested
IF it_parameter IS NOT INITIAL.
* Read Function import parameter value
READ TABLE it_parameter INTO ls_parameter WITH KEY name = 'Country'.
IF sy-subrc = 0.
lv_country = ls_parameter-value.
ENDIF.

IF lv_country IS NOT INITIAL.
SELECT * FROM zuserinfo INTO TABLE lt_userinfo WHERE country =
lv_country.
LOOP AT lt_userinfo INTO ls_userinfo .
ls_entity-userid = ls_userinfo-userid.
ls_entity-firstname = ls_userinfo-firstname.
ls_entity-lastname = ls_userinfo-lastname.
ls_entity-email = ls_userinfo-email.
ls_entity-phone = ls_userinfo-phone.
ls_entity-country = ls_userinfo-country.
APPEND ls_entity TO lt_entityset.
ENDLOOP.
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 13/23
* Call methos copy_data_to_ref and export entity set data
copy_data_to_ref( EXPORTING is_data = lt_entityset
CHANGING cr_data = er_data ).

ENDIF.
ENDIF.
ENDIF.

ENDMETHOD.

To test function import we need to query as /sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?Country='US'
If there are multiple parameters then it will be separated by comma.
Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_FUNC_IMPORT
OData Query Method
Parameter
Alternative way to get the
value
Coding
required
to
implement
Query
Operation
/sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?
Country='US'
IT_PARAMETER DATA: my_parameter TYPE
/iwbep/t_mgw_name_value_pair.
my_parameter =
io_tech_request_context-
>get_parameters( ).
Yes


Closing Remarks

This is very simple example of an OData service. In real case scenarios, you will have multiple entities, relationship,
association and navigation between them but when it comes to coding in DPC_EXT class methods, you will find above
explanation of each method, parameters and alternative way to get the parameter values useful.

I hope you enjoyed reading this blog and now ready to develop your OData service! I request you to put
comments/suggestions. Please feel free if you have any different thought to improve any part of this blog as
well.
[Update - 9/25/2014] I have posted new blog which focuses more on association/navigation and data provider
$expand. You can access it at Let's code association/navigation and data provider expand in OData
service!

Happy Learning & Coding
8908 Views
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 14/23
Average User Rating
(12 ratings)
0 Tweet 1
Products: sap_netweaver_gateway Tags: developer, gateway, odata, netweaver_gateway, crud, segw, crud_operations,
rest_webservice, function_import
Share 1 0 Like
52 Comments
Like (1)
Syam Babu Mar 7, 2014 8:33 AM
Hi Chandra,

Excellent Blog and Nicely written each and every point and definitely will try this in my system.

Is it possible to create function import operation for Deep_Insert ?

Thanks,
Syam
Like (0)
Chandrashekhar Mahajan Mar 10, 2014 8:50 AM (in response to Syam Babu)
Hi Syam,

I don't think that deep_insert is possible with Function import. As per my understanding,
Deep insert will require kind of parent child entities with association between them and it will
be not suitable to pass this kind of information via function import parameters.

But again, I am not 100% sure as technically it may be possible but it will be very bad design
and should not be recommended. May be some expert can comment more on this.

Regards,
Chandra
Like (1)
faraz khan Mar 7, 2014 4:43 PM
Chandra Sir Once Again Extremely good blog.

Regards Faraz
Like (0)
Chandrashekhar Mahajan Mar 10, 2014 8:51 AM (in response to faraz khan)
Thanks faraz khan !

Regards,
Chandra
Like (1)
Ajith Cheruvally Mar 12, 2014 10:56 AM
Hi Chandra,

I am a beginner in NW Gateway, and I found this document very useful. Thanks a lot!!!.
Like (0)
Chandrashekhar Mahajan Mar 12, 2014 2:22 PM (in response to Ajith Cheruvally)
Thanks for your comments Ajith Cheruvally ! Yes my motive to write this blog was to
explain basics of SAP GW OData service in simple way so that anyone can start quickly
developing it

Regards,
Chandra
Like (0)
Sharada G Mar 17, 2014 11:49 AM (in response to Chandrashekhar Mahajan)
good one !!
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 15/23
Like (1)
Arindam Samanta Mar 18, 2014 8:57 AM
Hi Chandrashekhar,

Thanks for your great effort. Its very good stuff...

Regards,
Arindam Samanta
Like (0)
akshath lt Mar 18, 2014 11:01 AM
HI Chandrashekar,

I have used "Create Service (Method => FILE_UPLOAD_CREATE_ENTITY) from Service
Implementation" for update data to backend system and i have written logic for update back end
system.

When i test in SAP NW Gateway Client i am getting below error

Browser showing below output/error

I have refered http://scn.sap.com/community/netweaver-gateway/blog/2014/03/06/let-s-code-crudq-
and-function-import-operations-in-odata-service blog but i couldnt get any idea.

Kindly help me on this

Thanks,
Akshath
Like (1)
Atanu Mallik Mar 18, 2014 12:35 PM (in response to akshath lt)
The error is expected. You can not perform a post operation on the metadata document.
Neither you are passing any content to be created. check section Lets code CRUDQ and
Function Import operations in OData service! carefully... specially the URL and the Payload
passed in the Gateway Client
govindu nagotla Mar 18, 2014 8:27 PM
Hello Chandra,

This blog is very helpful for us in understanding the GW odata services in the ABAPers point of view .

I want to know the performance of the Odata services in the real time HTML5 applications.

Could you suggest/Guide us in developing the best odata services in the performance point of view ?

Have you faced any time performance issues with Odata services ?

the above details will be very help for us in real time application developments.

Regards,
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 16/23
Like (0)
Govindu
Like (0)
Chandrashekhar Mahajan Mar 19, 2014 11:19 AM (in response to govindu nagotla)
Thanks Govindu for your comments!

For performance of OData services as well as best practices, you can refer

Regards,
Chandra
Performance Best Practices - SAP NetWeaver Gateway Foundation (SAP_GWFND) -
SAP Library
OData Best Practices - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP
Library
and Creating High-Quality OData Services - SAP NetWeaver Gateway Foundation
(SAP_GWFND) - SAP Library
akshath lt Mar 19, 2014 5:55 PM (in response to Chandrashekhar Mahajan)
HI Chandra,

I have requirement to both upload/download PDF file into MIME repository or DMS
through SAP UI5.

I have tried to download PDF file from the MIME repository and I have followed
below steps.

1.1 Default path 'SAP/PUBLIC/<File name> for MIME repository
1.2 Used below code for upload/ download
--------------------------------------------------------------
Initiating class
--------------------------------------------------------------

lr_mime_rep = cl_mime_repository_api=>if_mr_api~get_api( ).


--------------------------------------------------------------
Reading file
--------------------------------------------------------------
lr_mime_rep->get(
EXPORTING
i_url = p_path
i_content = lv_content
EXCEPTIONS
parameter_missing = 1
error_occured = 2
cancelled = 3
permission_failure = 4
data_inconsistency = 5
new_loio_already_exists = 6
is_folder = 7
OTHERS = 8 ).


--------------------------------------------------------------
Converting XSTRING TO BINARY
--------------------------------------------------------------

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
input_length = lv_length
* first_line = 0
* last_line = 0
IMPORTING
buffer = lv_content
TABLES
binary_tab = lt_data
EXCEPTIONS
failed = 1
OTHERS = 2.

--------------------------------------------------------------
Download file to desktop
--------------------------------------------------------------

lv_filename = 'C:\ABC.PDF'.

cl_gui_frontend_services=>gui_download(
EXPORTING
filename = lv_filename " Name of file
filetype = 'BIN'
IMPORTING
filelength = lv_length " File length
CHANGING
1. Created FM for PDF download.
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 17/23
Like (0)
data_tab = lt_data " Transfer table for file contents
EXCEPTIONS
OTHERS = 19 ).
-----------------------------------------------------------------------------


Problem is CL_GUI_FRONTEND_SERVICE won't work in background so I can't
able to download file.

Is this correct way to download / Upload file to desktop ?

If you have any better solution please share with me..

Thanks,
Akshath
1. Created project in SEGW.
2. Used FM inside GET_ENTITY method.
Like (0)
Chandrashekhar Mahajan Mar 20, 2014 8:02 AM (in response to akshath lt)
Hi Akshath,

To download pdf file from sapui5 application, you can refer my blog
Display Smartform (PDF) in SAPUI5

Regards,
Chandra
Like (1)
Lakhan Sarada Mar 31, 2014 2:24 PM
Very useful blog....More descriptive and understandable.
Thanks for this!
Like (0)
Syam Babu Apr 23, 2014 1:07 PM
Hi Chandra,

Does we have multiple input parameters in my RFC ..so how do i have to pass URI path

like for get_entityset we are passing when we have multiple inputs

?$filter=BankCtry eq 'IN' and BankKey eq '222'


Thanks,
Syam
Like (0)
Chandrashekhar Mahajan Apr 23, 2014 1:18 PM (in response to Syam Babu)
Hi Syam,

yes ?$filter=BankCtry eq 'IN' and BankKey eq '222' this is the correct way of passing
multiple input parameters.

Regards,
Chandra
Like (0)
Syam Babu Apr 23, 2014 3:21 PM (in response to Chandrashekhar Mahajan)
Yes..When we are using the function import also we have to pass like above URI?

Bit confusing here in your function import single parameter your not using any
$filter in URI
/sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?Country='US'...


Thanks,
Syam
Chandrashekhar Mahajan May 2, 2014 10:02 AM (in response to Syam Babu)
for function import multiple parameters will be separated by comma &. for
e.g
/sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?
Country='US',City='Pune'
it should be
/sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?
Country='US'&City='Pune'
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 18/23
Like (1)
Regards,
Chandra
Like (0)
Syam Babu Apr 23, 2014 3:50 PM (in response to Chandrashekhar
Mahajan)
Yes..Now I understood..


Thanks,
Syam
Like (0)
Syam Babu Apr 23, 2014 4:27 PM
Hi Chandra,

I have done the function import what you have done like..it's working fine...

When I am creating function import using complex type as Return type everything fine coding part.

But here how to to test the URI for complex type as a return type in function import ..any Specific
sample URI for complex type.


Thanks,
Syam
Like (0)
Chandrashekhar Mahajan Apr 23, 2014 4:59 PM (in response to Syam Babu)
Not sure about URI for complex type but you can refer this discussion thread How to use
complex types in a odata url filter

Regards,
Chandra
Like (0)
Syam Babu May 2, 2014 8:03 AM
Hi Chandra,

Can I get the data as a JSON format from Function Import?

If yes,Give me sample URI for JSON format.

Thanks,
Syam
Like (0)
Chandrashekhar Mahajan May 2, 2014 10:04 AM (in response to Syam Babu)
Hi,

it will be /sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?
Country='US'&City='Pune'&$format=json

also note that multiple parameters will be separated by & and not comma. I corrected my
earlier response.

Regards,
Chandra
Syam Babu May 2, 2014 10:20 AM (in response to Chandrashekhar Mahajan)
Hi Chandra,

In my function import there is no input parameter,so that time I am passing to URI
given below as a json

Below of URI's i have tried without parameter.

/sap/opu/odata/sap/ZPROJ_FILES_SRV/FilesList?&$format=json

10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 19/23
Like (0)
/sap/opu/odata/sap/ZPROJ_FILES_SRV/FilesList&$format=json

Still getting error says...HTTP 500 - Internal Server Error.

Where can i expect mistake in my development.

Note : Getting data for XML.

Thanks,
Syam
Like (0)
Chandrashekhar Mahajan May 2, 2014 11:30 AM (in response to Syam Babu)
it will be /sap/opu/odata/sap/ZPROJ_FILES_SRV/FilesList?
$format=json

Regards,
Chandra
Like (0)
Sunil Khemchand May 3, 2014 12:13 AM
Nice work Chandra!!
Like (1)
Hemendra Sabharwal May 7, 2014 11:43 AM
Great work Chandra.

Thanks & Regards
Hemendra
Like (0)
pavan jhawar May 11, 2014 8:30 AM
Hi Chandra,

It is an excellent blog for the beginners. I am trying the similar steps in my system, but getting the
below issue when i try to execute the service from the transaction /IWFND/GW_CLIENT. But that url
works in browser and i can see the metadata and entityset parameters working in chrome browser.

error: HTTP Receive failed: ICM_HTTP_CONNECTION_FAILED

What can be the issue?
Like (0)
Syam Babu May 11, 2014 9:29 AM (in response to pavan jhawar)
Hi Pavan,

Check with your basis team.they will help about this issue.

Thanks,
Syam
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 20/23
Like (0)
Ankit Maskara Oct 9, 2014 6:28 PM (in response to pavan jhawar)
Hi Pavan,

This issue comes up when the URL is wrong. Open the same in SM59 and see. You will not
be able to launch that URL in any browser.

PS: In case of SSO/SSL - you may get another error ICM_HTTP_SSL_ERROR.


BR.
Like (0)
Krishnakant Joshi Aug 30, 2014 11:01 AM
Great blog chandra,

Just wanted to know that, can we pass table param ( entity set ) to function import ?

Regards,
Kk
Like (0)
Ashwin Dutt R Sep 1, 2014 3:20 AM (in response to Krishnakant Joshi)
Hello Krishna,

You meant to say u have table of inputs and u want all those entries to be sent as inputs to
ur function import ? If yes, u cannot send at one shot like how we give to FM generally. U
need to operate in BATCH mode.

Example for GET: Search is happening based on different Name's and u get the
corresponding output as per the logic written in ur Execute_Action Method.

Regards,
Ashwin
Like (0)
Lukasz Zemi Sep 10, 2014 12:55 PM
Hello, How you get er_data ?
Ashwin Dutt R Sep 10, 2014 1:13 PM (in response to Lukasz Zemi)
Hello Lukasz,

er_data is actually an exporting parameter.

The below method which is already an existing method and you need to just call this and
send back the response as below. Only thing which has to be done from your end is to just
map back the data as response as below.

copy_data_to_ref( EXPORTING is_data = <your result set>
CHANGING cr_data = er_data ).

10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 21/23
Like (0)
Regards,
Aswhin
Like (0)
Lukasz Zemi Sep 19, 2014 1:08 PM
Hello,

How to know for example if odata update request finished without errors ?
from http status ?
Like (0)
Ashwin Dutt R Sep 19, 2014 1:58 PM (in response to Lukasz Zemi)
Hello Lukasz,

Yes. Its based on status code 204.
In my opinion Its always good to write an error handling to through an exception if update is
failed. I mean check for errors encountered if update is failed. If error are encountered raise
exception.
If no errors are encountered after update do nothing. Lets the flow continue.

If interested, U can also fire a read after an update to check if data is really updated.
U can accomplish via BATCH ( Batch Retrieval and Change Set ).

Regards,
Ashwin
Like (0)
Jordan Tchorbadjiyski Sep 30, 2014 4:37 PM
Hi Chandra, great post!
I have a question regarding the function import though:
Is it also possible to pass multiple values for a single parameter and what would be the correct
syntax?

That is something like: /sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?
Country='US'&Country='UK'

I've been trying different formats but couldn't get it right.

Thanks,
Jordan
Like (0)
Ashwin Dutt R Oct 1, 2014 1:10 AM (in response to Jordan Tchorbadjiyski)
Hello Jordan,

U can send as below but values sent will be comma separated.

/sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?Country='US',Country='UK'

But if u put a break-point in ur Execute_Action method and check the entry of
'IT_PARAMETER' at the runtime, the different set of data u sent by repeating the same
properties as mentioned in above URL will be comma separated and extraction of those
values would be tedious and may turn into messy..
Once u debug u will get to know how the data will be captured in IT_PARAMETER which u
have sent as part of ur action parameters in the above URL.
According to me Execute action is not a correct operation if u want to send inputs in a
fashion u are trying to send.

Regards,
Ashwin
Jordan Tchorbadjiyski Oct 1, 2014 9:25 AM (in response to Ashwin Dutt R)
Hi Ashwin,

Thanks for the answer, unfortunately that's one of the ways I already tried but was
reluctant to parse on the server side
(sap/opu/odata/sap/ZUSERINFO_SRV/UsersByCountry?Country='US|UK'')

Since it_parameter is a table with no size restrictions I was hoping there should be
a way to fill it with more than one entry per parameter and pass the information in a
cleaner manner...

My use case is that I want to trigger a function in the back end; that function accepts
either a table or a single value for a parameter (selection of multiple values in the
UI should be possible, even preferred). That's why I thought it doesn't fit into any
CRUD operation and wanted to use a function import.
What would you advise? Perhaps an update operation on an entity set is
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 22/23
Like (0)
semantically closest but the requirement to pass multiple values can be satisfied
only for get entitiy set?

Thanks and best regards,
Jordan
Like (0)
Ashwin Dutt R Oct 1, 2014 11:32 AM (in response to Jordan Tchorbadjiyski)
Hello Jordan,

The function which u would like to trigger at the back end by sending
table entries/ Single value is a GET operation is it ?

Regards,
Ashwin
Like (0)
Jordan Tchorbadjiyski Oct 1, 2014 12:38 PM (in response to Ashwin Dutt
R)
Hi Ashwin,
No, the function starts a job (a background process) with no
direct output related to the service...

Best regards,
Jordan
Like (0)
Ashwin Dutt R Oct 1, 2014 1:30 PM (in response to Jordan
Tchorbadjiyski)
Hello Jordan,

Like u said its not falling under any standard CRUD
operation and cant be accomplished through function
import.

Then i think u can call an update.
But since u may have multiple entries, then how will u
send those to ur update operation ?
Only flat structure update is possible. I mean one entry
at a time.

Then u need to operate on BATCH mode so that inside
a payload u can load all ur entries and fire update.
But this update will call ur Job for very entry.
Say 10 entries are there inside the table, then 10 times
ur job will be called when u operate in BATCH.

So u need to think about this as well.

We can do this via Create_Deep_Entity but its not a
standard approach i feel and would be just an
workaround.

Regards,
Ashwin
Like (1)
Rajesh Dadwal Sep 30, 2014 7:29 PM
Thanks Chandra, Well documented blog for CRUD & function import..


Regards,
Rajesh
Like (0)
Vishnu Pankajakshan Oct 7, 2014 11:10 AM
I tried all the above steps and its working fine.I tried another method by redefining DPC_EXT and
MPC_EXT and i used a custom functional module with TABLE's as input and output.I passed my input
through XML payload in gateway client.I am not sure whether the method i used is proper or not.And i
also would like to know a method to do the same thing using GPA.

regards,
Vishnu
Vishnu Pankajakshan Oct 8, 2014 6:59 AM
Thanks chandra for such a large documentation for CRUD Operation.
10/31/2014 Lets code CRUDQ and Function Import oper... | SCN
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 23/23
Follow SCN
Site Index Contact Us SAP Help Portal
Privacy Terms of Use Legal Disclosure Copyright
Like (0)
Like (0)
Arindam Samanta Oct 8, 2014 7:24 AM
Hi Chandrshekhar,

Recently based on my requirement, I have used ?$filter option in my odata service url for query
operation. My URL is -

http://yyy.yyy.yyy.com:0000/sap/opu/odata/sap/ZAS_SODEXO_SRV/SODEXO_DISBURSE_SET?
$filter=Action eq 'COUNT' and Fdate eq '12.12.2014' and Tdate eq '12.12.2014' and Bukrs eq
'1001' and Pernr eq '123456'
This URL is being used in SAPUI5 application. While executing my application, /$count is
automatically added at the end my URL. As a result, ODATA not able to fetch data. How to get the
records through this kind of ODATA URL?
Kindly suggest!
Thanks & Regards,
Arindam Samanta
Like (0)
Vishnu Pankajakshan Oct 8, 2014 2:13 PM
Hi Chandra,

Which and how we can pass a entity_Set through the payload.I tried implementing it but i got stuck
with error '"The server is refusing to process the request because the entity has a unsupported format"

Regards,
Vishnu
Like (0)
Vishnu Pankajakshan Oct 8, 2014 2:18 PM (in response to Vishnu Pankajakshan)
I am trying to retrieve an internal table of data (Expecting to pass multiple entities using Http
POST method) so that i can modify and return through the response XML.

Regards,
Vishnu
Like (1)
Ankit Maskara Oct 14, 2014 11:06 AM
Good Work Chandra.
Like (0)
vivek gaurav Oct 14, 2014 2:59 PM (in response to Ankit Maskara)
Very nice and helpfull blog
Like (0)
Pham Chinh Thi Oct 29, 2014 6:24 AM
Hi Chandra,
How can we put parameter from action of BO, when I redefined a BOPF, I see the case that the
parameter name of function import after I redefined from action of BO is different from the method that
I implement in class?

Thanks
Chinh Pham

Você também pode gostar