How to resolve the Error ‘Database table must be a flat structure’ in SAP ABAP

DATA: BEGIN OF ps_struct.

INCLUDE STRUCTURE zss_db_table1. “ ==>Below syntax Error:

“ZSS_DB_TABLE! must be a flat structure.

                            “Internal tables, strings, references, and structures cannot be used as components.

DATA field4 TYPE i.

DATA: END of ps_struct.

DATA lt_internal_table TYPE TABLE OF ztt_db_Table1.

SELECT *FROM ztt_db_table1 INTO TABLE lt_internal_table.

LOOP AT lt_internal_table ASSIGNING FIELD-SYMBOL(<ls_struct>).

              MOVE_CORRESPONDING <ls_struct> TO ps_struct.

              ps_struct-field4 = ‘field4’.

              WRITE:/ ps_struct-field1.

ENDLOOP.

The error is due to the field3 component in the Database table. The Datatype of FIELD3 component is String.

FIELD1TypeZDEL_IDNUMC
FIELD2TypeZDEL_NAMECHAR
FIELD3TypeZDEL_DESCSTRING
ztt_db_Table1

Flat structure consists of all elementary data types except string and xstring. The error is resolved by changing the datatype of the component ‘FIELD3’.

How to Create an ALV Grid using CL_GUI_ALV_GRID in SAP ABAP

DATA lr_alv_grid TYPE REF TO cl_gui_alv_grid.

DATA pt_internal_table TYPE TABLE OF ztt_Db_Table2.

DATA lt_field_Catalog TYPE lvc_t_fcat.

CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’

              EXPORTING

                            i_Structure_name = ‘ZTT_DB_TABLE2’

              CHANGING

                            ct_fieldcat = lt_field_catalog.

lr_alv_grid = NEW cl_gui_alv_grid( i_parent = cl_gui_container=>default_screen ).

SELECT *FROM ztt_db_table2 INTO TABLE pt_internal_table.

DATA(lv_layout) = VALUE lvc_s_layo( grid_title = ‘Contact details’ ).

lr_alv_grid->set_table_for_first_display( EXPORTING is_layout = lv_layout i_save = ‘A’

CHANGING it_fieldcatalog = lt_field_catalog

it_outtab = pt_internal_table ).

WRITE: space.

How to create an ALV list using merged field catalog in SAP ABAP

The field catalog can be merged in order to add the new fields in the output ALV list. Let us consider the table ‘ztt_db_table1’. It has the details of the customer_id, name and Address. Now we would like to add an additional field active or inactive contact.

The following steps are necessary to populate the fields of the ALV list.

  1. Define a Local structure with the fields of the DDIC structure. Add the additional necessary fields. Active is the newly added field to the structure.
  2. Define an Internal table with this structure.
  3. Load the contents of the database table to the Internal table. The data can be assigned to the new field of the modified Internal table.
  4. Create the new column in the field catalog.
  5. Create a merged Field Catalog from the fields of the Database Table, Field Catalog. The SAP Function Module ‘REUSE_ALV_FIELDCATALOG_MERGE’ is used for this purpose.
  6. Display the contents of the newly merged field catalog using the ‘REUSE_ALV_GRID_DISPLAY’ function.

TYPES: BEGIN OF st_contact,

contact_id TYPE ZDE_Contact_id,

contact_name TYPE ZDE_Contact_name,

contact_address TYPE ZDE_Contact_address,

active TYPE abap_bool.

END of st_contact.

TYPES: tt_contact TYPE TABLE OF st_contact.

DATA pt_contact TYPE tt_contact.

DATA ls_contact TYPE st_contact.

DATA lt_table_content TYPE TABLE OF ztt_db_table1.

SELECT *FROM ztt_db_table1 INTO TABLE lt_table_content.

LOOP AT lt_table_content ASSIGNING FIELD-SYMBOL (<ls_tab_content>).

              MOVE-CORRESPONDING <ls_tab_content> TO ls_contact.

              ls_contact-active = abap-true.

              APPEND ls_contact TO pt_contact.

ENDLOOP.

DATA lt_field_catalog TYPE slis_t_fieldcat_alv.

DATA ls_field_catalog TYPE slis_fieldcat_alv.

ls_field_catalog-fieldname = ‘ACTIVE’.

ls_field_catalog-seltext_l = ‘Active’.

APPEND ls_field_catalog TO lt_field_catalog.

CALL FUNCTION ‘REUSE_ALV_FIELDCATALOG_MERGE’

EXPORTING

              i_structure_name = ‘ZTT_DB_TABLE1’

CHANGING
              ct_fieldcat = lt_field_catalog.

Verify the columns of the Field Catalog by printing out the columns using the LOOP AT.

LOOP AT lt_field_catalog ASSIGNING FIELD_SYMBOL (<ls_field_cat>).

              WRITE: <ls_field_cat>-fieldname.

ENDLOOP.

Finally we can display the contents in the ALV list using the below function.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’

              EXPORTING

                            it_fieldcat = lt_field_catalog.

              TABLES

                            t_outtab = pt_contact.

