Skip to Content
0
Former Member
Dec 09, 2013 at 12:03 PM

ALV-Editing and saving the edited values in Database (OOPS)

1901 Views

Hi ABAPers,

I'm creating a report where i can edit & save the edited values in database using oops.

But the problem is, I would like to display the edited values the next time I click on execute. (i.e would like to display the data from ztable).

I'm a fresher, so i refered the following link -- ALV-Editing and saving the edited values in Database(OOPS) - Code Gallery - SCN Wiki

Please refer my code and let me know where i need to make changes.

Thanks in advance.

*&---------------------------------------------------------------------*

*& Report ZSID_ALV_OOPS

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT ZSID_ALV_OOPS.

TABLES: mara, ZSID_MARA.

* Data declarations

DATA : itab TYPE STANDARD TABLE OF ZSID_MARA,"Output table

i_fieldcat TYPE STANDARD TABLE OF lvc_s_fcat,"Field catalog

i_selected_rows TYPE lvc_t_row,"Selected Rows

w_selected_rows TYPE lvc_S_row,

i_modified TYPE STANDARD TABLE OF ZSID_MARA,"For getting modified rows

w_modified TYPE ZSID_MARA,

wa TYPE ZSID_MARA,

w_variant TYPE disvariant,

o_docking TYPE REF TO cl_gui_docking_container,"Docking Container

o_grid TYPE REF TO cl_gui_alv_grid."Grid

FIELD-SYMBOLS : <fs_fieldcat> TYPE lvc_s_fcat.

SELECT-OPTIONS s_matnr FOR mara-matnr.

SELECT MATNR

PSTAT

MTART

BRGEW

FROM MARA INTO CORRESPONDING FIELDS OF TABLE itab WHERE matnr IN s_matnr.

CALL SCREEN 0001.

*&---------------------------------------------------------------------*

*& Module STATUS_0001 OUTPUT

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

module STATUS_0001 output.

IF o_docking IS INITIAL.

SET PF-STATUS 'ZSID_STATUS'. "GUI Status

SET TITLEBAR 'ZSID_TITLE'. "Title

* Creating Docking Container and grid

PERFORM create_object.

* Filling the fieldcatalog table

PERFORM create_fieldcat.

* Modifying the fieldcatalog table

PERFORM modify_fieldcat.

* Registering edit

PERFORM register_edit.

* Displaying the output

PERFORM display_output.

ENDIF.

endmodule. " STATUS_0001 OUTPUT

*&---------------------------------------------------------------------*

*& Module USER_COMMAND_0001 INPUT

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

module USER_COMMAND_0001 input.

DATA lv_ucomm TYPE sy-ucomm.

lv_ucomm = sy-ucomm.

CASE lv_ucomm.

WHEN 'CANCEL' OR 'EXIT'.

PERFORM free_objects.

LEAVE PROGRAM.

WHEN 'BACK'.

PERFORM free_objects.

SET SCREEN '0'.

LEAVE SCREEN.

WHEN 'SAVE'.

PERFORM save_database.

CALL METHOD o_grid->refresh_table_display.

ENDCASE.

endmodule. " USER_COMMAND_0001 INPUT

*&amp;---------------------------------------------------------------------*

*&amp; Form create_object

*&amp;---------------------------------------------------------------------*

* Creating Docking Container and grid

*----------------------------------------------------------------------*

FORM create_object .

* Creating Docking Container

CREATE OBJECT o_docking

EXPORTING ratio = '95'.

IF sy-subrc EQ 0.

* Creating Grid

CREATE OBJECT o_grid

EXPORTING i_parent = o_docking.

ENDIF.

ENDFORM. " create_object

*&---------------------------------------------------------------------*

*& Form create_fieldcat

*&---------------------------------------------------------------------*

* Filling the fieldcatalog table

*----------------------------------------------------------------------*

FORM create_fieldcat .

* Filling the fieldcatalog table

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING i_structure_name = 'ZSID_MARA'

CHANGING ct_fieldcat = i_fieldcat

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

ENDFORM. " create_fieldcat

*&amp;---------------------------------------------------------------------*

*&amp; Form modify_fieldcat

*&amp;---------------------------------------------------------------------*

* Making the column as ediable

*----------------------------------------------------------------------*

FORM modify_fieldcat .

LOOP AT i_fieldcat ASSIGNING <fs_fieldcat>.

CASE <fs_fieldcat>-fieldname.

WHEN 'BRGEW'.

* Making a column as Editable

<fs_fieldcat>-edit = 'X'.

ENDCASE.

ENDLOOP.

ENDFORM. " modify_fieldcat

*&---------------------------------------------------------------------*

*& Form register_edit

*&---------------------------------------------------------------------*

* Registering Edit

*----------------------------------------------------------------------*

FORM register_edit .

CALL METHOD o_grid->register_edit_event

EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_modified.

ENDFORM. " register_edit

*&amp;---------------------------------------------------------------------*

*&amp; Form display_output

*&amp;---------------------------------------------------------------------*

* Displaying the output

*----------------------------------------------------------------------*

FORM display_output .

w_variant-report = sy-repid.

* Displaying the output

CALL METHOD o_grid->set_table_for_first_display

EXPORTING

is_variant = w_variant

i_save = 'A'

CHANGING

it_outtab = itab

it_fieldcatalog = i_fieldcat

EXCEPTIONS

invalid_parameter_combination = 1

program_error = 2

too_many_lines = 3

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.

ENDFORM. " display_output

*&---------------------------------------------------------------------*

*& Form free_objects

*&---------------------------------------------------------------------*

* Free Objects

*----------------------------------------------------------------------*

FORM free_objects .

CALL METHOD o_grid->free

EXCEPTIONS

cntl_error = 1

cntl_system_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

CALL METHOD o_docking->free

EXCEPTIONS

cntl_error = 1

cntl_system_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDFORM. " free_objects

*&amp;---------------------------------------------------------------------*

*&amp; Form save_database

*&amp;---------------------------------------------------------------------*

* Save in database

*----------------------------------------------------------------------*

FORM save_database .

* Getting the selected rows index

CALL METHOD o_grid->get_selected_rows

IMPORTING et_index_rows = i_selected_rows.

"Through the index capturing the values of selected rows

LOOP AT i_selected_rows INTO w_selected_rows.

READ TABLE itab INTO WA INDEX w_selected_rows-index.

IF sy-subrc EQ 0.

MOVE-CORRESPONDING wa TO w_modified.

APPEND w_modified TO i_modified.

ENDIF.

ENDLOOP.

MODIFY ZSID_MARA FROM TABLE i_modified.

MESSAGE 'VALUES SAVED' type 'I'.

ENDFORM.