How to enable adding new records to the SAP ALV List

The primary key fields of the data records must be unique. There should not be duplicate records in the Database table. The fields of the ALV List are editable through the field catalog. The primary key fields of the SAP ALV List cannot be edited directly. If a new line of record is inserted in to SAP ALV List, the primary key fields of the SAP ALV List are disabled by default. It is not possible to insert a new record in the ALV List through the edit field of the field catalog.

Contact_IdContact_nameContact_address
101RamStreet 10
102MahesStreet 11

If a new line of record is inserted in to above SAP ALV List, the field contact_id is disabled by default. The contact_id is the primary key field of the SAP ALV List. If a new line of record must be inserted in the Table, then the primary key fields of the SAP ALV List must be enabled for editing. The primary key fields of the existing data records must be disabled by default.

A new line of record can be inserted in to the ALV Table through the internal table lvc_t_styl. It is introduced to the structure of the internal table of data records. The keywords ‘INCLUDE STRUCTURE’ adds the existing structure of internal table which contains the data records. The following steps helps us to achieve this.

  • Define the structure with lvc_t_styl.
  • Define the stylename field of the layout
  • Define the style field of lvc_s_styl to CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

  • Define the structure with lvc_t_styl.

TYPES: BEGIN OF st_contact.

INCLUDE STRUCTURE ztt_db_table2.

TYPES: fieldstyle TYPE lvc_t_styl,

END OF st_contact.

DATA lt_contact TYPE TABLE OF st_contact.

  • Define the stylename field of the layout

DATA ls_layout TYPE lvc_s_layo.

ls_layout-stylename = ‘FIELDSTYLE’.

This layout is given as parameter while calling the set_table_for_first_display method of the ALV object.

  • Define the style field of lvc_s_styl to CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

This style is defined for the primary key fields of the database table. This style is set to each data record of the database table. But it is constrained to specific columns by specifying the fieldname of the data records. It is finally inserted in to the field style of each data record.

Key steps to set the style field are:

a) Define the fieldname of structure lvc_s_styl.

b) Define the style of structure lvc_s_styl.

c) Insert the structure lvc_s_styl in the fieldstyle field of each data record. Fieldstyle field is the internal table of type lvc_t_styl.

The selection of the style cl_gui_alv_grid=>mc_style_disabled blocks the fields of the respective column in editing mode. For example in the above table, the editing of column CONTACT_ID is deactivated.

DATA ls_style_record TYPE lvc_s_styl.

FIELD-SYMBOLS <ls_contact> TYPE ztt_db_table2.

LOOP AT lt_contact ASSIGNING <ls_contact>.

ls_style_record-fieldname = ‘CONTACT_ID’.

ls_style_record-style = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

INSERT ls_style_record INTO TABLE <ls_contact>-fieldstyle.

ENDLOOP.

The primary key fields of the table will now be available for insertion of new record. The insert button facilitates insertion of new record. The primary key fields of the ALV List for already existing data records are disabled for editing.

How to set the column as editable in SAP ALV Grid

One of the key advantage of the old ALV Models is that it enables the user to edit the values in the table.

There are two different ways to set the ALV column as editable.

a) Field Catalog

b) SET_READY_FOR_INPUT

a) Field Catalog: The fields such as field name is used to set the column name of the ALV. The field ‘EDIT’ of the field catalog structure must be set to abap_true. Let us look at the following example.

FIELD-SYMBOLS <ls_field_cat> TYPE lvc_s_fcat.

LOOP AT lt_field_cat ASSIGNING <ls_field_cat>.

IF <ls_field_cat>-fieldname = ‘contact_address’.

<ls_field_cat>-edit = abap_true.

ENDIF.

ENDLOOP.

Thus the editing mode of the ALV Grid for the respective columns are activated. The ALV Grid facilitates

additional buttons for editing data.

b) The other way of editing data is through the SET_READY_FOR_INPUT method of the CL_GUI_ALV_GRID class. The editing mode is activated or deactivated through the parameter i_ready_for_input.

For activation:

CALL METHOD lr_gui_alv_grid->set_ready_for_input

EXPORTING

i_ready_for_input = 1.

For deactivation:

CALL METHOD lr_gui_alv_grid->set_ready_for_input

EXPORTING

i_ready_for_input = 0.

The data is modified through the above technique. It is much similar to the conventional

DISPLAY <-> CHANGE options of the ABAP Editor.

once the data is edited, it must be saved initially to the internal table. Based on the needs, the data can be saved to the Database table. The method check_changed_data of the class CL_GUI_ALV_GRID is used to check if the data of the ALV Grid has been changed.

DATA lv_alv_changed TYPE c.

CALL METHOD lr_gui_alv_grid->check_changed_data

IMPORTING

c_valid = lv_alv_changed.

The variable lv_alv_changed holds the value ‘X’ or initial. If the data of the grid is changed, then it is ‘X’. If it is not changed, it’s value is INITIAL. Therefore the changes are saved to the Internal table. If it is required, it can also be saved to the Database table.