Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

OO ABAP 'SAVE' functionality

Former Member
0 Likes
734

Hi Experts,

I have a ALV Grid screen program using OO concepts. Requirement is that the final ALV table has 10 fields, all are editable fields and will be blank for the first time. So, I am doing a DO.....ENDO for the first time so that the final ALV table is displayed with 10 fields and 10 records. I need to key in some information in these fields and try to insert this information in custom database table. My question here is I am able to enter the values in the ALV report. But, when I am trying to SAVE the entries entered into the ALV grid, my internal table is not updated with the entries entered on the ALV report. Appreciate if anybody could guide me in correct direction.

Below is the piece of my code.

   IF IT_FINAL[] IS INITIAL.

    DO 10 TIMES.
      WA_FINAL-SEPARATOR = '   '.
      APPEND WA_FINAL TO IT_FINAL.
      CLEAR: WA_FINAL.
    ENDDO.

*Create a custom container control for the ALV Control
    CREATE OBJECT CUSTOM_CONTAINER
      EXPORTING
        CONTAINER_NAME              = C1
      EXCEPTIONS
        CNTL_ERROR                  = 1
        CNTL_SYSTEM_ERROR           = 2
        CREATE_ERROR                = 3
        LIFETIME_ERROR              = 4
        LIFETIME_DYNPRO_DYNPRO_LINK = 5.

*Create an instance of ALV control
    CREATE OBJECT GRID1
      EXPORTING
        I_PARENT = CUSTOM_CONTAINER.

    CALL METHOD GRID1->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
        IS_LAYOUT            = IS_LAYOUT
        IS_VARIANT           = IS_VARIANT
        I_SAVE               = 'A'
        I_BYPASSING_BUFFER   = 'X'

      CHANGING
        IT_FIELDCATALOG      = IT_FIELDCAT
        IT_OUTTAB            = IT_FINAL.

 
  ENDIF.

   MODULE USER_COMMAND_0200 INPUT.

   CASE OKCODE.
    WHEN GC_SAVE.
      DATA : IS_VALID.

************TRIED THE BELOW COMMENTED CODE. BUT, NOT WORKING******************
*      IF GRID1 IS INITIAL.
*       CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
*         IMPORTING
*           E_GRID = GRID1.
*      ENDIF.
*      IF NOT GRID1 IS INITIAL.
*        CALL METHOD GRID1->CHECK_CHANGED_DATA .
*      ENDIF.

      LOOP AT IT_FINAL INTO WA_FINAL.        "Even though I have entries this table is blank


      ENDLOOP.

   ENDMODULE.

Thanks,

Nani

5 REPLIES 5
Read only

Former Member
0 Likes
673

Hi Nani,

You have to create a local class in your report to handle event when data in the grid is changed.

Before you create your ALV object, you will need to do some preparation like:

*---Set Event Handler for ALV Grid Instance

CREATE OBJECT gr_event_handler.

SET HANDLER gr_event_handler->handle_data_changed FOR gr_alvgrid.

*----Register Edit Event for Enter pressed on keyboard

gr_alvgrid->register_edit_event( exporting i_event_id = cl_gui_alv_grid=>mc_evt_enter ).

Please take a look at the source code of report BCALV_EDIT_04 to find out more information.

Hope it helps! Cheers!

Read only

Abhijit74
Active Contributor
0 Likes
673

Hello,

Apart from your code you need to put below code in your program.

 

DATA: go_obj   TYPE REF TO gcl_event_receiver.

   IF NOT go_obj IS BOUND.

        CREATE OBJECT go_obj.

        SET HANDLER go_obj->handle_data_changed FOR GRID1.

 

        CALL METHOD go_grid->register_edit_event

          EXPORTING

            i_event_id = cl_gui_alv_grid=>mc_evt_enter.

ENDIF.

And also before saving you can check whether any data has been changed or not by writing below code.

Data: gv_valid type c.

CLEAR gv_valid.

   CALL METHOD Grid1->check_changed_data

     IMPORTING

       e_valid = gv_valid.

If gv_valid is not initial.

** save data*...

endif.

Thanks,

Abhijit

Read only

RaymondGiuseppi
Active Contributor
0 Likes
673

A grid->check_changed_data which don't raise an error should have updated your internal table, check that you don't coded an exit function-code for SAVE. A cl_gui_cfw=>flush should not be required.

Regards,

Raymond

Read only

0 Likes
673

Hi All,

I have created a local class in my report to handle event when the data is changed as said by Nguyen and Abhijit. When I did a syntax check, program gave me an error to create class definition and implementation. I have declared it as shown below.

*---------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER DEFINITION.

  PUBLIC SECTION.
    METHODS: HANDLE_DATA_CHANGED FOR EVENT DATA_CHANGED
                                        OF CL_GUI_ALV_GRID
                                 IMPORTING ER_DATA_CHANGED.

  PRIVATE SECTION.

ENDCLASS.                    "lcl_event_receiver DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.

  METHOD HANDLE_DATA_CHANGED.

   ENDMETHOD.                    "handle_data_changed

ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION

I do not know what code to put in method HANDLE_DATA_CHANGED under implementation. I need some help here. Before displaying my ALV object, below is the code I inserted.

IF IT_FINAL[] IS INITIAL.

    DO 10 TIMES.
      WA_FINAL-SEPARATOR = '   '.
      APPEND WA_FINAL TO IT_FINAL.
      CLEAR: WA_FINAL.
    ENDDO.

