Tutorial Name

ALV Edit and Save Functionality in ABAP

Tutorial Description

Making an ALV editable in SAP ABAP and save data after editing, editable ALV using normal ABAP function modules

Tutorial Area Core ABAP
Prerequisites ABAP
Learning Level Intermediate
Estimated Time to learn 60
Save to Learning List Save to your Learning List
+ -
Requirement: Develop a ALV report to display list of materials for a range of materials and provide option to edit material type and material group.

Analysis: For this requirement, we need to add select-options for material no, we need to get materials from MARA, we need to make the cells editable.

Function Modules Used:

  • REUSE_ALV_GRID_DISPLAY
  • REUSE_ALV_COMMENTARY_WRITE

REUSE_ALV_GRID_DISPLAY : is used to display ALV Grid.

REUSE_ALV_COMMENTARY_WRITE : Is used to display Top Of Page .

Follow the below steps to Create ALV with edit and Save functionality

Create a program in SE38, add data declarations

Create a report program in SE38 and add data declarations for MARA table, MAKT table.Add select-options for material no input.

Editable ALV ABAP

TYPES: BEGIN OF TY_MARA,
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
        LABOR TYPE MARA-LABOR,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
       WA_MARA TYPE TY_MARA.
SELECT-OPTIONS: S_MATNR FOR WA_MARA-MATNR. "material no input

Build Field catalog for MARA

Build field catalog for MARA table and specify EDIT = 'X' for field catalog fields to make them editable.
DATA :  WA_FIELDCAT   TYPE  SLIS_FIELDCAT_ALV, "Field catalog work area
        IT_FIELDCAT   TYPE  SLIS_T_FIELDCAT_ALV. "field catalog internal table
  WA_FIELDCAT-FIELDNAME   = 'MATNR'. "field name
  WA_FIELDCAT-SELTEXT_M   = 'Material Value'. "field text
  WA_FIELDCAT-COL_POS     =  1. "position
  WA_FIELDCAT-KEY         = 'X'. "is key field
  WA_FIELDCAT-HOTSPOT         = 'X'. "hotspot(hperlink)
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'MTART'.
  WA_FIELDCAT-SELTEXT_M   = 'Material Type'.
  WA_FIELDCAT-EDIT        = 'X'.            "make editable
  WA_FIELDCAT-COL_POS     =  2.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'MBRSH'.
  WA_FIELDCAT-SELTEXT_M   = 'Industry Sector'.
  WA_FIELDCAT-EDIT        = 'X'.              "make editable
  WA_FIELDCAT-COL_POS     =  3.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'MATKL'.
  WA_FIELDCAT-SELTEXT_M   = 'Material Group'.
  WA_FIELDCAT-EDIT        = 'X'.             "make editable
  WA_FIELDCAT-COL_POS     =  4.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'MEINS'.
  WA_FIELDCAT-SELTEXT_M   = 'Base UOM'.
  WA_FIELDCAT-COL_POS     =  5.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'LABOR'.
  WA_FIELDCAT-SELTEXT_M   = 'Lab Office'.
  WA_FIELDCAT-COL_POS     =  6.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

Call Function Module to display ALV

Call ALV function module, provide subroutine names for top of page and user command.
DATA :  IT_LAYOUT TYPE SLIS_LAYOUT_ALV, "ALV layout settings
        FORM_TOP_OF_PAGE  TYPE SLIS_FORMNAME VALUE 'FORM_TOP_OF_PAGE', "for avl header.
        FORM_CALLBACK TYPE SLIS_FORMNAME VALUE 'USER_COMMAND'. "For user command
  IT_MARACP[] = IT_MARA[]. "store original data before displaying ALV
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
*     I_INTERFACE_CHECK       = ' '
*     I_BYPASSING_BUFFER      = ' '
*     I_BUFFER_ACTIVE         = ' '
      I_CALLBACK_PROGRAM      = SY-REPID "program name
*     I_CALLBACK_PF_STATUS_SET          = ' '
      I_CALLBACK_USER_COMMAND = FORM_CALLBACK "user command subroutine name
      I_CALLBACK_TOP_OF_PAGE  = FORM_TOP_OF_PAGE "sub routine for top of page
*     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*     I_CALLBACK_HTML_END_OF_LIST       = ' '
*     I_STRUCTURE_NAME        =
*     I_BACKGROUND_ID         = ' '
*     I_GRID_TITLE            =
*     I_GRID_SETTINGS         =
      IS_LAYOUT               = IT_LAYOUT
      IT_FIELDCAT             = IT_FIELDCAT
