Saturday, 6 July 2019

How to create OData for GET operation in SAP Cloud for Customer System ?

Open Data Protocol (OData) is an open protocol which allows the creation and consumption of quarriable and inter-operable RESTful APIs in a simple and standard way.

This blog post covers the step by step process for beginners on how to create the OData service for GET operation in the SAP Cloud for Customer System.

The retrieval of the data for GET operation can be achieved in two ways,

1. By Selecting the Standard Business Object


This method of retrieval of data from Standard BO would be quite straight forward. Steps need to be followed

◈ Create OData Service

We need to provide following information

◈ Service Type
◈ Proxy Name
◈ Service Name
◈ Namespace
◈ Package Name (Optional)
◈ Transport Request (Optional)
◈ Exit Class name (explained in later section) (optional)

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

◈ Select BO

Once service is created Edit the service, and select the BO for which you want to implement the GET service.

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

◈ Create Entity Types

Once BO is selected Choose the field of Standard BO which should be reflected into the GET service fields these fields are nothing but the Properties of OData service.

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

◈ Save and Activate the service

After activation of the service, Service URL will be generated on the top. Which will provide metadata information. Which includes entity types, properties and collection name.

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

◈ By using service URL concatenated with collection name we get the data.

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

2. By Implementing Custom Class


This type of implementation of OData Service has more to deal with the creation of custom class called Exit class and writing the data retrieval logic in the methods. We use this type of implementation when data is need to be retrieved from the table or non-standard BO or has got some complex logic to fetch the data and which can be accomplished by coding.

Following are the steps involved

◈ Create OData service

Create the OData service, we should provide the exit class name which we will create in the back end.

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

In my case it is ‘ZODATA_EXIT_CLASS’.

◈ Create Exit class

Create the Exit class which we have mentioned in the OData. We should mention the Super Class ‘CL_AP_ODATA_EXIT’ which will import the interfaces which intern will provide the methods where we accomplish following task by Overriding (re-implementing) the inherited methods

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

◈ For creating the Entity Type, We should Override the method:

IF_AP_ODATA_EXIT~GET_ENTITY_TYPE_NAMES

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

◈ For creating Entity Type Properties, We should Override the method:

IF_AP_ODATA_EXIT~GET_ENTITY_TYPE_PROPERTIES

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

◈ Implement the logic for data retrieval

Write the logic for data retrieval in the method IF_AP_ODATA_EXIT~GET_DATA

And assign the data to changing parameter ct_data

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Tutorials and Materials

Now here we are done with the implementation of the OData service, by browsing the service URL we get details about the service including entity types.

We can  also combine both the methods of creating OData service when we need to design a complex OData to retrieve data from BO and/or from other data source.

No comments:

Post a Comment