how to maintain log for custom tables

14
How to maintain log for custom tables (Log Maintenance in CDHDR & CDPOS tables) By Rahul Mehta, Tata Consultancy Services 1. Create one Z table ZEMP_TABLE. The table consists of three fields EMPID (Employee id), ENAME (Employee name) and ELOC (Employee Location). 2. Create the Z data elements and in the further characteristics tab of the data element tick the check box change document.

Upload: campeador2012

Post on 30-Nov-2015

56 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: How to Maintain Log for Custom Tables

How to maintain log for custom tables (Log Maintenance in CDHDR & CDPOS tables)

By Rahul Mehta, Tata Consultancy Services

1. Create one Z table ZEMP_TABLE.

The table consists of three fields EMPID (Employee id), ENAME (Employee name) and ELOC (Employee Location).  

2. Create the Z data elements and in the further characteristics tab of the data element tick the check box change document.  

3. Now Go to T-code SCDO. The screen looks like as follows.  

Page 2: How to Maintain Log for Custom Tables

Click on the create button.  

4. The following screen appears.  

  

Provide the name of the change document object.  

  

Click on the continue button and stored the object in a package or as a local object.  

5. The following screen appears.

Page 3: How to Maintain Log for Custom Tables

Provide the Text and the name of the table.  

Click on the insert entries button.  

6. The Following screen appears.  

Save it.  

Page 4: How to Maintain Log for Custom Tables

7. Now click on back button, the following screen appears.  

  

Put the cursor on the change document object ‘ZCHOBJ’ and click on ‘Generate Update Pgm’ button.  

8. The following screen appears.  

Provide the function group and click on the generate button.  

Page 5: How to Maintain Log for Custom Tables

  

9. The following screen appears.  

Page 6: How to Maintain Log for Custom Tables

When we see the above information one function module (ZCHOBJ_WRITE_DOCUMENT) is generated. The naming convention of this generated function module is as follows.  

Name of the changing document object + keyword (WRITE_DOCUMENT).  

Click on the save button.  

Now we will use this function module to maintain the logs in CDPOS & CDHDR tables.

11. Now see the parameters of this generated function module.  

Page 7: How to Maintain Log for Custom Tables

When we see the parameters of this function module it has 2 parameters. (N_ZEMP_TABLE, O_ ZEMP_TABLE).  

Where N stands for new data & O stands for old data. The naming conventions of these generated parameters are as follows-  

N_name of the table & O_name of the table.  

Imp Points:

1. To maintain a log for the insert entries: Pass the data to the new parameter. (N_ZEMP_TABLE).

2. To maintain a log for the update entries: Pass the new data to the new parameter (N_ZEMP_TABLE) and old data to the old parameter. (O_ZEMP_TABLE) 

3. To maintain a log for the Delete entries: Pass the data to the old parameter (O_ZEMP_TABLE).   

Now we will maintain log for insert, update and delete entries.  

12. Insert Log: We will insert one entry in table ZEMP_TABLE and maintain   the insertion log in CDHDR and CDPOS table.    

Create one executable program and write the following code.  

REPORT  zinsert.

DATA:ls_emp TYPE zemp_table.DATA:ls_emp_new TYPE zemp_table.DATA:ls_emp_old TYPE zemp_table.

Page 8: How to Maintain Log for Custom Tables

DATA:lt_cdtxt TYPE TABLE OF cdtxt.DATA:lv_objectid TYPE cdhdr-objectid.

ls_emp-empid = '1'.ls_emp-ename = 'STUDENT'.ls_emp-eloc = 'GANDHINAGAR'.

INSERT zemp_table FROM ls_emp.

lv_objectid = ls_emp-empid.ls_emp_new = ls_emp.

CALL FUNCTION 'ZCHOBJ_WRITE_DOCUMENT'  EXPORTING    objectid       = lv_objectid    tcode          = sy-tcode    utime          = sy-uzeit    udate          = sy-datum    username       = sy-uname    n_zemp_table   = ls_emp_new    o_zemp_table   = ls_emp_old    upd_zemp_table = 'I'  TABLES    icdtxt_zchobj  = lt_cdtxt.  

