본문 바로가기
ERP-SAP/ABAP

<ABAP> Mainternence view event 01 생성/수정시간넣기

by 행복한워니의 기록 2013. 8. 26.
728x90
반응형
*&---------------------------------------------------------------------*
*&  데이터 생성 및 수정시 로그데이터(작업자, 일자, 시간) 업데이트.
*&---------------------------------------------------------------------*

*GELOESCHT : flagged for deletion
*NEUER_EINTRAG : New entry
*AENDERN : changed entry
*UPDATE_GELOESCHT : entry first changed and then flagged for deletion
*NEUER_GELOESCHT : entry first newly created, not yet saved, and then flagged for deletion
*ORIGINAL : the same as the database status

  DATA : l_index LIKE sy-tabix. "Index to note the lines found

  DATA : ls_component TYPE abap_componentdescr,
         lt_component TYPE abap_component_tab,

         lr_structure TYPE REF TO cl_abap_structdescr,
         lr_handle    TYPE REF TO data.


  FIELD-SYMBOLS : <l_structure> TYPE ANY,
                  <l_field>     TYPE ANY.

  lr_structure ?= cl_abap_structdescr=>describe_by_name( x_header-viewname ).
  lt_component = lr_structure->get_components( ).
  ls_component-type ?= cl_abap_datadescr=>describe_by_data( <action> ).
  ls_component-name = 'N_____'.
  APPEND ls_component TO lt_component.

  lr_structure = cl_abap_structdescr=>create( lt_component ).

  CREATE DATA lr_handle TYPE HANDLE lr_structure.
  ASSIGN lr_handle->* TO <l_structure>.

  LOOP AT total.
    IF <action> = neuer_eintrag OR <action> = aendern.
      READ TABLE extract WITH KEY <vim_xtotal_key>.

      IF sy-subrc EQ 0.
        l_index = sy-tabix.
      ELSE.
        CLEAR l_index.
      ENDIF.

      CHECK l_index GT 0.

      MOVE total TO <l_structure>.

      CASE <action>.
        WHEN aendern. "/Change/Update
          ASSIGN COMPONENT 'AENAM' OF STRUCTURE <l_structure> TO <l_field>.
          IF sy-subrc = 0.
            MOVE sy-uname TO <l_field>.
          ENDIF.

          ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <l_structure> TO <l_field>.
          IF sy-subrc = 0.
            MOVE sy-datum TO <l_field>.
          ENDIF.

          ASSIGN COMPONENT 'AEZET' OF STRUCTURE <l_structure> TO <l_field>.
          IF sy-subrc = 0.
            MOVE sy-uzeit TO <l_field>.
          ENDIF.

        WHEN neuer_eintrag. "/New Entries
          ASSIGN COMPONENT 'ERNAM' OF STRUCTURE <l_structure> TO <l_field>.
          IF sy-subrc = 0.
            MOVE sy-uname TO <l_field>.
          ENDIF.

          ASSIGN COMPONENT 'ERDAT' OF STRUCTURE <l_structure> TO <l_field>.
          IF sy-subrc = 0.
            MOVE sy-datum TO <l_field>.
          ENDIF.

          ASSIGN COMPONENT 'ERZET' OF STRUCTURE <l_structure> TO <l_field>.
          IF sy-subrc = 0.
            MOVE sy-uzeit TO <l_field>.
          ENDIF.
      ENDCASE.

      MOVE <l_structure> TO total.
      MODIFY total.

      extract = total.
      MODIFY extract INDEX l_index.
    ENDIF.
  ENDLOOP.

  sy-subrc = 0.

 

 

 

--------------------추가 구글링 정보

 

Table Maintenance with Create/Modified Information Automatically Filled in

If you are creating custom table which would be used as configuration table i.e. content of table would be rarely changed, then creating a custom build data entry screen is not worth the effort. In such cases using table maintenance generator is highly justified. Table Maintenance Generator is very powerful tool to create basic data entry screen. Although entry screen is basic, it comes with standard function of value help, value checked against check table, duplication check on primary key etc. With all these features within few minute it can provide you data entry screen with insert, change and delete function.