*Create a custom container control for the ALV Control
    CREATE OBJECT CUSTOM_CONTAINER
      EXPORTING
        CONTAINER_NAME              = C1
      EXCEPTIONS
        CNTL_ERROR                  = 1
        CNTL_SYSTEM_ERROR           = 2
        CREATE_ERROR                = 3
        LIFETIME_ERROR              = 4
        LIFETIME_DYNPRO_DYNPRO_LINK = 5.

*Create an instance of ALV control
    CREATE OBJECT GRID1
      EXPORTING
        I_PARENT = CUSTOM_CONTAINER.

***********************Inserted this code************************

      IF NOT GV_VERIFIER IS BOUND.
* Create Object to verify input values
      CREATE OBJECT GV_VERIFIER.
      SET HANDLER GV_VERIFIER->HANDLE_DATA_CHANGED FOR GRID1.

      CALL METHOD GRID1->REGISTER_EDIT_EVENT
        EXPORTING
          I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER.
    ENDIF.

*******************************************************************
    CALL METHOD GRID1->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
        IS_LAYOUT            = IS_LAYOUT
        IS_VARIANT           = IS_VARIANT
        I_SAVE               = 'A'
        I_BYPASSING_BUFFER   = 'X'
 

      CHANGING
        IT_FIELDCATALOG      = IT_FIELDCAT
        IT_OUTTAB            = IT_FINAL.

 
  ENDIF.

But, still when I do my SAVE functionality. Even though my editable ALV grid had some values, my internal table IT_FINAL is blank.  Please help me to figure out this solution.

   *&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0500  INPUT
*&---------------------------------------------------------------------*
MODULE USER_COMMAND_0500 INPUT.

  CASE OKCODE.
    WHEN GC_SAVE.
      DATA: GV_VALID TYPE C.
      CLEAR GV_VALID.

      CALL METHOD GRID1->CHECK_CHANGED_DATA
        IMPORTING
          E_VALID = GV_VALID.

      IF GV_VALID IS NOT INITIAL.
           LOOP AT IT_FINAL INTO WA_FINAL.  "INTERNAL TABLE IS BLANK HERE EVEN        

                                                                    "THOUGH i HAVE ENTERED SOME VALUES IN 

                                                                    "THE EDITABLE FIELDS

           ENDLOOP.  

      ENDIF.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_0100  INPUT

APPRECIATE IF ANYBODY COULD HELP ME SOLVE THIS ISSUE.

Thanks,

Nani

Read only

0 Likes
673

Hello,

CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.

  METHOD HANDLE_DATA_CHANGED.

* Call a subroutine for Modifying the alv data

          PERFORM f_check_modify_data USING    er_data_changed

                                CHANGING IT_FINAL..

   ENDMETHOD.                    "handle_data_ch



ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION

Suppose you want to modify the cell in your alv output and you have a field 'MATNR' now you want to display the material description. So, in a alv grid if you change the MATNR then it should change the description too and that should reflect in your final table too.

This is a dummy code. You can customize your own.

FORM f_check_modify_data  USING    p_er_data_changed TYPE REF TO                                                                                 cl_alv_changed_data_protocol

                                         CHANGING p_it_final TYPE ty_t_final.

  DATA: lt_mod_cells  TYPE lvc_t_modi,

        ls_row_id     TYPE lvc_s_row.

  DATA: lv_value     TYPE lvc_value,

             lv_fname     TYPE lvc_fname,

             lv_matnrval  TYPE matnr,

             lv_matnr     TYPE matnr,

             lv_maktx     TYPE maktx.

  FIELD-SYMBOLS: <lfs_mod_cells>  TYPE lvc_s_modi,

                                <lfs_item>       TYPE ty_item.

  lt_mod_cells[] = p_er_data_changed->mt_mod_cells[].

  LOOP AT lt_mod_cells ASSIGNING <lfs_mod_cells>.

    CLEAR: lv_value, lv_matnrval, lv_matnr.

    IF <lfs_mod_cells>-fieldname = gc_matnr

    AND NOT <lfs_mod_cells>-value IS INITIAL.

       PERFORM f_get_row_id CHANGING ls_row_id.

       MOVE <lfs_mod_cells>-value TO lv_matnrval.

       CLEAR lv_matnr.

      SELECT SINGLE matnr

             FROM mara

             INTO lv_matnr

             WHERE matnr = lv_matnrval.

      IF sy-subrc = 0.

        CLEAR lv_maktx.

        SELECT SINGLE maktx

               FROM makt

               INTO lv_maktx

               WHERE matnr = lv_matnr

               AND spras = sy-langu.

        IF sy-subrc = 0.

          READ TABLE p_gt_item ASSIGNING <lfs_item> INDEX <lfs_mod_cells>-tabix.

          IF sy-subrc = 0.

            MOVE: lv_maktx TO <lfs_item>-maktx,

                  lv_maktx TO lv_value,

                  gc_maktx TO lv_fname.

            CALL METHOD p_er_data_changed->modify_cell

              EXPORTING

                i_row_id    = <lfs_mod_cells>-row_id

                i_tabix     = <lfs_mod_cells>-tabix

                i_fieldname = lv_fname

                i_value     = lv_value.

          ENDIF.

        ENDIF.

      ENDIF.

 

    ENDIF.

  ENDLOOP.

ENDFORM.                    " F_CHECK_MODIFY_DATA


FORM f_get_row_id  CHANGING p_ls_row_id TYPE lvc_s_row.

 

  CLEAR: p_ls_row_id.

  CALL METHOD go_grid->get_current_cell

    IMPORTING

      es_row_id = p_ls_row_id.

 

ENDFORM.                    " F_GET_ROW_ID

Thanks,

Abhijit