How to create a simple ALV List using field catalog.

The following two entries are necessary to populate the fields of the ALV list.

  1. Field Catalog
  2. Internal Table

DATA lt_field_Catalog TYPE slis_t_fieldcat_alv.

DATA ls_field_Catalog TYPE slis_fieldcat_alv.

DATA lt_internal_table TYPE TABLE OF ztt_db_table1.

ls_field_catalog-fieldname = ‘CONTACT_ID’

ls_field_catalog-seltext_l = ‘Contact_identification’.

APPEND ls_field_catalog TO lt_field_catalog.

ls_field_catalog-fieldname = ‘CONTACT_NAME’.

ls_field_catalog-seltext_l = ‘Contact name’.

APPEND ls_field_catalog TO lt_field_catalog.

ls_field_catalog-fieldname = ‘CONTACT_ADDRESS’.

ls_field_catalog-seltext_l = ‘CONTACT Address’.

APPEND ls_field_Catalog TO lt_field_catalog.

SELECT * FROM ztt_db_table1 INTO TABLE lt_internal_table.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’

              EXPORTING

                            It_field_cat = lt_field_catalog

              TABLES

                            T_outtab = lt_internal_table.

How to create a simple ALV List using DDIC structure

The following two entities are necessary to populate the fields of the ALV list.

a. Database Table or Structure

b. Internal Table

Contact_IdContact_NameContact_Address
101MikeStreet 10
102 RajStreet 11
103ThiruStreet 12
DATABASE TABLE : ZTT_DB_TABLE1

DATA lt_internal_table TYPE TABLE OF ztt_db_table1.

DATA ls_structure Like LINE OF lt_internal_table.

SELECT * FROM ztt_db_table1 INTO TABLE lt_internal_table

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’

              EXPORTING

                            i_structure_name = ‘ZTT_DB_TABLE1’

              TABLES

                            T_outtab = lt_internal_table

What is the difference between Upcasting and Downcasting in SAP ABAP?

Data lr_top_floor TYPE REF TO zcl_top_floor

Data lr_ground_floor TYPE REF TO zcl_ground_floor

lr_top_floor = New zcl_top_floor ().

lr_ground_floor = New zcl_ground_floor ().

Downcast:

When you stand in the top floor, the view of the properties such as roads, other houses are visible easily. The view is widened in the top floor. So, we can call it as widening cast When you are in the ground floor, the view is narrowing. The properties are not visible in the ground floor. Therefore it is called as Narrowing cast.

Let us assign the instance of the top floor to the ground floor. When you are able to view the other houses, people will always question your increased visibility. Therefore during the assignment, place a question mark before the ‘=’ sign.

lr_ground_floor ?= lr_top_floor.

Please remember the word Assignment to distinguish the upcast and downcast. In the above case, it is assigned to the ground floor (Downcasting) In other words it is moving down from top floor to ground floor. In this case remember the direction of movement.

So lr_ground_floor ?= lr_top_floor (more view) widening cast.

It is assigned to the ground floor(Downcast).

Upcast:

When you stand on the ground floor, the view is less. You will not be able to see the other properties. People will not question you, as the visibility will be less. Therefore there is no question mark during the assignment of the ground floor instance to the top floor instance.

lr_top_floor = lr_ground_floor (less view) Therefore, narrowing cast.

(Direction,Assignment)

It is assigned to the top floor reference variable. The direction is from ground to top, hence it is named as upcast.

What is the difference between Abstract and Interface?

PARAMETERSABSTRACTINTERFACE
What is it?Super base class for all sub classes.It is the skeleton. It contains the method and variable definition.
How are the methods and variables defined?The methods and variables can be public, protected and private.The methods and variables are always public. It is explicitly defined in the beginning like below.
INTERFACE zif_interface1 PUBLIC.
How is the instance created?An instance is created through the sub class.
DATA lr_super_abstract_class TYPE REF TO zcl_super_class.
lr_super_abstract_class=
NEW
zcl_sub_class ( ).
Abstract classes cannot be instantiated.
An instance is created through the implemented class.
In the class ‘ZCL_CLASS1’, Interface ‘ZIF_INTERFACE1’ is implemented.
Data lr_interface TYPE REF TO zif_interface1.
DATA lr_interface = NEW zcl_class1().
Interfaces cannot be instantiated.
How are the methods implemented?The methods can be implemented in the same super base abstract class except the abstract methods.The methods can be implemented only in the implementing class. So here the methods are implicitly defined as abstract.
How is it inherited?Sub class instance is created, which inherits the methods and variables of the super abstract class. In the sub class definition, it is explicitly inherited with the keyword ‘INHERITING FROM’ CLASS zcl_sub_class DEFINITION PUBLIC INHERITING FROM zcl_super_class.The class implements the interface. Interfaces can only be mention in the public section.
CLASS zcl_super_class DEFINITION PUBLIC ABSTRACT.
PUBLIC SECTION.
INTERFACES zif_interface1.
Should all methods be implemented? Is there any excuse?All abstract methods must be redefined and implemented in the sub class. Otherwise it gives an error in the immediate subclass inheriting the super class. It cannot be implemented in the super class.All methods from the interfaces must be implemented in the implementing class. This implicitly means that all the methods in the interface are abstract. Otherwise a warning will pop up during syntax check of the implementing class.
Syntax of Method Implementation?METHOD m_method1.
ENDMETHOD.
METHOD zif_interface1~method2.
ENDMETHOD.
We can als use aliases in method definition.
ALIASES method2 FOR zif_interface1~method2.
So in this case method implementation is similar to normal method.
METHOD method2.
ENDMETHOD.

