Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
SanatanaBehera
Explorer
2,638

 

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

  1. Fetch the record and store in the internal table.
  2. Call the specified Screen
  3. Create the container object using cl_gui_custom_container
  4. Create the OALV grid inside the custom container
  5. Enable the editable mode for CRUD operation
  6. Populate the field catalog based on the structure of the internal table Used in alv grid.
  7.  Display the initial data using the method set_table_for_first_display.
  8.  Add the toolbar events of cl_gui_alv_grid to create the button like create And delete.
  9.  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.

SanatanaBehera_1-1739617998803.png

Read 

To retreive the flight details from the selected country. 

SanatanaBehera_2-1739618060676.png

SanatanaBehera_3-1739618098130.png

Update 

Admin can update the flight details based on the requirements.

SanatanaBehera_4-1739618190388.png

SanatanaBehera_5-1739618241134.png

SanatanaBehera_6-1739618617206.png

SanatanaBehera_7-1739618642759.png

Here the data is updated successfully. 

SanatanaBehera_8-1739618686906.png

Insert 

Admin can insert the new flights to the existing database table. Click on append row.  

SanatanaBehera_9-1739618773501.png

SanatanaBehera_10-1739618803227.png

Click on save  

SanatanaBehera_11-1739618872913.png

click on save 

SanatanaBehera_12-1739618927806.png

One entry is  created in the database table.  also multiple entries  can be created by clicking on append row multiple times.

SanatanaBehera_13-1739618996594.png

Delete 

Select the flights (entry)  and click on the delete button.  

SanatanaBehera_14-1739619068234.png

SanatanaBehera_15-1739619098218.png

the selected flight entry is deleted from the database table.

SanatanaBehera_16-1739619174532.png