*     IT_EXCLUDING            =
*     IT_SPECIAL_GROUPS       =
*     IT_SORT                 =
*     IT_FILTER               =
*     IS_SEL_HIDE             =
*     I_DEFAULT               = 'X'
*     I_SAVE                  = ' '
*     IS_VARIANT              =
*     IT_EVENTS               =
*     IT_EVENT_EXIT           =
*     IS_PRINT                =
*     IS_REPREP_ID            =
*     I_SCREEN_START_COLUMN   = 0
*     I_SCREEN_STARlv_line    = 0
*     I_SCREEN_END_COLUMN     = 0
*     I_SCREEN_END_LINE       = 0
*     I_HTML_HEIGHT_TOP       = 0
*     I_HTML_HEIGHT_END       = 0
*     IT_ALV_GRAPHICS         =
*     IT_HYPERLINK            =
*     IT_ADD_FIELDCAT         =
*     IT_EXCEPT_QINFO         =
*     IR_SALV_FULLSCREEN_ADAPTER        =
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER =
*     ES_EXIT_CAUSED_BY_USER  =
    TABLES
      T_OUTTAB                = IT_MARA
    EXCEPTIONS
      PROGRAM_ERROR           = 1
      OTHERS                  = 2.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

Add Code to display top of page

Implement subroutine for top of page.
FORM FORM_TOP_OF_PAGE.

  DATA: IT_HEADER TYPE SLIS_T_LISTHEADER,
        WA_HEADER TYPE SLIS_LISTHEADER,
        LV_LINE LIKE WA_HEADER-INFO,
        LD_LINES TYPE I,
        LD_LINESC(10) TYPE C.

* Title
  WA_HEADER-TYP  = 'H'.
  WA_HEADER-INFO = 'MARA Master Report'.
  APPEND WA_HEADER TO IT_HEADER.
  CLEAR WA_HEADER.

* Date
  WA_HEADER-TYP  = 'S'.
  WA_HEADER-KEY = 'Date: '.
  CONCATENATE  SY-DATUM+6(2) '.'
               SY-DATUM+4(2) '.'
               SY-DATUM(4) INTO WA_HEADER-INFO.   "todays date
  APPEND WA_HEADER TO IT_HEADER.
  CLEAR: WA_HEADER.
* Date
  WA_HEADER-TYP  = 'S'.
  WA_HEADER-KEY = 'User: '.
  CONCATENATE  SY-UNAME ' ' INTO WA_HEADER-INFO.   "Logged in user
  APPEND WA_HEADER TO IT_HEADER.
  CLEAR: WA_HEADER.
* Total No. of Records Selected
  DESCRIBE TABLE  IT_MARA LINES LD_LINES.
  LD_LINESC = LD_LINES.
  CONCATENATE 'Total No. of Records Selected: ' LD_LINESC
                    INTO LV_LINE SEPARATED BY SPACE.
  WA_HEADER-TYP  = 'A'.
  WA_HEADER-INFO = LV_LINE.
  APPEND WA_HEADER TO IT_HEADER.
  CLEAR: WA_HEADER, LV_LINE.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      IT_LIST_COMMENTARY = IT_HEADER.
  .
  CLEAR IT_HEADER.
ENDFORM.                    "TOP-OF-PAGE

Implement user command subroutine

Implement subroutine for user command to save data after edit.
FORM USER_COMMAND  USING P_UCOMM    LIKE SY-UCOMM
                         P_SELFIELD TYPE SLIS_SELFIELD.
  "p_ucomm will hold user action like double click, clicking a button ,etc
  CASE P_UCOMM.
    WHEN '&IC1'. " Function code for double click 
      READ TABLE IT_MARA INTO WA_MARA INDEX P_SELFIELD-TABINDEX. " Getting Row data
      CASE P_SELFIELD-FIELDNAME.
        WHEN 'MATNR'. " Column data
          SET PARAMETER ID: 'MAT' FIELD P_SELFIELD-VALUE.

          CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
        WHEN 'MTART'.
          MESSAGE P_SELFIELD-VALUE TYPE 'S'.
      ENDCASE.
    WHEN '&DATA_SAVE'. "Save Function
            MESSAGE 'Saving data' TYPE 'S'.
        DATA : WA_MARACP     TYPE TY_MARA.
  DATA : WA_MARA_TMP TYPE MARA.
  CLEAR IT_CHANGES[].
