Downtime Announcement: Please note that SAP Community Wiki will be unavailable due to a system upgrade on Tuesday, August 11th between 7 and 11 AM CEST
Skip to end of metadata
Go to start of metadata

Author: François Henrotte
Submitted: 2007-02-08

This is a sample program to illustrate several techniques:

  • use of dynamic selections
  • persistent data stored into cluster INDX
  • data definition at runtime 
  • display internal table using SALV class
  • local class used as listener to ALV events (include is given at end of code).

Just copy the code into a new program, create include for events, then copy status STANDARD from program SAPLSALV. You have to add the SAVE function code to the disk button. This will allow you to save modifications to the database.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
*&---------------------------------------------------------------------*
*& Report  ZEPO_UPDATE_TABLE
*&---------------------------------------------------------------------*
*& Program to update table contents (one field at a time)
*& Use at your own risks - misuse can lead to database inconsistencies
*----------------------------------------------------------------------*
* Author : François Henrotte (www.eponasolutions.com)                  *
*----------------------------------------------------------------------*
REPORT zepo_update_table NO STANDARD PAGE HEADING.

***
*   Data definition
***

TYPE-POOLS: rsds.

DATA: is_x030l   TYPE x030l,
      it_x031l   TYPE TABLE OF x031l,
      is_x031l   TYPE x031l.

DATA: w_selid    TYPE rsdynsel-selid,
      it_tables  TYPE TABLE OF rsdstabs,
      is_tables  TYPE rsdstabs,
      it_fields  TYPE TABLE OF rsdsfields,
      it_expr    TYPE rsds_texpr,
      it_ranges  TYPE rsds_trange,
      it_where   TYPE rsds_twhere,
      is_where   TYPE rsds_where,
      w_active   TYPE i.

DATA: w_repid    TYPE sy-repid,
      w_dynnr    TYPE sy-dynnr,
      wt_dynp    TYPE TABLE OF dynpread,
      ws_dynp    TYPE dynpread.

DATA: it_content TYPE REF TO data,
      is_content TYPE REF TO data.

DATA: w_okcode   TYPE sy-ucomm.

DATA: w_fdkey    TYPE x VALUE '01'.

DATA: w_akey     TYPE indx-srtfd,
      w_rkey     TYPE indx-srtfd,
      w_fkey     TYPE indx-srtfd.

* Include to handle events on ALV display screen
INCLUDE zbc_query_events.


FIELD-SYMBOLS: <itab> TYPE STANDARD TABLE,
               <irec> TYPE ANY.

***
* Macros
***

DEFINE table_error.
  message e398(00) with 'Table' p_table &1.
END-OF-DEFINITION.

DEFINE fixed_val.
  assign component is_x031l-fieldname of structure <irec> to <fld>.
  if sy-subrc = 0.
    <fld> = &1.
  endif.
END-OF-DEFINITION.


***
* Selection screen
***

SELECTION-SCREEN: BEGIN OF BLOCK b01 WITH FRAME.

PARAMETERS: p_table TYPE tabname OBLIGATORY                    "table
                                 MEMORY ID dtb
                                 MATCHCODE OBJECT dd_dbtb_16.

SELECTION-SCREEN: BEGIN OF LINE,

                  PUSHBUTTON 33(20) selopt USER-COMMAND sel,
                  COMMENT    55(15) selcnt,
                  END OF LINE.

SELECTION-SCREEN: SKIP.

PARAMETERS: p_field TYPE fieldname,                            "field
            p_value TYPE text132.                              "value

SELECTION-SCREEN: END OF BLOCK b01,

                  SKIP,
                  BEGIN OF BLOCK b02 WITH FRAME.

PARAMETERS: p_displ TYPE c AS CHECKBOX DEFAULT 'X',            "display
            p_systm TYPE c AS CHECKBOX.                        "system

SELECTION-SCREEN: END OF BLOCK b02.

***
* Initialization
***

INITIALIZATION.
  MOVE '<at:var at:name="4G" /> Filter records' TO selopt.
  ws_dynp-fieldname = 'P_TABLE'.
  APPEND ws_dynp TO wt_dynp.

