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

Changed Date - Changed By

kammaje_cis
SAP Mentor
SAP Mentor
0 Likes
6,331

Hi all,

I have created a custom table. In addition to custom fields, we need "changed Date(AEDTM)" and "changed by" fields in this table.

User is not going to fill these fields, system should fill this automatically.

for example: whenever a new record is created(using sm31), changed date = today's date, and changed by = user name.

How to achieve this?

Any idea?

Thanks

Krishna

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,859

Using the Table Maintenance event you can populate those fields.

You need to use the event 01

FORM f_actions_event_01.
IF vim_abort_saving IS INITIAL.
  * Update Change history fields
  INCLUDE ZZFSAI086_UPD_CHG_HSTY .
ENDIF.
ENDFORM. " f_actions_event_01

include code

 DATA:
  lv_index1 TYPE sy-tabix, "Index for Table 'TOTAL'
  lv_index2 TYPE sy-tabix, "Index for Table 'EXTRACT'
  lr_data   TYPE REF TO data, "Reference Data object
  lv_action TYPE char1, "Variable for View action
  lv_mark   TYPE char1. "Variable for View mark


FIELD-SYMBOLS:
  <lf_fval>  TYPE any, "For field value
  <ls_str>   TYPE any, "For data string
  <ls_xfrom> TYPE x, "Hexadecimal value of from value
  <ls_xto>   TYPE x. "Hexadecimal value of to value


* Dynamic creation of data object using the table name
CREATE DATA lr_data TYPE (x_header-viewname).
ASSIGN lr_data->* TO <ls_str>.


CLEAR: lv_index1, lv_index2, lr_data.
* Looping through all the records in the M.View
LOOP AT total.
  CLEAR lv_index1.
  lv_index1 = sy-tabix.
  READ TABLE extract WITH KEY total.
  IF sy-subrc EQ 0.
    lv_index2 = sy-tabix.
  ELSE.
    CLEAR lv_index2.
  ENDIF.
  ASSIGN total TO <ls_xfrom> CASTING.
  ASSIGN <ls_str> TO <ls_xto> CASTING.
  <ls_xto> = <ls_xfrom>.
  MOVE:
  <action> TO lv_action,
  <mark> TO lv_mark.
  CASE <action>.
    WHEN neuer_eintrag. " New entry
* If there is a new entry fill created by / date
      ASSIGN COMPONENT 'CRNAM' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        <lf_fval> = sy-uname.
      ENDIF.
      ASSIGN COMPONENT 'CRDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        <lf_fval> = sy-datum.
      ENDIF.
* If Changed by / date have to be blanked out if e.g. an entry is
      copied
     assign component 'AENAM' of structure <ls_str> to <lf_fval>.
      IF sy-subrc = 0.
        CLEAR <lf_fval>.
      ENDIF.


      ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        CLEAR <lf_fval>.
      ENDIF.


    WHEN aendern. "Updated entry
* Update to an existing entry fill in changed by / date
      ASSIGN COMPONENT 'AENAM' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        <lf_fval> = sy-uname.
      ENDIF.


      ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        <lf_fval> = sy-datum.
      ENDIF.
  ENDCASE.


*(make desired changes to the line total)


* Changes to be incorporated only when update or new entry.
  CHECK <action> EQ aendern OR <action> EQ neuer_eintrag.
  CLEAR: total,extract.
  ASSIGN <ls_str> TO <ls_xfrom> CASTING.
  ASSIGN total TO <ls_xto> CASTING.
  <ls_xto> = <ls_xfrom>.
  MOVE :
  lv_action TO <action>,
  lv_mark TO <mark>.
  CONCATENATE total lv_action lv_mark INTO total.
  MODIFY total FROM total INDEX lv_index1.
* modify extract if this is necessary
  CHECK lv_index2 GT 0.
  extract = total.
  MODIFY extract FROM extract INDEX lv_index2.
ENDLOOP.
5 REPLIES 5
Read only

Former Member
0 Likes
3,859

Hi,

In my opinion, you can do that with events :

using SE11 then Utility -> maintenance screen generator

Then via the menu -> environment -> modifications -> events

There you have to choose the appropriate event and define a FORM routine. This FORM routine is created within a form-include of the function group containing the maintenance dialogs.

If you want to set the default value at the start of the dialog and when the user creates new entries you will need events '21' (fill hidden fields) and '05' (creating a new entry).