**loop thorugh it_mara and dind changed records
  LOOP AT IT_MARA INTO WA_MARA.
    READ TABLE IT_MARACP INTO WA_MARACP INDEX SY-TABIX.
    IF WA_MARACP NE WA_MARA.
      MOVE-CORRESPONDING WA_MARA TO WA_MARA_TMP.
      MODIFY MARA FROM WA_MARA_TMP . "modify standard MARA table
      IF SY-SUBRC EQ 0.
        MESSAGE 'Data Updated' TYPE 'I'.
      ENDIF.
    ENDIF.
    CLEAR WA_MARACP.
  ENDLOOP.

   ENDCASE.
ENDFORM.                    "user_command

Full and Final Code to display blocked list ALV

REPORT ZSAN_EDITABLE_MARA.
TYPES: BEGIN OF TY_MARA,
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
        LABOR TYPE MARA-LABOR,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
       WA_MARA TYPE TY_MARA.
DATA :  IT_MARACP     TYPE  STANDARD TABLE OF TY_MARA, "temparary table for MARA
        IT_CHANGES    TYPE  STANDARD TABLE OF TY_MARA. "changes on grid

DATA :  WA_FIELDCAT   TYPE  SLIS_FIELDCAT_ALV, "Field catalog work area
        IT_FIELDCAT   TYPE  SLIS_T_FIELDCAT_ALV, "field catalog internal table
        IT_LAYOUT TYPE SLIS_LAYOUT_ALV, "ALV layout settings
        FORM_TOP_OF_PAGE  TYPE SLIS_FORMNAME VALUE 'FORM_TOP_OF_PAGE', "for avl header.
        FORM_CALLBACK TYPE SLIS_FORMNAME VALUE 'USER_COMMAND'. "For user command
SELECT-OPTIONS: S_MATNR FOR WA_MARA-MATNR. "material no input

START-OF-SELECTION.
  SELECT MATNR
         MTART
         MBRSH
         MATKL
         MEINS
         LABOR FROM MARA INTO TABLE IT_MARA WHERE MATNR IN S_MATNR.
  PERFORM BUILD_FCAT.
  PERFORM DISPLAY_ALV.
FORM BUILD_FCAT .
  WA_FIELDCAT-FIELDNAME   = 'MATNR'.
  WA_FIELDCAT-SELTEXT_M   = 'Material Value'.
  WA_FIELDCAT-COL_POS     =  1.
  WA_FIELDCAT-KEY         = 'X'.
  WA_FIELDCAT-HOTSPOT         = 'X'.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'MTART'.
  WA_FIELDCAT-SELTEXT_M   = 'Material Type'.
  WA_FIELDCAT-EDIT        = 'X'.
  WA_FIELDCAT-COL_POS     =  2.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'MBRSH'.
  WA_FIELDCAT-SELTEXT_M   = 'Industry Sector'.
  WA_FIELDCAT-EDIT        = 'X'.
  WA_FIELDCAT-COL_POS     =  3.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'MATKL'.
  WA_FIELDCAT-SELTEXT_M   = 'Material Group'.
  WA_FIELDCAT-EDIT        = 'X'.
  WA_FIELDCAT-COL_POS     =  4.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'MEINS'.
  WA_FIELDCAT-SELTEXT_M   = 'Base UOM'.
  WA_FIELDCAT-ICON         = 'X'.  " Display the field as ICON
  WA_FIELDCAT-COL_POS     =  5.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.

  WA_FIELDCAT-FIELDNAME   = 'LABOR'.
  WA_FIELDCAT-SELTEXT_M   = 'Lab Office'.
  WA_FIELDCAT-ICON         = 'X'.  " Display the field as ICON
  WA_FIELDCAT-COL_POS     =  6.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
  CLEAR WA_FIELDCAT.
ENDFORM.                    " BUILD_FCAT

FORM DISPLAY_ALV .
  IT_MARACP[] = IT_MARA[].
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM      = SY-REPID "program name
      I_CALLBACK_USER_COMMAND = FORM_CALLBACK "user command subroutine name
      I_CALLBACK_TOP_OF_PAGE  = FORM_TOP_OF_PAGE "sub routine for top of page
      IS_LAYOUT               = IT_LAYOUT
      IT_FIELDCAT             = IT_FIELDCAT
    TABLES
      T_OUTTAB                = IT_MARA
    EXCEPTIONS
      PROGRAM_ERROR           = 1
      OTHERS                  = 2.
  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_ALV

FORM FORM_TOP_OF_PAGE.

  DATA: IT_HEADER TYPE SLIS_T_LISTHEADER,
        WA_HEADER TYPE SLIS_LISTHEADER,
        LV_LINE LIKE WA_HEADER-INFO,
        LD_LINES TYPE I,
        LD_LINESC(10) TYPE C.

* Title
  WA_HEADER-TYP  = 'H'.
  WA_HEADER-INFO = 'MARA Master Report'.
  APPEND WA_HEADER TO IT_HEADER.
  CLEAR WA_HEADER.