Note : Multiple Inheritance is not allowed in SAP ABAP. SO the subclass can have only one super class. Two sub classes can have the same super class. The multiple inheritance is achieved by implementing more than one interface in the class definition.

Below picture depicts a classic example of Abstract and interface concept

The parent will always provide all the essentials for the child. But the parent will not take back anything from the child. They don’t create the wealth for themselves. In such a perspective, the Abstract base super class cannot be instantiated. only the base class can instantiate. The child must also inherit the good values given by the parents. Abstract super class will implement the methods which can be inherited by the child sub class.

Each and every parent performs the common functionalities such as getting groceries, paying school fees, planning education and cooking food. The common functions can be grouped together. Similary the Interface provides the common functions which can be implemented by every class. Interface has only the definition. The parents will be able to implement these functions based on their financial capacity. So each parent super class will implement their own methods.

How to use Exception Texts in SAP ABAP

  1. Create an Exception class. The class inherits the standard SAP Exception Class such as CX_STATIC_CHECK.
  2. Add the necessary attributes in the Attributes Tab of the Exception class. The Level is generally defined as Instance Attribute and its visibility is Public.
  3. Add the Exception ID in the Texts Tab of the Exception class. Mention the Exception Text in the Text column. The Attributes included in the Exception Text refers to the added attributes in the attributes tab.

What happens in the Background when Texts ID (Exception text ) is added ?

An attribute is automatically created of type ‘ SOTR_CONC’ which is a static constant with public visibility. It can be under the Attributes Tab of the Class Builder.

The exception texts are used in ABAP progam using the constructor of the exception class. The parameters such as textid are passed. The other parameters such as attributes and previous are passed only when it is needed. The attributes refers to the attributes in the Exception text.

How can you make yourself clear with this Exception Texts

  1. In the class Builder under Texts Tabreiter : It is mentioned as Exception text.
  2. In the class Builder under Attributes Tabreiter : It is mentioned as Attribute. Below are its properties

Attribute : Exception Id

Level : Constant

Visibility : Public

Associated Type : SOTR_CONC

Initial Value : It will hold a random value

3. In the constructor of the exception class : It is mentioned as textid.

RAISE EXCEPTION NEW zcx_exception_class ( textid = zcx_exception_class => exception_id ) .

How to use Attributes in Exception Texts

  1. In the attributes tab of the Class Builder. Add the below instance attribute of respective type.

Attribute1

2. Mention the attributes in the Exception texts. This is done under the Texts Tab.

Exception Id | Text

exception_id | An Exception was raised from the class &Attribute1&

3. In the constructor of the Exception class, mention the Attribute along with the textid

RAISE EXCEPTION NEW zcx_exception_class ( textid = zcx_exception_class => exception_id

Attribute1 = attribute1_value ).

How to use previous in the Exception class constructor

TRY.

lt_check = throw_exception _1 ( ) .

CATCH zcx_exception_1 INTO DATA ( lx_exception_1).

RAISE EXCEPTION NEW zcx_exception_2 ( text_id = zcx_exception_2 => exception_id

Attribute1 = attribute_value

previous = lx_exception_1 ).

ENDTRY.

Exception CX_SY_ITAB_NOT_FOUND in Table Expressions

This exception occurs when the specified table row is not found in the internal table.

The Database Table ztt_db_table2 contains the following entries.

Contact_id Contact_name Contact_address

DATA lt_internal_table TYPE STANDARD TABLE OF ztt_db_table2.

DATA ls_structure TYPE ztt_db_table2.

SELECT contact_id contact_name contact_address FROM ztt_db_table2 INTO TABLE lt_internal_table.

TRY.

IF lt_internal_table [ contact_id = 105 ] – contact_address is initial.

WRITE:/ ‘ Contact address is initial ‘ .

ENDIF.

CATCH cx_sy_itab_line_not_found INTO DATA (lx_errpr) .

ENDTRY.

But where as in Read Table command, the following occurs

READ TABLE lt_internal_table TRANSPORTING NO FIELDS WITH KEY contact_id = 105.

IF sy-subrc = 0.

WRITE:/ ‘Data Row exists’.

ELSE.

WRITE:/ ‘Data Row does not exists’.

ENDIF.

One of the difference between READ Table and Table expressions is accessing a Record which does not exist.

Table expressions : It throws a Runtime Exception CX_SY_ITAB_LINE_NOT_FOUND error.

READ TABLE : The READ TABLE query will be unsuccessful. It returns the value 4 for sy-subrc.