In SAP ABAP, CRUD (Create, Read, Update, Delete) operations form the foundation of most applications. They are essential for managing data in business scenarios. Using OALV (Object-Oriented ALV) containers, we can create interactive, user-friendly screens that allow end users to perform CRUD operations with ease. In this blog, Iβll demonstrate how to implement these operations step-by-step using OALV containers.
Steps To acheive crud operation using OALV containers
Fetch the record and store in the internal table.Call the specified ScreenCreate the container object using cl_gui_custom_containerCreate the OALV grid inside the custom containerEnable the editable mode for CRUD operationPopulate the field catalog based on the structure of the internal table Used in alv grid. Display the initial data using the method set_table_for_first_display. Add the toolbar events of cl_gui_alv_grid to create the button like create And delete. Define and handle events for the crud operation.
Let me explain with a scenario An airline admin wants to manage flights in the system. The system provides functionality to retrieve, create, update, and delete flight records using a user-friendly interface. This includes details such as the flight number, destination, departure time, arrival time, and status. Here i have a flight database table named as zsan_dt_flight. Here I am retreiving available flights based on the country from and country to.
Here i created one report program and maintained all the data declarations.
REPORT ZSAN_RP_OALV_CRUD.
INITIALIZATION .
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001 .
PARAMETERS : P_cnfrom type ZSAN_DE_CNTRYFROM MATCHCODE OBJECT
ZSAN_ESH_CFROM ,
P_cnto type ZSAN_DE_CNTRYTO MATCHCODE OBJECT
ZSAN_ESH_CTO .
SELECTION-SCREEN end of BLOCK b1 .
DATA : gt_flight_det TYPE TABLE OF zsan_dt_flight,
gs_flight_det TYPE zsan_dt_flight,
gt_flight_old TYPE TABLE OF zsan_dt_flight ,
gs_flight_new TYPE zsan_dt_flight ,
gt_flight_new type table of zsan_dt_flight ,
lo_cust TYPE REF TO cl_gui_custom_container,
lo_grid TYPE REF TO cl_gui_alv_grid ,
ls_layo TYPE lvc_s_layo,
lt_fieldcat TYPE lvc_t_fcat ,
CHECK .
types : tt_flight_old type table of zsan_dt_flight ,
tt_flight_new type table of zsan_dt_flight .
FIELD-SYMBOLS : <ft_flight_new> type tt_flight_new ,
<fs_flight_new> type zsan_dt_flight ,
<ft_flight_old> type tt_flight_old ,
<fs_flight_old> type zsan_dt_flight .
ASSIGN gt_flight_old to <ft_flight_old> .
ASSIGN gt_flight_new to <ft_flight_new> .
assign gs_flight_new to <fs_flight_new> .
Need to fetch the data from the relevant tables and call the specified screen and build layout for enabling editable mode.
START-OF-SELECTION .
PERFORM fetch_data.
call SCREEN β100β .
INCLUDE zsan_rp_fetch_data.
*&βββββββββββββββββββββββ*
*& Form fetch_data
*&βββββββββββββββββββββββ*
*& text
*&βββββββββββββββββββββββ*
*& β> p1 text
*& <β p2 text
*&βββββββββββββββββββββββ*
FORM fetch_data .
SELECT FROM zsan_dt_flight FIELDS * WHERE countryfr = @P_cnfrom AND countryto = @p_cnto
into table _flight_det .
perform build_layout.
ENDFORM.
*&βββββββββββββββββββββββ*
*& Form build_layout
*&βββββββββββββββββββββββ*
*& text
*&βββββββββββββββββββββββ*
*& β> p1 text
*& <β p2 text
*&βββββββββββββββββββββββ*
FORM build_layout .
ls_layo = VALUE #( edit = βXβ col_opt = βXβ ) .
ENDFORM.
Create the container object using cl_gui_custom_container.
Create the OALV grid inside the custom container
*βββββββββββββββββββββββ-*
***INCLUDE ZSAN_RP_OALV_CRUD_STATUS_01O01.
*βββββββββββββββββββββββ-*
*&βββββββββββββββββββββββ*
*& Module STATUS_0100 OUTPUT
*&βββββββββββββββββββββββ*
*&
*&βββββββββββββββββββββββ*
MODULE status_0100 OUTPUT.
* SET PF-STATUS βxxxxxxxxβ.
* SET TITLEBAR βxxxβ.
if lo_cust is not BOUND .
CREATE OBJECT lo_cust
EXPORTING
* parent = β Parent container
container_name = βCONTAINERβ
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
endif .
if lo_cust is BOUND .
create OBJECT lo_grid
EXPORTING
i_parent = lo_cust β Parent Container .
endif .
CALL FUNCTION βLVC_FIELDCATALOG_MERGEβ
EXPORTING
* I_BUFFER_ACTIVE =
I_STRUCTURE_NAME = βZSAN_DT_FLIGHTβ
* I_CLIENT_NEVER_DISPLAY = βXβ
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
.
set HANDLER handle_event=>handle_toolbar for lo_grid .
set HANDLER handle_event=>handle_user for lo_grid .
lo_grid->set_table_for_first_display(
EXPORTING
i_structure_name = βZSAN_DT_FLIGHTβ β Internal Output Table Structure Name
is_layout = ls_layo β Layout
CHANGING
it_outtab = gt_flight_det β Output Table
it_fieldcatalog = lt_fieldcat β Field Catalog
EXCEPTIONS
invalid_parameter_combination = 1 β Wrong Parameter
program_error = 2 β Program Errors
too_many_lines = 3 β Too many Rows in Ready for Input Grid
others = 4
).
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDMODULE.
In the Process after input write the logic to enable the pfstatus back cancel and leave button.
*βββββββββββββββββββββββ-*
***INCLUDE ZSAN_RP_OALV_CRUD_USER_COMMI01.
*βββββββββββββββββββββββ-*
*&βββββββββββββββββββββββ*
*& Module USER_COMMAND_0100 INPUT
*&βββββββββββββββββββββββ*
* text
*βββββββββββββββββββββββ-*
MODULE user_command_0100 INPUT.
case SY-UCOMM .
WHEN βBACKβ OR βCANCELβ OR βEXITβ .
LEAVE to SCREEN 0 .
ENDCASE .
ENDMODULE.
Add the toolbar events of cl_gui_alv_grid to create the button like create And delete.
CLASS handle_event DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive.
CLASS-METHODS:handle_user FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS.
We can make use of standard table type ttb_button to add, modify, control toolbar buttons in alv.
FUNCTION ICON QUICKINFO BUTN_TYPE DISABLED TEXT CHECKED are the fields available in the predefined stucture STB_BUTTON.
CLASS handle_event IMPLEMENTATION .
METHOD handle_toolbar.
e_object->mt_toolbar = VALUE #( base e_object->mt_toolbar (
function = βSAVEβ
icon = ICON_SYSTEM_SAVE
butn_type = 0
text = βSAVEβ
quickinfo = βSAVEβ) (
function = βDELETEβ
icon = ICON_DELETE
quickinfo = βdelete recordβ
butn_type = 0
text = βDELETEβ
) ) .
ENDMETHOD.
METHOD handle_user.
CASE e_ucomm.
WHEN βSAVEβ.
PERFORM update_data_base.
when βDELETEβ .
PERFORM DELETE_DATA_BASE .
ENDCASE.
ENDMETHOD.
ENDCLASS .
Define and handle events for the crud operation.
*&βββββββββββββββββββββββ*
*& Form update_data_base
*&βββββββββββββββββββββββ*
*& text
*&βββββββββββββββββββββββ*
*& β> p1 text
*& <β p2 text
*&βββββββββββββββββββββββ*
FORM update_data_base .
lo_grid->check_changed_data(
IMPORTING
e_valid = check β Entries are Consistent
* CHANGING
* c_refresh = βXβ β Character Field of Length 1
).
IF gt_flight_det NE gt_flight_old .
LOOP AT gt_flight_det ASSIGNING FIELD-SYMBOL(<fs_flight_det>) .
MOVE-CORRESPONDING <fs_flight_det> TO <fs_flight_new> .
APPEND INITIAL LINE TO <ft_flight_new> ASSIGNING <fs_flight_new> .
ENDLOOP .
CALL FUNCTION βZUPD_FM_FLIGHT_UPDATEβ IN UPDATE TASK
EXPORTING
gt_upd_flight_det = <ft_flight_new> .
COMMIT WORK . βUpdate function moduleβ
IF sy-subrc = 0 .
MESSAGE s055(zcl_san_msg) . βData Updated Successfullyβ
ELSE .
MESSAGE e080(zcl_san_msg) DISPLAY LIKE βIβ .
βData is Not updated to the databaseβ
ENDIF .
ENDIF .
ENDFORM.
DATA: lt_selected_rows TYPE lvc_t_row,
lv_index TYPE lvc_index,
ls_row TYPE lvc_s_row.
CALL METHOD lo_grid->get_selected_rows
IMPORTING
et_index_rows = lt_selected_rows β Indexes of Selected
Rows
* et_row_no = β Numeric IDs of Selected Rows
.
IF lt_selected_rows IS INITIAL.
MESSAGE βNo rows selected for deletionβ TYPE βIβ.
RETURN.
ENDIF.
LOOP AT lt_selected_rows INTO ls_row.
lv_index = ls_row-index.
β Get the corresponding row from the internal table
READ TABLE gt_flight_det INTO gs_flight_det INDEX lv_index.
IF sy-subrc = 0.
β Delete the record from the database
DELETE FROM zsan_dt_flight WHERE airline_code
= gs_flight_det-airline_code AND
flight_conn_no = gs_flight_det-flight_conn_no
AND airport_to = gs_flight_det-airport_to .
IF sy-subrc = 0.
β If deletion was successful, remove from the internal table
DELETE gt_flight_det INDEX lv_index.
ELSE.
MESSAGE e081(zcl_san_msg) .
ENDIF.
ENDIF.
ENDLOOP.
CALL METHOD lo_grid->refresh_table_display.
MESSAGE s060(zcl_san_msg) .
ENDFORM.
Execute the report progaram.
Read
To retreive the flight details from the selected country.
Update
Admin can update the flight details based on the requirements.
Here the data is updated successfully.
Insert
Admin can insert the new flights to the existing database table. Click on append row.
Click on save
click on save
One entry is created in the database table. also multiple entries can be created by clicking on append row multiple times.
Delete
Select the flights (entry) and click on the delete button.
the selected flight entry is deleted from the database table.
β In SAP ABAP, CRUD (Create, Read, Update, Delete) operations form the foundation of most applications. They are essential for managing data in business scenarios. Using OALV (Object-Oriented ALV) containers, we can create interactive, user-friendly screens that allow end users to perform CRUD operations with ease. In this blog, Iβll demonstrate how to implement these operations step-by-step using OALV containers.Steps To acheive crud operation using OALV containersFetch the record and store in the internal table.Call the specified ScreenCreate the container object using cl_gui_custom_containerCreate the OALV grid inside the custom containerEnable the editable mode for CRUD operationPopulate the field catalog based on the structure of the internal table Used in alv grid. Display the initial data using the method set_table_for_first_display. Add the toolbar events of cl_gui_alv_grid to create the button like create And delete. Define and handle events for the crud operation.Let me explain with a scenario An airline admin wants to manage flights in the system. The system provides functionality to retrieve, create, update, and delete flight records using a user-friendly interface. This includes details such as the flight number, destination, departure time, arrival time, and status. Here i have a flight database table named as zsan_dt_flight. Here I am retreiving available flights based on the country from and country to.Here i created one report program and maintained all the data declarations. REPORT ZSAN_RP_OALV_CRUD.
INITIALIZATION .
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001 .
PARAMETERS : P_cnfrom type ZSAN_DE_CNTRYFROM MATCHCODE OBJECT
ZSAN_ESH_CFROM ,
P_cnto type ZSAN_DE_CNTRYTO MATCHCODE OBJECT
ZSAN_ESH_CTO .
SELECTION-SCREEN end of BLOCK b1 .
DATA : gt_flight_det TYPE TABLE OF zsan_dt_flight,
gs_flight_det TYPE zsan_dt_flight,
gt_flight_old TYPE TABLE OF zsan_dt_flight ,
gs_flight_new TYPE zsan_dt_flight ,
gt_flight_new type table of zsan_dt_flight ,
lo_cust TYPE REF TO cl_gui_custom_container,
lo_grid TYPE REF TO cl_gui_alv_grid ,
ls_layo TYPE lvc_s_layo,
lt_fieldcat TYPE lvc_t_fcat ,
CHECK .
types : tt_flight_old type table of zsan_dt_flight ,
tt_flight_new type table of zsan_dt_flight .
FIELD-SYMBOLS : <ft_flight_new> type tt_flight_new ,
<fs_flight_new> type zsan_dt_flight ,
<ft_flight_old> type tt_flight_old ,
<fs_flight_old> type zsan_dt_flight .
ASSIGN gt_flight_old to <ft_flight_old> .
ASSIGN gt_flight_new to <ft_flight_new> .
assign gs_flight_new to <fs_flight_new> . Need to fetch the data from the relevant tables and call the specified screen and build layout for enabling editable mode. START-OF-SELECTION .
PERFORM fetch_data.
call SCREEN β100β .
INCLUDE zsan_rp_fetch_data.
*&βββββββββββββββββββββββ*
*& Form fetch_data
*&βββββββββββββββββββββββ*
*& text
*&βββββββββββββββββββββββ*
*& β> p1 text
*& <β p2 text
*&βββββββββββββββββββββββ*
FORM fetch_data .
SELECT FROM zsan_dt_flight FIELDS * WHERE countryfr = @P_cnfrom AND countryto = @p_cnto
into table _flight_det .
perform build_layout.
ENDFORM.
*&βββββββββββββββββββββββ*
*& Form build_layout
*&βββββββββββββββββββββββ*
*& text
*&βββββββββββββββββββββββ*
*& β> p1 text
*& <β p2 text
*&βββββββββββββββββββββββ*
FORM build_layout .
ls_layo = VALUE #( edit = βXβ col_opt = βXβ ) .
ENDFORM. Create the container object using cl_gui_custom_container.Create the OALV grid inside the custom container *βββββββββββββββββββββββ-*
***INCLUDE ZSAN_RP_OALV_CRUD_STATUS_01O01.
*βββββββββββββββββββββββ-*
*&βββββββββββββββββββββββ*
*& Module STATUS_0100 OUTPUT
*&βββββββββββββββββββββββ*
*&
*&βββββββββββββββββββββββ*
MODULE status_0100 OUTPUT.
* SET PF-STATUS βxxxxxxxxβ.
* SET TITLEBAR βxxxβ.
if lo_cust is not BOUND .
CREATE OBJECT lo_cust
EXPORTING
* parent = β Parent container
container_name = βCONTAINERβ
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
endif .
if lo_cust is BOUND .
create OBJECT lo_grid
EXPORTING
i_parent = lo_cust β Parent Container .
endif .
CALL FUNCTION βLVC_FIELDCATALOG_MERGEβ
EXPORTING
* I_BUFFER_ACTIVE =
I_STRUCTURE_NAME = βZSAN_DT_FLIGHTβ
* I_CLIENT_NEVER_DISPLAY = βXβ
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
.
set HANDLER handle_event=>handle_toolbar for lo_grid .
set HANDLER handle_event=>handle_user for lo_grid .
lo_grid->set_table_for_first_display(
EXPORTING
i_structure_name = βZSAN_DT_FLIGHTβ β Internal Output Table Structure Name
is_layout = ls_layo β Layout
CHANGING
it_outtab = gt_flight_det β Output Table
it_fieldcatalog = lt_fieldcat β Field Catalog
EXCEPTIONS
invalid_parameter_combination = 1 β Wrong Parameter
program_error = 2 β Program Errors
too_many_lines = 3 β Too many Rows in Ready for Input Grid
others = 4
).
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDMODULE. In the Process after input write the logic to enable the pfstatus back cancel and leave button. *βββββββββββββββββββββββ-*
***INCLUDE ZSAN_RP_OALV_CRUD_USER_COMMI01.
*βββββββββββββββββββββββ-*
*&βββββββββββββββββββββββ*
*& Module USER_COMMAND_0100 INPUT
*&βββββββββββββββββββββββ*
* text
*βββββββββββββββββββββββ-*
MODULE user_command_0100 INPUT.
case SY-UCOMM .
WHEN βBACKβ OR βCANCELβ OR βEXITβ .
LEAVE to SCREEN 0 .
ENDCASE .
ENDMODULE. Add the toolbar events of cl_gui_alv_grid to create the button like create And delete. CLASS handle_event DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive.
CLASS-METHODS:handle_user FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS. We can make use of standard table type ttb_button to add, modify, control toolbar buttons in alv.FUNCTION ICON QUICKINFO BUTN_TYPE DISABLED TEXT CHECKED are the fields available in the predefined stucture STB_BUTTON. CLASS handle_event IMPLEMENTATION .
METHOD handle_toolbar.
e_object->mt_toolbar = VALUE #( base e_object->mt_toolbar (
function = βSAVEβ
icon = ICON_SYSTEM_SAVE
butn_type = 0
text = βSAVEβ
quickinfo = βSAVEβ) (
function = βDELETEβ
icon = ICON_DELETE
quickinfo = βdelete recordβ
butn_type = 0
text = βDELETEβ
) ) .
ENDMETHOD.
METHOD handle_user.
CASE e_ucomm.
WHEN βSAVEβ.
PERFORM update_data_base.
when βDELETEβ .
PERFORM DELETE_DATA_BASE .
ENDCASE.
ENDMETHOD.
ENDCLASS . Define and handle events for the crud operation. *&βββββββββββββββββββββββ*
*& Form update_data_base
*&βββββββββββββββββββββββ*
*& text
*&βββββββββββββββββββββββ*
*& β> p1 text
*& <β p2 text
*&βββββββββββββββββββββββ*
FORM update_data_base .
lo_grid->check_changed_data(
IMPORTING
e_valid = check β Entries are Consistent
* CHANGING
* c_refresh = βXβ β Character Field of Length 1
).
IF gt_flight_det NE gt_flight_old .
LOOP AT gt_flight_det ASSIGNING FIELD-SYMBOL(<fs_flight_det>) .
MOVE-CORRESPONDING <fs_flight_det> TO <fs_flight_new> .
APPEND INITIAL LINE TO <ft_flight_new> ASSIGNING <fs_flight_new> .
ENDLOOP .
CALL FUNCTION βZUPD_FM_FLIGHT_UPDATEβ IN UPDATE TASK
EXPORTING
gt_upd_flight_det = <ft_flight_new> .
COMMIT WORK . βUpdate function moduleβ
IF sy-subrc = 0 .
MESSAGE s055(zcl_san_msg) . βData Updated Successfullyβ
ELSE .
MESSAGE e080(zcl_san_msg) DISPLAY LIKE βIβ .
βData is Not updated to the databaseβ
ENDIF .
ENDIF .
ENDFORM. DATA: lt_selected_rows TYPE lvc_t_row,
lv_index TYPE lvc_index,
ls_row TYPE lvc_s_row.
CALL METHOD lo_grid->get_selected_rows
IMPORTING
et_index_rows = lt_selected_rows β Indexes of Selected
Rows
* et_row_no = β Numeric IDs of Selected Rows
.
IF lt_selected_rows IS INITIAL.
MESSAGE βNo rows selected for deletionβ TYPE βIβ.
RETURN.
ENDIF.
LOOP AT lt_selected_rows INTO ls_row.
lv_index = ls_row-index.
β Get the corresponding row from the internal table
READ TABLE gt_flight_det INTO gs_flight_det INDEX lv_index.
IF sy-subrc = 0.
β Delete the record from the database
DELETE FROM zsan_dt_flight WHERE airline_code
= gs_flight_det-airline_code AND
flight_conn_no = gs_flight_det-flight_conn_no
AND airport_to = gs_flight_det-airport_to .
IF sy-subrc = 0.
β If deletion was successful, remove from the internal table
DELETE gt_flight_det INDEX lv_index.
ELSE.
MESSAGE e081(zcl_san_msg) .
ENDIF.
ENDIF.
ENDLOOP.
CALL METHOD lo_grid->refresh_table_display.
MESSAGE s060(zcl_san_msg) .
ENDFORM. Execute the report progaram.Read To retreive the flight details from the selected country. Update Admin can update the flight details based on the requirements.Here the data is updated successfully. Insert Admin can insert the new flights to the existing database table. Click on append row. Click on save click on save One entry is created in the database table. also multiple entries can be created by clicking on append row multiple times.Delete Select the flights (entry) and click on the delete button. the selected flight entry is deleted from the database table. Read More Application Development Blog Posts articles
#SAP