* Date
  WA_HEADER-TYP  = 'S'.
  WA_HEADER-KEY = 'Date: '.
  CONCATENATE  SY-DATUM+6(2) '.'
               SY-DATUM+4(2) '.'
               SY-DATUM(4) INTO WA_HEADER-INFO.   "todays date
  APPEND WA_HEADER TO IT_HEADER.
  CLEAR: WA_HEADER.
* Date
  WA_HEADER-TYP  = 'S'.
  WA_HEADER-KEY = 'User: '.
  CONCATENATE  SY-UNAME ' ' INTO WA_HEADER-INFO.   "Logged in user
  APPEND WA_HEADER TO IT_HEADER.
  CLEAR: WA_HEADER.
* Total No. of Records Selected
  DESCRIBE TABLE  IT_MARA LINES LD_LINES.
  LD_LINESC = LD_LINES.
  CONCATENATE 'Total No. of Records Selected: ' LD_LINESC
                    INTO LV_LINE SEPARATED BY SPACE.
  WA_HEADER-TYP  = 'A'.
  WA_HEADER-INFO = LV_LINE.
  APPEND WA_HEADER TO IT_HEADER.
  CLEAR: WA_HEADER, LV_LINE.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      IT_LIST_COMMENTARY = IT_HEADER.
  .
  CLEAR IT_HEADER.
ENDFORM.                    "TOP-OF-PAGE
FORM F_SAVE_DATA.
  DATA : WA_MARACP     TYPE TY_MARA.
  DATA : WA_MARA_TMP TYPE MARA.
  CLEAR IT_CHANGES[].
  LOOP AT IT_MARA INTO WA_MARA.
    READ TABLE IT_MARACP INTO WA_MARACP INDEX SY-TABIX.
    IF WA_MARACP NE WA_MARA.
      APPEND WA_MARA TO IT_CHANGES.
      MOVE-CORRESPONDING WA_MARA TO WA_MARA_TMP.
      MODIFY MARA FROM WA_MARA_TMP .
      IF SY-SUBRC EQ 0.
        MESSAGE 'Data Updated' TYPE 'I'.
      ENDIF.
    ENDIF.
    CLEAR WA_MARACP.
  ENDLOOP.
ENDFORM.                    "f_save_data
FORM USER_COMMAND  USING P_UCOMM    LIKE SY-UCOMM
                         P_SELFIELD TYPE SLIS_SELFIELD.
  "p_ucomm will hold user action like double click, save click
  CASE P_UCOMM.
    WHEN '&IC1'.        " SAP standard code for double-clicking
      READ TABLE IT_MARA INTO WA_MARA INDEX P_SELFIELD-TABINDEX. " Getting Row data
      CASE P_SELFIELD-FIELDNAME.
        WHEN 'MATNR'. " Column data
          SET PARAMETER ID: 'MAT' FIELD P_SELFIELD-VALUE.

          CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
        WHEN 'MTART'.
          MESSAGE P_SELFIELD-VALUE TYPE 'S'.
      ENDCASE.
    WHEN '&DATA_SAVE'.
            MESSAGE 'Trying to save' TYPE 'S'.
      PERFORM F_SAVE_DATA.

      "It_changes hold all the change made in the ALV
  ENDCASE.
ENDFORM.                    "user_command

editable ALV in ABAP

If you face any difficulties please add a comment below.

Author | Last Updated| | 11 Comments | F Share

Tutorial Comments

Total Comments: Add your Comment
14 May 2014

Thanks ashok , really appreciate your work

22 May 2014

can u send me code for sava field value after edit field in editable alv report more clear.

22 Jul 2014

ThanQ Ashok.....,its wonderful....am serching for this code but ididnt find these type of code.....u wrote veru understandable.....

10 Nov 2014

DATA UPDATED POPUP COMING 2 TIMES IF I UPDATE 2 COLUMNS . PLEASE HELP

11 Nov 2014

All fields are getting updated except checkbox values using the save code

21 May 2015

@aswathy please loop the table where checkbox eq 'X' and cick on save. before this action yu need to add one field in mara types declartion as chb type char1 in feild catlog checkbox = ''X pass

23 Jun 2015

Code is nice but data modifications is not happening sir

02 Sep 2015

Awesome Work...really very nice

05 Nov 2015

I cannot find COMMIT WORK. I think it's not possible to save e permanentes.

26 Jul 2016

Its really helpful. Can u send me the code while saving it should saved in material master using BAPI

21 Aug 2016

How can add the two custom buttons in alv report

Leave a Comment

Enter Comment Text