* Get dynamic selection from cluster

  w_akey(1)   = 'A'.
  w_akey+1(12) 	= sy-uname.

  IMPORT w_active  FROM DATABASE indx(xy) ID w_akey.
  w_rkey(1)   = 'R'.
  w_rkey+1(12) 	= sy-uname.

  IMPORT it_expr   FROM DATABASE indx(xy) ID w_rkey.

  w_rkey(1)   = 'F'.
  w_rkey+1(12) 	= sy-uname.

  IMPORT it_fields FROM DATABASE indx(xy) ID w_fkey.

***
* PBO
***

AT SELECTION-SCREEN OUTPUT.

  IF w_active IS INITIAL.
    CLEAR: selcnt.
  ELSE.
    WRITE w_active TO selcnt LEFT-JUSTIFIED.
  ENDIF.

***
* PAI
***

AT SELECTION-SCREEN.

  IF p_table NE is_x030l-tabname.
    PERFORM f_init_table.
  ENDIF.

  IF sy-ucomm = 'SEL'.
    IF w_selid IS INITIAL.
      PERFORM f_init_selections.
    ENDIF.

*   Display free selection dialog

    CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
      EXPORTING
        selection_id            = w_selid
        title                   = 'Selection'
        status                  = 1
        as_window               = 'X'
      IMPORTING
        expressions             = it_expr
        field_ranges            = it_ranges
        number_of_active_fields = w_active
      TABLES
        fields_tab              = it_fields
      EXCEPTIONS
        OTHERS                  = 1.



*   Write dynamic selection to cluster

    w_akey(1)   = 'A'.
    w_akey+1(12) 	= sy-uname.

    EXPORT w_active  TO DATABASE indx(xy) ID w_akey.

    w_rkey(1)   = 'R'.
    w_rkey+1(12) 	= sy-uname.

    EXPORT it_expr   TO DATABASE indx(xy) ID w_rkey.

    w_rkey(1)   = 'F'.
    w_rkey+1(12) 	= sy-uname.

    EXPORT it_fields TO DATABASE indx(xy) ID w_fkey.

  ENDIF.

  IF p_field IS NOT INITIAL.

    READ TABLE it_x031l INTO is_x031l
                    WITH KEY fieldname = p_field.

    IF sy-subrc = 0.

      IF is_x031l-flag1 O w_fdkey.
        MESSAGE e129(53) WITH p_field p_table.
      ENDIF.

    ELSE.

      MESSAGE e804(5g) WITH p_field p_table.

    ENDIF.

  ENDIF.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_field.

  w_repid = sy-repid.
  w_dynnr = sy-dynnr.

  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname     = w_repid
      dynumb     = w_dynnr
    TABLES
      dynpfields = wt_dynp
    EXCEPTIONS
      OTHERS     = 1.

  IF sy-subrc = 0.

    READ TABLE wt_dynp INTO ws_dynp INDEX 1.

    p_table = ws_dynp-fieldvalue.

    CALL FUNCTION 'F4_DD_TABLE_FIELDS'
      EXPORTING
        table  = p_table
      IMPORTING
        RESULT = p_field.

  ENDIF.

***
* Start of processing
***

START-OF-SELECTION.

  PERFORM f_create_table USING p_table.
  PERFORM f_select_table.
  PERFORM f_modify_table.
  PERFORM f_display_table USING <itab>.

*&---------------------------------------------------------------------*
*&      Form  f_init_table
*&---------------------------------------------------------------------*
FORM f_init_table.

*   Prepare free selection on table
  PERFORM f_table_def USING p_table.

  REFRESH it_tables.
  is_tables-prim_tab = p_table.
  APPEND is_tables TO it_tables.

  CLEAR: w_selid.

ENDFORM.                    "f_init_table

*&---------------------------------------------------------------------*
*&      Form  f_init_selections
*&---------------------------------------------------------------------*

FORM f_init_selections.

* Init free selection dialog

  CALL FUNCTION 'FREE_SELECTIONS_INIT'
    EXPORTING
      expressions  = it_expr
    IMPORTING
      selection_id = w_selid
      expressions  = it_expr
    TABLES
      tables_tab   = it_tables
      fields_tab   = it_fields
    EXCEPTIONS
      OTHERS       = 1.

ENDFORM.                    "f_init_selections

*---------------------------------------------------------------------*
*       FORM f_table_def                                              *
*---------------------------------------------------------------------*