Table maintenance generator creates a function group which is combination of sap-standard includes, custom program and screen. The custom (usually name starting with Z) part of function group can be changed to add specific requirement. In this blog I will discuss about filling Created by username, create on date, created on time automatically when new record is inserted. And filling Updated by username, updated on date and updated on time when the record is modified.

Using this information you can certainly answer who created or last updated the record and when. If there is further requirement to track every change then I would recommend creating you own data entry screen and use Change Object Technique to log changes.

So, I have created a table as shown in screen shot below.



Notice that it has field ERNAM, ERDAT, ERZET and AENAM, AEDAT, AEZET. These fields stores the information which I would like table maintenance to fill-in automatically.

All these changes are done in following steps.

1. Create table maintenance
2. Hide the columns
3. Event to fill-in Created by Information
4. Event to fill-in Updated information

Step 1 Create table maintenance
Create table maintenance using table maintenance generator. At this stage you will have every column displayed in your screen.



Step 2 Hide the columns
You can either hide or deactivate these columns as we don’t want users to change these values. Open then function group created in SE80 and double click on screen which has table control. In the PBO just before the LOOP AT extract WITH CONTROL add a module.

Here, I’ve added module modify_element. Double click on the module and the module in new include.



This code will hide the column from table control.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
MODULE modify_element OUTPUT.
  LOOP AT <VIM_TCTRL>-cols INTO vim_tc_cols.
    IF vim_tc_cols-screen-name = 'ZLBN_ROUTETM-ERNAM' OR
       vim_tc_cols-screen-name = 'ZLBN_ROUTETM-ERDAT' OR
       vim_tc_cols-screen-name = 'ZLBN_ROUTETM-ERZET' OR
       vim_tc_cols-screen-name = 'ZLBN_ROUTETM-AENAM' OR
       vim_tc_cols-screen-name = 'ZLBN_ROUTETM-AEDAT' OR
       vim_tc_cols-screen-name = 'ZLBN_ROUTETM-AEZET' .
   
      vim_tc_cols-invisible = 1 .
      MODIFY <VIM_TCTRL>-cols FROM vim_tc_cols .
    ENDIF.
  ENDLOOP.
ENDMODULE.                 " MODIFY_ELEMENT  OUTPUT

Note: If you just want to deactivate the column you need to add module within LOOP AT extract WITH CONTOL and put deactivation code in there.

After this, save and activate the function module. At this point if you run table maintenance it should not show you the fields as shown in below screen shot.



Step 3 Event to fill-in Created by Information

We will add two events here which will be used to fill information in field at the time of adding new record and when records are changed.
To add event open table maintenance generator in change mode then in menu path Enviroment->Events add event 01 and 05 put a subroutine name and create subroutine by hitting button next to name.





In the form routine under event 05 add code which will fill created user, date and time information.

?
1
2
3
4
5
FORM zlbn_routetm_new_entry .
  zlbn_routetm-ernam = sy-uname .
  zlbn_routetm-erdat = sy-datum .
  zlbn_routetm-erzet = sy-uzeit .
ENDFORM.                    "ZLBN_ROUTETM_NEW_ENTRY


Step 4 Event to fill-in Updated information

In the form routine under event 01 put following code. This will fill update by information in respective fields.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
FORM zlbn_routetm_save .
  FIELD-SYMBOLS : <fs_field> TYPE ANY .
  
  LOOP AT total .
    CHECK <action> EQ aendern.
    ASSIGN COMPONENT 'AENAM' OF STRUCTURE <vim_total_struc> TO <fs_field> .
    IF sy-subrc = 0 .
      <fs_field> = sy-uname .
    ENDIF.
  
    ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <vim_total_struc> TO <fs_field> .
    IF sy-subrc = 0 .
      <fs_field> = sy-datum .
    ENDIF.
  
    ASSIGN COMPONENT 'AEZET' OF STRUCTURE <vim_total_struc> TO <fs_field> .
    IF sy-subrc = 0 .
      <fs_field> = sy-uzeit .
    ENDIF.
  
    READ TABLE extract WITH KEY <vim_xtotal_key>.
    IF sy-subrc = 0.
      extract = total .
      MODIFY extract INDEX sy-tabix.
    ENDIF.
    MODIFY total.
  ENDLOOP.
ENDFORM.                    "ZLBN_ROUTETM_SAVE

At this point all you need is a transaction code and your config table is good to go.  
728x90
반응형