Create OData Service in SAP Fiori Server
Overview
- SAP Fiori apps use OData to display and update data in back-end server (SAP or Non-SAP).
- OData (Open Data Protocol) is a standardized protocol for creating and consuming data APIs.
- OData builds on core protocols like HTTP and commonly accepted methodologies like REST.
- Odata supports message formats like JSON, XML.
- OData provides definitions for:
- Simple Types
- Complex Types
- Associations between entries
- Navigation Paths between entries
- Custom behavior (known as function imports) beyond the standard QUERY, CREATE, READ, UPDATE, DELETE (QCRUD) operations
- Here we see how to create a Odata-Service which consumes RFC of SAP-R3 (Back-end) server.
- This is a example of business scenario where we need to display/update data of SAP-R3 (Back-end) server in SAP UI5 Application.
- SAP UI5 Applcation consumes Odata-Service which in-turn consumes RFC of SAP-R3.
- This blog is part of below parent blog:
Steps to create OData service (which consumes RFC):
This is example to consume RFC in OData-Service.
RFC of SAP-R3 (backen-end) Server:
- Functionality of RFC is like, on invoke, it returns MARA table details.
- RFC has one output table structure ‘TBL_MARA‘, in which 10 records will be fetched on execution
- below is RFC code to fetch 10 records from mara table
- This RFC will be consumed in OdataService of SAP-Fiori.
Steps to Create OData Service:
- Pre-requisites:
- RFC of SAP-R3 (back-end) system
- In SAP-Fiori (front-end) system:
- One RFC-Destination connecting to SAP-R3 (back-end) System
- T-code ‘SEGW’
- Go-to t-code ‘SEGW’ -> click on icon ‘Create Project’
- Enter details as shown in screen
- Click ‘ok’ icon, OData project gets created
- Now we need to create a “Entity-Type” which is meta structure to hold RFC table output
- Select ‘Entity-Types’ -> right click -. ‘Create’
- Enter names as per input.
- here ‘Entity Type’ is like Data Type and ‘Entity Set’ is like variable of Data Type.
- With ‘Entity Set’ name we consume ODataService in UI5 App
- Click ‘Ok’ icon
- Now, add column (property) in ‘Entity Type’
- Double Click on Properties -> In center panel click on ‘Append Row’ icon to add new elements
- For example purpose, we need only two column details Material Number and its Type, then create two elements
- And mark ‘Material’ as a key element
- Now Save and click on icon ‘Generate Runtime Objects’,
- Click ok in next pop-up
- Enter package details
- as a success below screen should appear
- Redefine EntitySet method to consume RFC
- Once Meta structure is been defined, next is to consume RFC
- For that go to ‘Service Implementation’ -> ‘*_DPC_EXT‘
- ‘_DPC_EXT‘ has method ‘_GET_EntitySet‘ which we need to Re-Define, for same we can follow below screen
- Once method gets re-defined, we can see it in folder Redefinitions.
- Now in ‘_GET_EntitySet’ method we need to do some ABAP Code logic
- When SAP UI5 Application calls this OdataService with this EntitySet name i.e. ‘MateriallistSet’, GET_ENTITYSET method gets invoked.
- Here we can write logic to call RFC of SAP-R3 (Back-end) server.
- RFC returns output in table format, which we need to map structure of OData-Entity-Type ‘Materiallist’
- Below code can be written for same:
-
method MATERIALLISTSET_GET_ENTITYSET. * Structure for RFC's mara table data TYPES: BEGIN OF ZMARA, MATNR TYPE C LENGTH 21, MTART TYPE C LENGTH 4, END OF ZMARA. DATA: IT_MARA TYPE TABLE OF ZMARA, "internal table to store RFC result wA_MARA TYPE ZMARA, "work area for single RFC result WA_ENTITY TYPE ZCL_ZTEST_ODATA_MPC=>TS_MATERIALLIST. "work area for Odata Entity 'MaterialList' * Calling SAR-R3 (Back-Ens) server RFC Call FUNCTION 'ZTEST_RFC_FIORI' DESTINATION 'rfcDestinationName' TABLES TBL_MARA = IT_MARA. * Return RFC output to Odata Entity output 'MaterialList' IF SY-SUBRC = 0. *if VALUE FOUND, then map output to Odata Entiy 'MaterialList' LOOP AT IT_MARA INTO WA_MARA. CLEAR WA_ENTITY. WA_ENTITY-MATERIAL = WA_MARA-MATNR. WA_ENTITY-MTYPE = WA_MARA-MTART. APPEND WA_ENTITY TO ET_ENTITYSET. "Append output to Odata Entiy 'MaterialList' CLEAR WA_MARA. ENDLOOP. ENDIF. endmethod.
- Now activate the ‘GET_ENTITYSET‘ method and ‘_DPC_EXT‘.
- Thus, a OData Service has been created, which consumes RFC of back-end system.
- Now next is to register the Service for same, we can follow below link:
- Once service gets registered, it can be consumed in SAP UI5 Application for display/upload of back-end data.
- We can test this ODataService in SAP-Fiori (front-End):
- t-code /n/iwfnd/gw_client
- url /sap/opu/odata/sap/ZTEST_ODATA_SRV/MaterialListSet
- Descritpion To get RFC detals via EntitySet ‘MaterialListSet’
- Test screen:
- Thus we have developed one OData Service in SAP-Fiori which consumes RFC of SAP-R3.
Thaks! this is very helpful, just one doubt, when we connect sapui5 with the odata if i pass a parameters to the odata the function module shuold be modified ? or we fetch all data with the rfc and inside the classes implemented in the odata we filter it ?
Dear Naoto,
Sorry for delayed response, I was too much busy in my current project….
Please find my comments about your queries:
Thanks & Regards
Dilip
Thanks Dilip Kumar KrishnaDev Pandey your post help me a lot! sometime i'd like to see one post about programming filters, search, query, navigation and so on of odata 😀
Dear Naoto,
That will be covered soon in my next blog .....that all we are already using...but for blog updation time required....
Thanks & Regards
Dilip
i'm looking forward for it, when i get some experience i'll do my own tutorial but in my matern lenguage, spanish.
Regards
Thanks @Dilip Kumar KrishnaDev Pandey
this is very helpful indeed.
hi, I have an issue here saying that I don't have authorize to create the project. how about it sir?
Hi Siti,
Where do you want to create project ? SEGW ?
Please ask your basis team to provide required developer user-id authorization
Thanks & Regards,
Dilip
Yes, I want to create a project in SEGW. But we do not have any basis team here. I am just exploring as I am new to SAP Fiori.
Hi Siti Hajar,
Sorry for late reply,
I hope by now you may have already addressed all required developer-roles.
However, just for information, you should have a look on below blog (a nice explanation of roles by Masayuki Sekihara ) and SAP-Link by SAP-Team:
https://blogs.sap.com/2015/07/09/sap-fiori-ll20-role-and-authorization-settings-for-sap-fiori-launchpad/
https://help.sap.com/viewer/a7b390faab1140c087b8926571e942b7/7.52.5/en-US/85be3fff35604fa09a1668dd97ef4407.html
Thanks & Regards,
Dilip
could you please help me with programming filters, search, query, navigation,pagination using top and skip, select,orderby etc in Odata with examples
Hi Ranjeet,
Please refer below SAP link to go through all operations:
https://help.sap.com/viewer/d599f15995d348a1b45ba5603e2aba9b/LATEST/en-US/a37167b95b504ca9b484c8e19e2c26bb.html
Thanks & Regards,
Dilip K K Pandey
Thanks Dilip for your blog....Can you post how to use associations.