FORM f_table_def USING in_tabname.

  CALL FUNCTION 'DDIF_NAMETAB_GET'
    EXPORTING
      tabname   = p_table
    IMPORTING
      x030l_wa  = is_x030l
    TABLES
      x031l_tab = it_x031l
    EXCEPTIONS
      OTHERS    = 1.

  IF is_x030l IS INITIAL.

    table_error 'does not exist or is not active'.

  ELSEIF is_x030l-tabtype NE 'T'.

    table_error 'is not selectable'.

  ENDIF.

ENDFORM.                    "f_table_def

*---------------------------------------------------------------------*
*       FORM f_create_table                                           *
*---------------------------------------------------------------------*

FORM f_create_table USING in_tabname.

  CREATE DATA it_content TYPE TABLE OF (in_tabname).

  IF sy-subrc = 0.

    ASSIGN it_content->* TO <itab>.

  ELSE.

    WRITE: 'Error creating internal table'.

    STOP.

  ENDIF.

ENDFORM.                    "f_create_table

*---------------------------------------------------------------------*
*       FORM f_select_table                                           *
*---------------------------------------------------------------------*

FORM f_select_table.

  IF w_active = 0.

    SELECT * FROM (p_table)
             INTO CORRESPONDING FIELDS OF TABLE <itab>.

  ELSE.

*   Selection with parameters

    CALL FUNCTION 'FREE_SELECTIONS_EX_2_WHERE'
      EXPORTING
        expressions              = it_expr
      IMPORTING
        where_clauses            = it_where
      EXCEPTIONS
        expression_not_supported = 1
        OTHERS                   = 2.

    READ TABLE it_where INTO is_where WITH KEY tablename = p_table.

    SELECT * FROM (p_table)
             INTO CORRESPONDING FIELDS OF TABLE <itab>
             WHERE (is_where-where_tab).

  ENDIF.

  IF sy-dbcnt = 0.
    WRITE: 'No record selected'.
    STOP.
  ENDIF.

ENDFORM.                    "f_select_table



*---------------------------------------------------------------------*
*       FORM f_modify_table                                           *
*---------------------------------------------------------------------*

FORM f_modify_table.

  FIELD-SYMBOLS: <fld> TYPE ANY.


  LOOP AT <itab> ASSIGNING <irec>.

    IF p_field IS NOT INITIAL.

      ASSIGN COMPONENT p_field OF STRUCTURE <irec> TO <fld>.

      IF sy-subrc = 0.

        CALL FUNCTION 'GENERIC_CONVERSION_EXIT_INPUT'
          EXPORTING
            i_tabname               = p_table
            i_fieldname             = p_field
            input_text              = p_value
          IMPORTING
            output_text             = <fld>
          EXCEPTIONS
            invalid_ddic_parameters = 1
            invalid_input           = 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.

      ENDIF.

    ENDIF.

    IF p_systm IS NOT INITIAL.

*     Default values for system fields

      LOOP AT it_x031l INTO is_x031l.

        IF is_x031l-dtyp = 'CLNT'.

          fixed_val sy-mandt.

        ELSEIF is_x031l-rollname = 'ERDAT'
            OR is_x031l-rollname = 'ERSDA'
            OR is_x031l-rollname = 'AEDAT'
            OR is_x031l-rollname = 'LAEDA'.
          fixed_val sy-datum.

        ELSEIF is_x031l-rollname = 'ERTIM'
            OR is_x031l-rollname = 'AETIM'.

          fixed_val sy-uzeit.

        ELSEIF is_x031l-rollname = 'ERNAM'
            OR is_x031l-rollname = 'AENAM'.

          fixed_val sy-uname.

        ENDIF.

      ENDLOOP.

    ENDIF.

  ENDLOOP.

ENDFORM.                    "f_modify_table

*---------------------------------------------------------------------*
*       FORM f_display_table                                          *
*---------------------------------------------------------------------*

FORM f_display_table USING in_table.

  DATA: ob_table TYPE REF TO cl_salv_table,
        ob_event TYPE REF TO cl_salv_events_table,
        cx_error TYPE REF TO cx_salv_msg.

  TRY.

      CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table = ob_table
        CHANGING
          t_table      = in_table.

    CATCH cx_salv_msg INTO cx_error.

      EXIT.

  ENDTRY.



  IF p_displ IS INITIAL AND
     p_field IS NOT INITIAL.

    CALL METHOD ob_table->set_screen_status
      EXPORTING
        report        = 'ZBC_UPDATE_TABLE'
        pfstatus      = 'STANDARD'
        set_functions = ob_table->c_functions_all.

    ob_event = ob_table->get_event( ).
    CREATE OBJECT ob_appl.

    SET HANDLER ob_appl->on_user_command FOR ob_event.

  ELSE.

    CALL METHOD ob_table->set_screen_status
      EXPORTING
        report        = 'SAPLSALV'
        pfstatus      = 'STANDARD'
        set_functions = ob_table->c_functions_all.

  ENDIF.

  CALL METHOD ob_table->display.

ENDFORM.                    "f_display_table

*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*

FORM user_command USING in_command TYPE salv_de_function.

  DATA: l_messg  TYPE string,
        l_answer TYPE c.

  DATA: ls_expr  TYPE string.

  CHECK in_command = 'SAVE'.

  l_messg = 'Overwrite field for all selected records ?'.



  CALL FUNCTION 'POPUP_TO_CONFIRM'
    EXPORTING
      titlebar              = 'Update table'
      text_question         = l_messg
      default_button        = '2'
      display_cancel_button = ' '
      popup_type            = '<at:var at:name="1A" />'
    IMPORTING
      answer                = l_answer
    EXCEPTIONS
      text_not_found        = 1
      OTHERS                = 2.

  IF l_answer = '1'.

    CONCATENATE p_field '= ''' INTO ls_expr
                    SEPARATED BY space.

    CONCATENATE ls_expr p_value '''' INTO ls_expr.

    TRY.

        UPDATE (p_table) SET (ls_expr)
                       WHERE (is_where-where_tab).

      CATCH cx_sy_dynamic_osql_error.
        ROLLBACK WORK.
        MESSAGE 'Error during update!' TYPE 'I'.

    ENDTRY.

    IF sy-subrc = 0.
      COMMIT WORK.
    ENDIF.

  ENDIF.

ENDFORM.                    "user_command

*&---------------------------------------------------------------------*
*&  Include           ZBC_QUERY_EVENTS
*&---------------------------------------------------------------------*

CLASS lcl_handle_events DEFINITION DEFERRED.

DATA: ob_appl TYPE REF TO lcl_handle_events.

*----------------------------------------------------------------------*
*       CLASS lcl_handle_events DEFINITION
*----------------------------------------------------------------------*

CLASS lcl_handle_events DEFINITION.

  PUBLIC SECTION.
    METHODS:

      on_user_command FOR EVENT added_function OF cl_salv_events
        IMPORTING e_salv_function,

      on_before_salv_function FOR EVENT before_salv_function OF cl_salv_events
        IMPORTING e_salv_function,

      on_after_salv_function FOR EVENT after_salv_function OF cl_salv_events
        IMPORTING e_salv_function,

      on_double_click FOR EVENT double_click OF cl_salv_events_table
        IMPORTING row column,

      on_link_click FOR EVENT link_click OF cl_salv_events_table
        IMPORTING row column.

ENDCLASS.                    "lcl_handle_events DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_handle_events IMPLEMENTATION
*----------------------------------------------------------------------*

CLASS lcl_handle_events IMPLEMENTATION.

  METHOD on_user_command.

    PERFORM user_command IN PROGRAM (sy-repid) IF FOUND
                              USING e_salv_function.

  ENDMETHOD.                    "on_user_command

  METHOD on_before_salv_function.

    PERFORM before_function IN PROGRAM (sy-repid) IF FOUND
                                 USING e_salv_function.

  ENDMETHOD.                    "on_before_salv_function

  METHOD on_after_salv_function.

    PERFORM after_function IN PROGRAM (sy-repid) IF FOUND
                                USING e_salv_function.

  ENDMETHOD.                    "on_after_salv_function

  METHOD on_double_click.

    PERFORM double_click IN PROGRAM (sy-repid) IF FOUND
                              USING row column.

  ENDMETHOD.                    "on_double_click



  METHOD on_link_click.

    PERFORM link_click IN PROGRAM (sy-repid) IF FOUND
                            USING row column.

  ENDMETHOD.                    "on_single_click

ENDCLASS.                    "lcl_handle_events IMPLEMENTATION