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.