How to create an OData Service in SAP

  1. The OData service is developed on a new project in the SAP system. Therefore start with the Transaction SEGW.
  2. Click New in the toolbar.
  3. Please enter a new project name and a short description on the popup fenster. Define also a package and finally confirm the entries through tick mark.
  4. A new project is now created. There are four folders under the new project. Right click on the folder ‘DATA MODEL’ and choose import from DDIC structure. The already available ABAP Dictionary structure can be imported directly and it is not necessary to manually assign an OData-service to each field. In addition to the ABAP structures, you can also import Type definitions from BAPI-Function module parameters or search help.
  5. In the popup fenster, provide the entity type and also provide the structure SFLIGHT in the respective fields. Ensure that the option Create default entity set is selected. Finally click the ‘Next’.
  6. In the next window, you will see all the fields of the table SFLIGHT. Now click all the fields of the structure, except the field MANDT. Click ‘Next’.
  7. In the next window, you will see all the fields with their respective Datatype. The primary keys of this structure must be confirmed through the checkbox column ‘IS KEY’. So please select the checkbox for the respective primary key fields of the structure. In this case, let us choose the CARRID, CONNID, FLDATE as our primary keys. Once you are done with the selection, please confirm the entries by selecting the ‘FINISH’ Button.
  8. As you can see, the Data model consists of the Entity types and Entity sets. The Entity Type Flights contains the properties folder. The properties folder contains all the fields of the structure. It displays the Edm core type and Label which describes the fields. The entityset ‘Flightsset’ is created as we checked this option ‘Create Default Entity Set’.
  9. The CRUDQ Methods of the Entityset is available in the folder service implementation.

How to construct an OData service in SAP ABAP

ODATA SERVICE CONSTRUCTION

Entity container: It contains all the entity sets.

Entity set: It represents a table. It is based on the entity type.

Entity type: All the fields(properties) and the primary keys are mentioned.

Property: It represents the fields along with data type.

Entity: It describes a line of record in the data table.

Association: It connects the primary and foreign key fields. It represents the cardinality of the relationship (1:1, 1: n).

Navigation: The two different entity sets can be related and represented through the navigation.

What is an OData service document

OData service document represents a service which includes entity sets, uniform resource identifier (URI)

It can be opened through the browser with the URI / URL

For example, let us consider the following OData service.

http://services.odata.org/v3/OData/OData.svc/

If you open this URL in browser, it shows up the different entity sets.

Entitysets shown in Browser

As you can see in the above image, the entitysets in this OData service are Products, Productdetails, Categories, Suppliers, Persons, PersonDetails, Advertisements. So it includes totally seven entitysets. The entityset ‘Category’ includes information about all categories.

If the categories is added to the above URL like

http://services.odata.org/v3/OData/OData.svc/Categories

The above url in the browser would list down all the categories.

Similarly if you wish to see all the Products. Then follow this link

http://services.odata.org/v3/OData/OData.svc/Products

URL is case sensitive. So the first letter ‘P’ in Products must be written in Capital. Otherwise it does not work.

It is much similar to the Transaction SE11 (ABAP Dictionary) where we give the Table name. The Data contents can be displayed for this table.

The URLs mentioned above executes the HTTP-Get methods, which in turn calls the Read-OData method. In the background, the data would be retrieved and the result is sent back.

Let us look into an example of retrieving data records with single primary key.

Specific line of record can also be retrieved by specifying the primary key value in the URL.

http://services.odata.org/v3/OData/OData.svc/Products(5)

In the above URL, the products with the ID 5 will be retrieved.

Retrieving data records with two primary keys.

/Entityset( p_key1 = ‘Value1’, p_key2 = ‘Value2’ )

In the above case, both the primary key fields are of datatype string. So the value is mentioned in quotes. If the datatype is integer, then quotes should not be used.

Retrieving data records with WHERE clause.

All the products with a particular category ID can also be retrieved. To achieve this, it must be predefined in the services. We can also navigate from one entity set to another. For example, in the below URL, only the data from the product entity set are displayed.

https://services.OData.org/v3/OData/OData.svc/Categories(5)/Products

Retrieving data records from two different entity sets

This option is much like the JOIN query in SQL. The data from two different entity sets are displayed with a WHERE condition.

https://services.OData.org/v3/OData/OData.svc/Categories(2)?$expand=Products

How does OData service works

OData services works through entity sets and entities. Data is represented in the form of Tables.

Tables are referred as Entity sets and the lines of record are referred as entities.

The Data is accessed through the following OData methods. These OData methods are referred as CRUD methods.

