10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN Getting Started Newsletters
Log On
Hi, Guest
Us
Store
Search the Community
Products
Services &
About SCN
s
Industries
Training & Education
Partnership
Developer Center
Activity
Lines of Business
University Alliances
Events & Webinars
Innovation
Browse
Communications
Actions
SAP Gateway
Let’s code CRUDQ and Function Import operations in OData service! Previous Next Posted by Chandrashekhar Mahajan in SAP Gateway on Mar 6, 2014 7:42:38 AM Share
1
0
Tweet
1
Like
Introduction Scenario Procedure Coding 1) Query Operation 2) Read Operation 3) Create Operation 4) Update Operation 5) Delete Operation 6) Function Import Closing Remarks
0
postpost
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 information table as ZINFO containing below fields
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
1/23
10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN
Now let's create OData service which will insert, read, delete, update and query this table along with one custom operation (sByCountry) 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
from <workarea> Read GET Select Single * From
into <workarea> Update PUT/PATCH Update
set <workarea> Delete DELETE Delete from
for MIME repository 1.2 Used below code for / -------------------------------------------------------------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. ------------------------------------------------------------- file to desktop -------------------------------------------------------------lv_filename = 'C:\ABC.PDF'. cl_gui_frontend_services=>gui_( EXPORTING filename = lv_filename " Name of file filetype = 'BIN' IMPORTING filelength = lv_length " File length CHANGING
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
16/23
10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN data_tab = lt_data " Transfer table for file contents EXCEPTIONS OTHERS = 19 ). ----------------------------------------------------------------------------1. Created project in SEGW. 2. Used FM inside GET_ENTITY method. Problem is CL_GUI_FRONTEND_SERVICE won't work in background so I can't able to file. Is this correct way to / file to desktop ? If you have any better solution please share with me.. Thanks, Akshath Like (0)
Chandrashekhar Mahajan Mar 20, 2014 8:02 AM (in response to akshath lt)
Hi Akshath, To pdf file from sapui5 application, you can refer my blog Display Smartform (PDF) in SAPUI5 Regards, Chandra Like (0)
Lakhan Sarada Mar 31, 2014 2:24 PM
Very useful blog....More descriptive and understandable. Thanks for this! Like (1)
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 URI path like for get_entityset we are ing 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 ing 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 like above URI? Bit confusing here in your function import single parameter your not using any $filter in URI /sap/opu/odata/sap/ZINFO_SRV/sByCountry?Country='US'... Thanks, Syam Like (0)
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/ZINFO_SRV/sByCountry? Country='US',City='Pune' it should be /sap/opu/odata/sap/ZINFO_SRV/sByCountry? Country='US'&City='Pune'
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
17/23
10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN Regards, Chandra Like (1)
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 complex types in a odata url filter
How to use
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/ZINFO_SRV/sByCountry? 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 Like (0)
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 ing 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
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
18/23
10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN /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 (0)
Hemendra Sabharwal May 7, 2014 11:43 AM
Great work Chandra. Thanks & Regards Hemendra Like (1)
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 Like (0)
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
19/23
10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN 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 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 ? Like (0)
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 =
CHANGING cr_data = er_data ).
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
20/23
10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN 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 multiple values for a single parameter and what would be the correct syntax? That is something like: /sap/opu/odata/sap/ZINFO_SRV/sByCountry? 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/ZINFO_SRV/sByCountry?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 Like (0)
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/ZINFO_SRV/sByCountry?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 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
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
21/23
10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN semantically closest but the requirement to 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 (0)
Rajesh Dadwal Sep 30, 2014 7:29 PM
Thanks Chandra, Well documented blog for CRUD & function import.. Regards, Rajesh Like (1)
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 ed 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 Like (0)
Vishnu Pankajakshan Oct 8, 2014 6:59 AM
Thanks chandra for such a large documentation for CRUD Operation.
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
22/23
10/31/2014
Let’s code CRUDQ and Function Import oper... | SCN 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 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 uned 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 multiple entities using Http POST method) so that i can modify and return through the response XML. Regards, Vishnu Like (0)
Ankit Maskara Oct 14, 2014 11:06 AM
Good Work Chandra. Like (1)
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 Like (0)
Site Index Privacy
Us of Use
SAP Help Portal Legal Disclosure
Copyright
http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service
Follow SCN
23/23
Related Documents 171j1w
Export And Import Management 5471x
November 2021 0
11 Consumption Function And Investment Function 1s4b4d
November 2019 46
Mastering Import And Export Management 1r2j1s
July 2020 0
Vegetative Structure And Function 2z135i
December 2019 48
Dna Structure And Function 601x6o
October 2021 0
Tissue Structure And Function 4y341c
November 2019 37
More Documents from "Prashanth Kumar" 5u6t60
Lose Your Accent In 28 Days b4214
November 2019 32
Hooke's Law Lab 312s3u
December 2019 36
Course Description, Cse Dept, National University,bangladesh 15n3t
December 2019 29
Fassler 3l442i
December 2019 28
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 Let’s go to transaction SEGW and create project as ZINFO. 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 ZINFO. 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 http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 2/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN so I will change it accordingly. Let’s call entity type as . It will look as below. Press enter. finally our entity type will look as below. Now let’s create Entity Set as Collection (or Set or s). You can refer Creating High-Quality OData Services - SAP NetWeaver Gateway Foundation (SAP_GWFND) - SAP Library I will go with Collection as my entity set name. Right click folder name Entity Sets and click create. Provide entity set name as Collection and Entity Type name as . It will display as below. Now let’s 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. http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 3/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN Now your service under service Maintenance folder. Click on 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 it’s 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 ZINFO table as http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 4/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN Now open Runtime artifacts folder and right click on Class ZCL_ZINFO_DPC_EXT and select Go to ABAP Workbench option. Select edit mode and redefine method COLLECTION_GET_ENTITYSET. In the simplest form, this is what minimal coding will look like in GET_ENTITYSET method. METHOD collection_get_entityset. DATA: lt_info TYPE TABLE OF zinfo, ls_info LIKE LINE OF lt_info, ls_entity LIKE LINE OF et_entityset. *Get data from ZINFO table SELECT * FROM zinfo INTO TABLE lt_info. *Fill ET_ENTITYSET LOOP AT lt_info INTO ls_entity-id ls_entity-firstname ls_entity-lastname ls_entity-email ls_entity-phone = = = = = ls_info . ls_info-id. ls_info-firstname. ls_info-lastname. ls_info-email. ls_info-phone. ls_entity-country = ls_info-country. APPEND ls_entity TO et_entityset. ENDLOOP. ENDMETHOD. We are selecting all data from table ZINFO and appending the result to exporting parameter ET_ENTITYSET. Now you can go to GW client transaction and execute URI /sap/opu/odata/sap/ZINFO_SRV/Collection which will display one record which we already added into Z table. http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 5/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN 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 Collection?$filter=ID IT_FILTER_SELECT_OPTIONS DATA: my_filter_options TYPE eq '123' and LastName eq 'Mahajan' and IV_FILTER_STRING Yes /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( ). Collection? $select=FirstName,LastName No method parameter Collection? IT_ORDER $orderby=FirstName,LastName OR data: my_select_fields type No /iwbep/t_mgw_tech_field_names. my_select_fields = io_tech_request_context>get_select( ). data: my_orderby_fields type /iwbep/t_mgw_tech_order. my_orderby_fields = io_tech_request_context>get_orderby( ). Yes data: my_search_string type string. my_search_string = io_tech_request_context- Yes Collection? $orderby=FirstName desc,LastName desc Collection?search='test' IV_SEARCH_STRING Collection?$top=1 IS_PAGING-TOP >get_search_string( ). data: my_top type string. my_top = io_tech_request_context>get_top( ). Yes data: my_skip type string. my_skip = http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 6/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN 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 = Yes io_tech_request_context>get_navigation_path( ). 2) Read Operation Now let’s implement GET_ENTITY method. Go to method COLLECTION_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 ed from query URI and then fill the export parameter ER_ENTITY. METHOD collection_get_entity. DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair, lv_id TYPE zinfo-id, ls_info TYPE zinfo. *Get the key property values READ TABLE it_key_tab WITH KEY name = 'ID' INTO ls_key_tab. lv_id = ls_key_tab-value. *Get the single record from ZINFO and fill ER_ENTITY SELECT SINGLE * FROM zinfo INTO ls_info WHERE id = lv_id. IF sy-subrc = 0. er_entity-id = ls_info-id. er_entity-firstname = ls_info-firstname. er_entity-lastname = ls_info-lastname. er_entity-email = ls_info-email. er_entity-phone = ls_info-phone. er_entity-country = ls_info-country. ENDIF. ENDMETHOD. Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITY OData Query Alternative way to get the value Method Parameter Coding required to implement Query Operation Collection(ID='Test') IT_KEY_TAB OR Collection('Test' DATA: lt_keys TYPE /iwbep/t_mgw_tech_pairs. lt_keys = io_tech_request_context>get_keys( ). In case if we had association IT_NAVIGATION_PATH DATA: my_nav_path type and navigation between 2 /iwbep/t_mgw_tech_navi. entities my_nav_path = io_tech_request_context>get_navigation_path( ). Yes Yes Collection('Test')? No $select=FirstName,LastName DATA: my_select_fields TYPE No /iwbep/t_mgw_tech_field_names. my_select_fields = io_tech_request_context>get_select( ). Collection('Test')? $format=json No No No http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 7/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN 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 COLLECTION_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 collection_create_entity. DATA: ls_request_input_data TYPE zcl_zinfo_mpc=>ts_, ls_info TYPE zinfo. * Read Request Data io_data_provider->read_entry_data( IMPORTING es_data = ls_request_input_data ). * Fill workarea to be inserted ls_info-id = ls_request_input_data-id. ls_info-firstname = ls_request_input_data-firstname. ls_info-lastname ls_info-email ls_info-phone ls_info-country = = = = ls_request_input_data-lastname. ls_request_input_data-email. ls_request_input_data-phone. ls_request_input_data-country. * Insert Data in table ZINFO INSERT zinfo FROM ls_info. 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/ZINFO_SRV/Collection('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/ZINFO_SRV/Collection and execute http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 8/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN 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/ZINFO_SRV/Collection DATA: Yes ls_request_input_data TYPE zcl_zinfo_mpc=>ts_. io_data_provider>read_entry_data( IMPORTING es_data = ls_request_input_data ). 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 COLLECTION_UPDATE_ENTITY. We need to put below code in this method. METHOD collection_update_entity. DATA: ls_request_input_data TYPE zcl_zinfo_mpc=>ts_, ls_key_tab TYPE /iwbep/s_mgw_name_value_pair, lv_id TYPE zinfo-id, ls_info TYPE zinfo. * Get key values READ TABLE it_key_tab WITH KEY name = 'ID' INTO ls_key_tab. lv_id = ls_key_tab-value. IF lv_id IS NOT INITIAL. * Read request data io_data_provider->read_entry_data( IMPORTING es_data = ls_request_input_data ). * Update fields of table ZINFO UPDATE zinfo SET firstname lastname email phone country = = = = = ls_request_input_data-firstname ls_request_input_data-lastname ls_request_input_data-email ls_request_input_data-phone ls_request_input_data-country WHERE id = lv_id. IF sy-subrc = 0. er_entity = ls_request_input_data. "Fill exporting parameter ER_ENTITY ENDIF. ENDIF. http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 9/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN 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 Collection('Test1') IT_KEY_TAB Alternative way to get the value DATA: lt_keys TYPE /iwbep/t_mgw_tech_pairs, ls_key TYPE Coding required to implement Query Operation Yes /iwbep/s_mgw_tech_pair. lt_keys = io_tech_request_context>get_keys( ). Collection('Test1') IO_DATA_PROVIDER io_data_provider>read_entry_data( Yes IMPORTING es_data = ls_request_input_data ). 5) Delete Operation To implement Delete operation, you need to execute DELETE HTTP method for particular key. Below is the code for method COLLECTION_DELETE_ENTITY. Here we are reading key value of the record to be deleted and executing Delete statement. METHOD collection_delete_entity. DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair, lv_id TYPE zinfo-id. * Read key values READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'ID'. lv_id = ls_key_tab-value. IF lv_id IS NOT INITIAL. * Delete record from table ZINFO DELETE FROM zinfo WHERE id = lv_id. ENDIF. ENDMETHOD. On successful record deletion, you will see HTTP response code as 204. http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 10/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN Here IO_TECH_REQUEST_CONTEXT refers to /IWBEP/IF_MGW_REQ_ENTITY_D OData Query Method Parameter Alternative way to get the Coding required to implement Query Operation value Collection('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 s 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 , if an operation cannot fit into CRUD scenario then you can perform it by function import. Suppose our ZINFO table looks like below, And we want to get s by specific country then we can implement function import. Let’s call our function import as sByCountry! 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 http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 11/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN Provide required details such as mentioned below. here we want to return collection of s 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 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/ZINFO_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_info TYPE TABLE OF zinfo, ls_info TYPE zinfo, ls_entity TYPE zcl_zinfo_mpc=>ts_, lt_entityset TYPE zcl_zinfo_mpc=>tt_. IF iv_action_name = 'sByCountry'. " 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 zinfo INTO TABLE lt_info WHERE country = lv_country. LOOP AT lt_info INTO ls_info . ls_entity-id = ls_info-id. ls_entity-firstname = ls_info-firstname. ls_entity-lastname = ls_info-lastname. ls_entity-email = ls_info-email. ls_entity-phone = ls_info-phone. ls_entity-country = ls_info-country. APPEND ls_entity TO lt_entityset. ENDLOOP. http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 12/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN * 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/ZINFO_SRV/sByCountry?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/ZINFO_SRV/sByCountry? IT_PARAMETER DATA: my_parameter TYPE Yes Country='US' /iwbep/t_mgw_name_value_pair. my_parameter = io_tech_request_context>get_parameters( ). 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 http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 13/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN Products: sap_netweaver_gateway Tags: developer, gateway, odata, netweaver_gateway, crud, segw, crud_operations, rest_webservice, function_import Average Rating (12 ratings) Share 0 1 Tweet 1 Like 0 52 Comments 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 (1) 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 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 (0) faraz khan Mar 7, 2014 4:43 PM Chandra Sir Once Again Extremely good blog. Regards Faraz Like (1) Chandrashekhar Mahajan Mar 10, 2014 8:51 AM (in response to faraz khan) Thanks faraz khan ! Regards, Chandra Like (0) 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 (1) 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 !! Like (0) http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 14/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN Arindam Samanta Mar 18, 2014 8:57 AM Hi Chandrashekhar, Thanks for your great effort. Its very good stuff... Regards, Arindam Samanta Like (1) akshath lt Mar 18, 2014 11:01 AM HI Chandrashekar, I have used "Create Service (Method => FILE__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-crudqand-function-import-operations-in-odata-service blog but i couldnt get any idea. Kindly help me on this Thanks, Akshath Like (0) 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 ing any content to be created. check section Let’s code CRUDQ and Function Import operations in OData service! carefully... specially the URL and the Payload ed in the Gateway Client Like (1) 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, http://scn.sap.com/community/gateway/blog/2014/03/06/let-s-code-crudq-and-function-import-operations-in-odata-service 15/23 10/31/2014 Let’s code CRUDQ and Function Import oper... | SCN 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 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 Regards, Chandra Like (0) akshath lt Mar 19, 2014 5:55 PM (in response to Chandrashekhar Mahajan) HI Chandra, I have requirement to both / PDF file into MIME repository or DMS through SAP UI5. I have tried to PDF file from the MIME repository and I have followed below steps. 1. Created FM for PDF . 1.1 Default path 'SAP/PUBLIC/