Note: In case of insertion pass the value of the parameter UPD_ZEMP_TABLE = ‘I’.  

Execute the program.  

After executing the program the table ZEMP_TABLE consists of following data.  

  

Now check the log in CDHDR and CDPOS tables.  

Go to table CDHDR and Pass the object class ‘ZCHOBJ’ (Which we have created using SCDO) and creation date (UDATE). (We can also give additional filter criteria).  

Page 9: How to Maintain Log for Custom Tables

Execute it and we will get the record in table CDHDR.

 

Copy the object class (OBJECTCLAS), object id (OBJECTID) and Document change number (CHANGENR) and go to table CDPOS. 

Provide the object class, object id and document change number.

Page 10: How to Maintain Log for Custom Tables

 

Execute it and the Table CDPOS consists of following data.

So the log for insert in maintained in CDPOS and CDHDR table.  

13. Update log: In this we will change the Employee name (ENAME) to ‘EMPLOYEE’ and Employee Location (ELOC) to ‘KURUKSHETRA’ in table ‘ZEMP_TABLE’ and maintain the Updation log.  

Create one executable program and write the following code.  

REPORT  ZCHANGE.

DATA:ls_emp TYPE zemp_table.DATA:ls_emp_new TYPE zemp_table.DATA:ls_emp_old TYPE zemp_table.DATA:lt_cdtxt TYPE TABLE OF cdtxt.DATA:lv_objectid TYPE cdhdr-objectid.

ls_emp_new-empid = '1'.

Page 11: How to Maintain Log for Custom Tables

ls_emp_new-ename = 'EMPLOYEE'.ls_emp_new-eloc = 'KURUKSHETRA'.

MODIFY zemp_table FROM ls_emp_new.

ls_emp_old-empid = '1'.ls_emp_old-ename = 'STUDENT'.ls_emp_old-eloc = 'GANDHINAGAR'.

lv_objectid = ls_emp_new-empid.

CALL FUNCTION 'ZCHOBJ_WRITE_DOCUMENT'  EXPORTING    objectid       = lv_objectid    tcode          = sy-tcode    utime          = sy-uzeit    udate          = sy-datum    username       = sy-uname    n_zemp_table   = ls_emp_new    o_zemp_table   = ls_emp_old    upd_zemp_table = 'U'  TABLES    icdtxt_zchobj  = lt_cdtxt.  

Note: In case of Updation pass the value of the parameter UPD_ZEMP_TABLE = ‘U’.  

Execute the program.  

After executing the program the table ZEMP_TABLE consists of following data.

Now check the log in CDHDR and CDPOS tables.  

CDHDR Entries:  

Page 12: How to Maintain Log for Custom Tables

CDPOS Entries:

In the field VALUE_NEW we will get the new values and in the field VALUE_OLD we will get the old values.  

14. Delete Log: In this we will delete this entry from table ‘ZEMP_TABLE’ and maintain the deletion log.  

Create an executable program and write the following code.  

REPORT  zdelete.

DATA:ls_emp TYPE zemp_table.DATA:ls_emp_new TYPE zemp_table.DATA:ls_emp_old TYPE zemp_table.DATA:lt_cdtxt TYPE TABLE OF cdtxt.DATA:lv_objectid TYPE cdhdr-objectid.

ls_emp_old-empid = '1'.DELETE zemp_table FROM ls_emp_old.

lv_objectid = ls_emp-empid.

CALL FUNCTION 'ZCHOBJ_WRITE_DOCUMENT'  EXPORTING    objectid       = lv_objectid    tcode          = sy-tcode    utime          = sy-uzeit    udate          = sy-datum    username       = sy-uname    n_zemp_table   = ls_emp_new    o_zemp_table   = ls_emp_old    upd_zemp_table = 'D'  TABLES    icdtxt_zchobj  = lt_cdtxt.  

Note: In case of deletion pass the value of the parameter UPD_ZEMP_TABLE = ‘D’.  

Execute the program. After executing the program the entry will be deleted from the table ZEMP_TABLE.  

Now check the log in CDHDR and CDPOS tables.  

CDHDR Entries:

Page 13: How to Maintain Log for Custom Tables

CDPOS Entries: