SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Update a field from any table



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Thu Oct 04, 2007 9:03 pm    Post subject: Update a field from any table Reply with quote

Author: François Henrotte

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.
Code:
*&---------------------------------------------------------------------*

*& 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 '@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            = '@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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.