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 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.

How to color the complete column of the SAP ALV Display of type CL_SALV_COLUMN_TABLE

The method set_color of the class CL_SALV_COLUMN_TABLE can be used to color the column of SAP ALV Display.

The color codes are much similar to old ALV Display of type CL_GUI_ALV_GRID

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

Lr_all_columns = lr_salv_table->get_columns( ).

Lr_single_column ?= lr_all_columns->get_column(‘CONTACT_NAME’).

DATA ls_color_column TYPE lvc_s_scol.

Ls_color_column-col = 7.

Ls_color_column-int = 1.

Ls_color_column-inv = 1.

Lr_single_column->set_color( value = ls_color_column ).

The column ‘Contact_name’ is colored or highlighted in orange colour.

The following table shows the other different colour codes. It is same as the old ALV Display.

Colorcolor_codecolintinv
GreenC510510
RedC610610
OrangeC711711
Color Codes

What is the difference between check box and check box hotspot cell type in CL_SALV_TABLE?

CheckboxCheckbox Hotspot
It is used just as a display of checkbox. It mentions that the particular line of record is selected.It can be used to trigger an event of type LINK_CLICK.
The value of the checkbox cannot be changed.The value of checkbox can be changed.
It is not used to manipulate the values of the fields of ALV Table.It can be used to manipulate the values of the fields of ALV Table. The ALV Table must be refreshed with the refresh method call {lr_alv_grid -> REFRESH ()}.