For the events define a form routine within an include in your function group. There you have access to the structure of the view (e.g. z_tabl_view). There you fill z_tabl_view-column = <fixed value>.

For checking data before saving you will need event 01 (before saving the data in the database).

For detailed information please refer to the SAP online documentation:

http://help.sap.com/saphelp_erp2005vp/helpdata/en/91/ca9f0ea9d111d1a5690000e82deaaa/content.htm

Hope this helps,

Erwan

Read only

0 Likes
3,859

Table maintenance screen generator

environment -> modifications -> events

write your code here

Read only

Former Member
0 Likes
3,860

Using the Table Maintenance event you can populate those fields.

You need to use the event 01

FORM f_actions_event_01.
IF vim_abort_saving IS INITIAL.
  * Update Change history fields
  INCLUDE ZZFSAI086_UPD_CHG_HSTY .
ENDIF.
ENDFORM. " f_actions_event_01

include code

 DATA:
  lv_index1 TYPE sy-tabix, "Index for Table 'TOTAL'
  lv_index2 TYPE sy-tabix, "Index for Table 'EXTRACT'
  lr_data   TYPE REF TO data, "Reference Data object
  lv_action TYPE char1, "Variable for View action
  lv_mark   TYPE char1. "Variable for View mark


FIELD-SYMBOLS:
  <lf_fval>  TYPE any, "For field value
  <ls_str>   TYPE any, "For data string
  <ls_xfrom> TYPE x, "Hexadecimal value of from value
  <ls_xto>   TYPE x. "Hexadecimal value of to value


* Dynamic creation of data object using the table name
CREATE DATA lr_data TYPE (x_header-viewname).
ASSIGN lr_data->* TO <ls_str>.


CLEAR: lv_index1, lv_index2, lr_data.
* Looping through all the records in the M.View
LOOP AT total.
  CLEAR lv_index1.
  lv_index1 = sy-tabix.
  READ TABLE extract WITH KEY total.
  IF sy-subrc EQ 0.
    lv_index2 = sy-tabix.
  ELSE.
    CLEAR lv_index2.
  ENDIF.
  ASSIGN total TO <ls_xfrom> CASTING.
  ASSIGN <ls_str> TO <ls_xto> CASTING.
  <ls_xto> = <ls_xfrom>.
  MOVE:
  <action> TO lv_action,
  <mark> TO lv_mark.
  CASE <action>.
    WHEN neuer_eintrag. " New entry
* If there is a new entry fill created by / date
      ASSIGN COMPONENT 'CRNAM' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        <lf_fval> = sy-uname.
      ENDIF.
      ASSIGN COMPONENT 'CRDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        <lf_fval> = sy-datum.
      ENDIF.
* If Changed by / date have to be blanked out if e.g. an entry is
      copied
     assign component 'AENAM' of structure <ls_str> to <lf_fval>.
      IF sy-subrc = 0.
        CLEAR <lf_fval>.
      ENDIF.


      ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        CLEAR <lf_fval>.
      ENDIF.


    WHEN aendern. "Updated entry
* Update to an existing entry fill in changed by / date
      ASSIGN COMPONENT 'AENAM' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        <lf_fval> = sy-uname.
      ENDIF.


      ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
      IF sy-subrc = 0.
        <lf_fval> = sy-datum.
      ENDIF.
  ENDCASE.


*(make desired changes to the line total)


* Changes to be incorporated only when update or new entry.
  CHECK <action> EQ aendern OR <action> EQ neuer_eintrag.
  CLEAR: total,extract.
  ASSIGN <ls_str> TO <ls_xfrom> CASTING.
  ASSIGN total TO <ls_xto> CASTING.
  <ls_xto> = <ls_xfrom>.
  MOVE :
  lv_action TO <action>,
  lv_mark TO <mark>.
  CONCATENATE total lv_action lv_mark INTO total.
  MODIFY total FROM total INDEX lv_index1.
* modify extract if this is necessary
  CHECK lv_index2 GT 0.
  extract = total.
  MODIFY extract FROM extract INDEX lv_index2.
ENDLOOP.
Read only

0 Likes
3,859

Hi Vijaya,

Thanks a lot for the solution. It is working well

Read only

Former Member
0 Likes
3,859

Hi Vijaya,

just like to thank you for the codes. It works perfectly for me. keep up the good work. Thanks again.

Regards

Edwin