OData MethodHttp MethodDescription and its relation to sql command
CreatePOSTIt creates a new record. It is similar to INSERT command in SQL
ReadGETIt reads the records. It is similar to SELECT command in SQL.
The Read method from OData can be further classified.
For example, the Read method is used to read only one record.
The Query method is used to read more lines of record.
UpdateUPDATEIt updates the records. It is similar to UPDATE command in SQL
DeleteDELETEIt deletes the records. It is similar to DELETE command in SQL

What is OData service in SAP

  • It is a http type protocol
  • The protocol is between the other applications (Web browser etc) and SAP system.
  • The OData-open data protocol supports two formats Atom and JSON. It can also be controlled in different ways through URLs.
  • It follows the REST Architecture (Client/Server).
  • The Data from the source side and the server side is identified through the uniform resource identifier.
  • OData service is a logical data model. The Data is stored as entities.
  • The OData service is identified through URL/URI.

How to do Sorting and grouping in ALV Table through CL_SALV_SORTS

The sorting and grouping of data in ALV Table can be programmatically achieved. The method add_sort( ) of the class CL_SALV_SORTS can be used to sort or group the data. This sorting object is created from the get_sorts( ) method of the ALV Object.

DATA lr_salv_sorts TYPE REF TO cl_salv_sorts.

Lr_salv_sorts = lr_salv_table->get_sorts( ).

Lr_salv_sorts->add_sort( EXPORTING columnname = ‘AGE’

                                                          sequence       = if_salv_c_sort=>sort_up ).

The following sorting and grouping options are available.

If_salv_c_sort=>sort_none.

If_salv_c_sort=>sort_up.

If_salv_c_sort=>sort_down.

If_salv_c_sort=>group_with_underline.

If_salv_c_sort=>group_with_newpage.

If_salv_c_sort=>group_none.

How to do Aggregation using the class CL_SALV_AGGREGATION

The column of the ALV Display can be aggregated using the add_aggregation( ) method of the class CL_SALV_AGGREGATION.

DATA lr_salv_aggregation TYPE REF TO cl_salv_aggregation.

Lr_salv_aggregation = lr_salv_table=>get_aggregations( ).

Lr_salv_aggregation->set_aggregation_before_items( abap_true ).

Lr_salv_aggregation->add_aggregation( columnname = ‘AGE’

                                                                aggregation  =  if_salv_c_aggregation=>average ).

The following aggregation types are available.

If_salv_c_aggregation=>minimum.

If_salv_c_aggregation=>maximum.

If_salv_c_aggregation=>average.

If_salv_c_aggregation=>total.

If_salv_c_aggregation=>none.

How to fill icons in SAP ALV Display of type CL_SALV_TABLE

In order to fill icons in ALV Table, we must add an additional field of type ICON_D. The type ICON_D is of datatype CHAR with length 4. This is later filled with the icon code.

Step1: Define the icon field

TYPE-POOLS: icon.

TYPES: BEGIN OF ty_contact.

                  INCLUDE STRUCTURE ztt_db_table2.

                  TYPES: icon_field TYPE icon_d,

               END OF ty_contact.

Step2: Fill the internal table with data contents.

DATA lt_contact TYPE TABLE OF ty_contact.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

Step3: Fill the internal table with icon code.

FIELD-SYMBOLS <ls_contact> TYPE ty_contact.

LOOP AT lt_contact ASSIGNING <ls_contact>.

               <ls_contact>-icon = icon_positive.

ENDLOOP.

How to color the cells of SAP ALV Display of type CL_SALV_COLUMNS_TABLE

Step 1:

Desired cells of the SAP ALV Display can be colored using the structure lvc_s_scol. The color codes must be set to each line of record. Therefore the structure of the ALV Display must be extended with the additional field with an internal table of type lvc_t_scol.

TYPES: BEGIN OF ty_contact.

              INCLUDE STRUCTURE ztt_db_table2.

              TYPES: color_field TYPE lvc_t_scol,

              END OF ty_contact.

DATA ls_color_field TYPE lvc_s_scol.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lt_contact TYPE TABLE OF ty_contact.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

Lr_all_columns = lr_salv_table->get_columns( ).

Step2:

Make the ALV Table recognize the newly added ‘color_field’ with the help of the method set_color_column( ) of class CL_SALV_COLUMNS_TABLE.

Lr_all_columns->set_color_column( ‘COLOR_FIELD’ ).

Step3:

Finally fill the internal table ( new field ) with the desired color codes.

DATA ls_contact TYPE ztt_db_table2.

LOOP AT lt_contact ASSIGNING ls_contact WHERE age > 30.

              Ls_color_field-fname = ‘CONTACT_ADDRESS’.

              Ls_color_field-color-col = 7.

              Ls_color_field-color-int = 1.

              Ls_color_field-color-inv = 1.

              APPEND ls_color_field TO ls_contact-color_field.

ENDLOOP.