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 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 ()}.

How to use various cell types in new SAP ALV Display of type CL_SALV_TABLE?

               The various cell types can be used both in container mode and complete mode. The cell types are constructed based on the Attribute of the Interface ‘IF_SALV_C_CELL_TYPE’.

Type of cellAttribute of IF_SALV_C_CELL_TYPEDisplay mode container / completeEvent
Text=> TextBoth
Checkbox=> CheckboxBoth
Checkbox hotspot=> checkbox_hotspotBothlink_click
Button=> ButtonBoth link_click
Dropdown=> dropdownContainer
Hotspot=> HotspotBoth link_click

The method set_cell_type of the column object ( cl_salv_column_table ) can be used to set the cell type for the cells in the ALV Display.

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_alv_grid->get_columns ().

lr_single_column ?= lr_all_columns->get_column (‘Contact_name’ ).

lr_single_column->set_cell_type (if_salv_c_cell_type =>Button).

How to optimize the width of the column of type CL_SALV_COLUMNS_TABLE

               The width of the column is optimized through three methods.

  1. Optimize all columns.
  2. Optimize one single column.
  3. Manually set the width of columns.

The optimization is based on the content of the ALV Table.

  1. Optimize all columns.

In order to optimize all the columns of the ALV Table, call the set_optimize method of the columns object.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

lr_all_columns = lr_alv_grid->get_columns ( ).

lr_all_columns->set_optimize ( ).

  • Optimize one single column.

‘SET_OPTIMIZED ( )’ method of the cl_salv_column_table is used to optimize the single column of the ALV Table.

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_alv_grid -> get_columns ( ).

lr_single_column ?= lr_all_columns -> get_column ( ‘Contact_name’ ).

lr_single_column -> set_optimized ( abap_true ).

  • Manual adjustment of column width.

The method set_output_length of the class cl_salv_column_table can be used to set the width of the particular column to a specific predefined value.

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_alv_grid -> get_columns ( ).

lr_single_column ?= lr_all_columns -> get_column ( ‘Contact_name’ ).

lr_single_column -> set_output_length ( 50 ).

The above manual adjustment of column width will be ignored, if the columns are optimized with the set_optimize ( ) method.

How to set the column position using the class CL_SALV_COLUMNS_TABLE?

The column position in the ALV Display can be controlled using the set_column_position method of the class CL_SALV_COLUMNS_TABLE. There are two parameter used in the method call. The column name identifies the column to be positioned. The position parameter mentions the position of the column in ALV Display.

Let us look at the following example:

DATA lr_salv_columns_table TYPE REF TO cl_salv_columns_table.

lr_salv_columns_table = lr_alv_grid->get_columns ().

lr_salv_columns_table->set_column_position ( columnname = ‘CONTACT_ADDRESS’

position = 3).

What is the difference between data reference and object reference in SAP ABAP

PARAMETERDATAOBJECT
DEFINITIONDATA lr_ref TYPE REF TO data.DATA lr_object TYPE REF TO object.
CREATIONCREATE DATA lr_ref TYPE REF TO (‘ZIF_INSTANCE’).CREATE OBJECT lr_object TYPE (‘ZCL_CLASS’).
DEREFERENCEIt can be dereferenced. The object or any value can be assigned to dereferenced reference variable.
lr_ref->* = lr_object
Object references cannot be dereferenced. This notation
->* cannot be used.
CONVERSION OR CASTINGHow to convert a value or an object in to a reference:
it can be converted in the following two ways.
lr_ref->* = value or object
lr_ref = REF #(value/obj)
How to cast an object.
lr_object2 = CAST zcl_object2( lr_object ).
USAGEused to create data referencesUsed to create objects for classes.
ASSIGNMENTAn object or any value cannot be assigned to a data reference variable. It must be deferenced in order to assign a value.
lr_ref->* = lr_object.
lr_ref->* = ‘string_value’
Only objects can be assigned. It can be assigned directly without dereference.
DATA lr_object TYPE REF TO object.
DATA lr_object2 TYPE REF TO object.
CREATE OBJECT lr_object TYPE (‘ZCL_CLASS’).
lr_object2 = lr_object.
POSSIBLE SYNTAX ERRORAs mentioned before, values cannot be assigned directly without dereference.
For example:
lr_ref = ‘String_value’. This line of code will result in the below Error.
“The type of ‘lr_interface1’ cannot be converted to the type of ‘lr_ref’
If the types of data objects are different, then it must be casted during the assignment. Otherwise you will get the below error.
‘The type of ‘lr_object’ cannot be converted to the type of ‘lr_object2’.