Applies to:
SAP ECC 6.0
Summary
In SAP implementation for a business, not all the requirements are covered in the standard SAP tables. There are possibilities wherein custom tables are used to hold important data.
This tutorial will explain on tracking data changes in custom tables through standard SAP change tables.
Author: Booma Ganesan
Company: Infosys Technologies Limited
Created on: 18 September 2009
Author Bio
Booma Ganesan is working as an ABAP consultant in Infosys Technologies Limited,chennai. She has completed Bachelor of Technology in Information Technology.
Table of Contents
Scope
Based on the customeer needs, custom tables are used to to hold important data and hence making it necessary to track the changes madeto them.
Standard change tables CDHR and CDPOS are used for tracking the changes made to the standard SAP objects.This tutorial will explain the procedure to track the changes made to custom tables and logging those changes in standard SAP change tables.
Two major processes involved in tracking the changes of custom tables are -
- Change document object
- Function module for the change document object
Illustration
Let us take an example of using a custom table to hold employee details with their email ids.
Pre-requisite
To support the tracking process we need to enable the check box for 'change document' in the respective data elements for all those which needs to be tracked.
The sample screen shot is as shown below.
Step by step explanation on creation of change document object
1. Go to Transaction code SCDO. Click on 'Create' button to start the creation of change document object.
2. Enter the name of the change document object in the pop-up screen. In our example the change document object class name is 'YTEST_TABLE_CNG".
Click the continue button. A pop-up screen to confirm the activity is displayed. Press yes to confirm in the confirmation screen. In the next screen, specify the package and TR details as required.
3. Specify the text for change document object and the table name, for which the change document object is been created. Press enter to continue.
4. Click on the menu 'Utilities (M)' and select 'Generate Update Program'. Press yes in the pop-up screen and continue.
5. A new screen will be displayed, wherein we need to specify the function group name.
Press yes to confirm the generation of the function group for the change document object.
6. All the details like the function group, function module and include names are listed before generation. Press "SAVE" to complete the program generation for the change document object.
Note: The change document object will be saved in a workbench request and it follows the regular TR migration procedure.
Now change document object for the custom table is created. Function module created through the program generation needs to be used for logging the changes in the standard SAP change tables - CDHDR and CDPOS. The operations like Insert, Update and Delete functionalities can be done on the entries available in the custom table.
For performing all the function, we need to add variable of type CHAR1 in the import parameters of the function module. When the value is passed as 'X' and the operation to be performed is 'D' (delete), then the current record is logged for deletion in CDHDR.
All the changes to the data available in the custom tables can be tracked with the change document object.
Using change document object's Function Module
To have more clarity on the tracking procedure, I have explained the usage of the function module through a report program. Before running the report program, our custom table is maintained with few entries to enable the update operation.
The report program will take the empno and type of operations to be performed from the selection screen and execute the set of codes accordingly.
Initially no details are availble against our change object class in both CDHDR and CDPOS table.
Update Functionality
Now the report program is executed with the below inputs
EMP no: 1001
Operation to be performed: update
* fetch all the details from the Ztable SELECT SINGLE * FROM ytest_table INTO gs_test WHERE zemp_no = p_emp_no. IF sy-subrc = 0. * move the old entries CLEAR ls_test. ls_test = gs_test. * New Values gs_test-zemp_no = p_emp_no. gs_test-zename = 'new_name1'. gs_test-zmail_id =After execution the CDHDR table is logged with one entry for our change objectEsta dirección de correo electrónico está siendo protegida contra los robots de spam. Necesita tener JavaScript habilitado para poder verlo. '. gs_test-zcontact_no = '02211336655'. APPEND gs_test TO gt_test. *Populate change tables CLEAR gs_change. gs_change-teilobjid = 'YTEST_TABLE_CNG'. gs_change-textart = 'TEST_2'. gs_change-textspr = 'EN'. gs_change-updkz = 'U'. APPEND gs_change TO gt_change. *Call the FM to log the values in CDHDR table. CALL FUNCTION 'YTEST_TABLE_CNG_WRITE_DOCUMENT' EXPORTING objectid = 'YTEST_TABLE_CNG' tcode = sy-tcode utime = sy-uzeit udate = sy-datum username = sy-uname planned_change_number = ' ' object_change_indicator = 'U' planned_or_real_changes = 'U' no_change_pointers = 'U' upd_icdtxt_ytest_table_cng = 'U' n_ytest_table = gs_test o_ytest_table = ls_test upd_ytest_table = 'U' lv_opt = ' ' TABLES icdtxt_ytest_table_cng = gt_change.
Corresponding CDPOS values
Likewise all the other modified fields are logged in CDPOS table.
Modified values reflecting in custome tables using update statement.
Insert functionality
The report program is executed for insert functionality with the below inputs
CLEAR: GS_TEST, LS_TEST. GS_TEST-ZEMP_NO = P_EMP_NO. GS_TEST-ZENAME = 'name_ins'. GS_TEST-ZMAIL_ID =The details logged in CDHDR and CDPOS are as below.Esta dirección de correo electrónico está siendo protegida contra los robots de spam. Necesita tener JavaScript habilitado para poder verlo. '. GS_TEST-ZCONTACT_NO = '1112131415'. APPEND GS_TEST TO GT_TEST. *populate change table CLEAR GS_CHANGE. GS_CHANGE-TEILOBJID = 'YTEST_TABLE_CNG'. GS_CHANGE-TEXTART = 'TEST_1'. GS_CHANGE-TEXTSPR = 'EN'. GS_CHANGE-UPDKZ = 'I'. APPEND GS_CHANGE TO GT_CHANGE. *call the fM to log the values in CDHDR table. CALL FUNCTION 'YTEST_TABLE_CNG_WRITE_DOCUMENT' EXPORTING OBJECTID = 'YTEST_TABLE_CNG' TCODE = SY-TCODE UTIME = SY-UZEIT UDATE = SY-DATUM USERNAME = SY-UNAME PLANNED_CHANGE_NUMBER = ' ' OBJECT_CHANGE_INDICATOR = 'I' PLANNED_OR_REAL_CHANGES = 'I' NO_CHANGE_POINTERS = 'I' UPD_ICDTXT_YTEST_TABLE_CNG = 'I' N_YTEST_TABLE = GS_TEST O_YTEST_TABLE = LS_TEST UPD_YTEST_TABLE = 'I' LV_OPT = ' ' TABLES ICDTXT_YTEST_TABLE_CNG = GT_CHANGE.
Corresponding entries in CDPOS
The new entry is added to the custom table using insert statement.
Delete Functionality
The report program is executed for delete functionality with the below inputs
* fetch all the details from the ztable. SELECT SINGLE * FROM YTEST_TABLE INTO GS_TEST WHERE ZEMP_NO = P_EMP_NO. *move the old entries IF SY-SUBRC = 0. CLEAR LS_TEST. LS_TEST = GS_TEST. CLEAR GS_TEST. *populate change table CLEAR GS_CHANGE. GS_CHANGE-TEILOBJID = 'YTEST_TABLE_CNG'. GS_CHANGE-TEXTART = 'TEST_3'. GS_CHANGE-TEXTSPR = 'EN'. GS_CHANGE-UPDKZ = 'D'. APPEND GS_CHANGE TO GT_CHANGE. *call the fM to log the values in CDHDR table. CALL FUNCTION 'YTEST_TABLE_CNG_WRITE_DOCUMENT' EXPORTING OBJECTID = 'YTEST_TABLE_CNG' TCODE = SY-TCODE UTIME = SY-UZEIT UDATE = SY-DATUM USERNAME = SY-UNAME PLANNED_CHANGE_NUMBER = ' ' OBJECT_CHANGE_INDICATOR = 'D' PLANNED_OR_REAL_CHANGES = 'D' NO_CHANGE_POINTERS = 'D' UPD_ICDTXT_YTEST_TABLE_CNG = 'D' N_YTEST_TABLE = GS_TEST O_YTEST_TABLE = LS_TEST UPD_YTEST_TABLE = 'D' LV_OPT = 'X' TABLES ICDTXT_YTEST_TABLE_CNG = GT_CHANGE.Log in CDHDR
Note: In CDPOS, the change ID is logged as 'E' - Delete (Single field documentation). In CDPOS all the changes made to particular row is been logged field wise and hence the change in ID.
The screen shot of the modified values reflecting in the custom table.
Reading Change logs
I have also added a button in application toolbar to list all the details logged in CDHDR And its corresponding entries in CDPOS tables in an alv grid display.
To read the entries from the change tables - CDHDR , CDPOS the below two functiona modules are used.
- CHANGEDOCUMENT_READ_HEADERS
- CHANGEDOCUMENT_READ_POSITIONS
DATA: LS_CDRED LIKE LINE OF GT_CDRED. CALL FUNCTION 'CHANGEDOCUMENT_READ_HEADERS' EXPORTING ARCHIVE_HANDLE = 0 DATE_OF_CHANGE = '00000000' OBJECTCLASS = 'YTEST_TABLE_CNG' OBJECTID = ' ' TIME_OF_CHANGE = '000000' USERNAME = SY-UNAME LOCAL_TIME = ' ' DATE_UNTIL = '99991231' TIME_UNTIL = '235959' NOPLUS_ASWILDCARD_INOBJID = ' ' TABLES I_CDHDR = GT_CDHDR EXCEPTIONS NO_POSITION_FOUND = 1 WRONG_ACCESS_TO_ARCHIVE = 2 TIME_ZONE_CONVERSION_ERROR = 3 OTHERS = 4. IF SY-SUBRC <> 0. MESSAGE 'No entries found' TYPE 'E'. ENDIF. IF NOT GT_CDHDR IS INITIAL. LOOP AT GT_CDHDR INTO GS_CDHDR. CALL FUNCTION 'CHANGEDOCUMENT_READ_POSITIONS' EXPORTING CHANGENUMBER = GS_CDHDR-CHANGENR TABLES EDITPOS = GT_CDSHW EDITPOS_WITH_HEADER = GT_CDRED EXCEPTIONS NO_POSITION_FOUND = 1 WRONG_ACCESS_TO_ARCHIVE = 2 OTHERS = 3. IF SY-SUBRC <> 0. MESSAGE 'No item details found' TYPE 'E'. ELSE. LOOP AT GT_CDRED INTO LS_CDRED. MOVE-CORRESPONDING GS_CDHDR TO GS_OUT. MOVE-CORRESPONDING LS_CDRED TO GS_OUT. APPEND GS_OUT TO GT_OUT. CLEAR: GS_OUT, LS_CDRED. ENDLOOP. CLEAR: GS_CDHDR. ENDIF. ENDLOOP. ENDIF.
Report Output
Related Content
https://forums.sdn.sap.com/message.jspa?messageID=2430357http://sample-code-abap.blogspot.com/2008/02/applying-change-document-technique-on.html (not available any more as of February 9th, 2016)
* fetch all the details from the Ztable
SELECT SINGLE * FROM ytest_table
INTO gs_test
WHERE zemp_no = p_emp_no.
IF sy-subrc = 0.
* move the old entries
CLEAR ls_test.
ls_test = gs_test.
* New Values
gs_test-zemp_no = p_emp_no.
gs_test-zename = 'new_name1'.
gs_test-zmail_id =
gs_test-zcontact_no = '02211336655'.
APPEND gs_test TO gt_test.
*Populate change tables
CLEAR gs_change.
gs_change-teilobjid = 'YTEST_TABLE_CNG'.
gs_change-textart = 'TEST_2'.
gs_change-textspr = 'EN'.
gs_change-updkz = 'U'.
APPEND gs_change TO gt_change.
*Call the FM to log the values in CDHDR table.
CALL FUNCTION 'YTEST_TABLE_CNG_WRITE_DOCUMENT'
EXPORTING
objectid = 'YTEST_TABLE_CNG'
tcode = sy-tcode
utime = sy-uzeit
udate = sy-datum
username = sy-uname
planned_change_number = ' '
object_change_indicator = 'U'
planned_or_real_changes = 'U'
no_change_pointers = 'U'
upd_icdtxt_ytest_table_cng = 'U'
n_ytest_table = gs_test
o_ytest_table = ls_test
upd_ytest_table = 'U'
lv_opt = ' '
TABLES
icdtxt_ytest_table_cng = gt_change.
* fetch all the details from the Ztable
SELECT SINGLE * FROM ytest_table
INTO gs_test
WHERE zemp_no = p_emp_no.
IF sy-subrc = 0.
* move the old entries
CLEAR ls_test.
ls_test = gs_test.
* New Values
gs_test-zemp_no = p_emp_no.
gs_test-zename = 'new_name1'.
gs_test-zmail_id =
gs_test-zcontact_no = '02211336655'.
APPEND gs_test TO gt_test.
*Populate change tables
CLEAR gs_change.
gs_change-teilobjid = 'YTEST_TABLE_CNG'.
gs_change-textart = 'TEST_2'.
gs_change-textspr = 'EN'.
gs_change-updkz = 'U'.
APPEND gs_change TO gt_change.
*Call the FM to log the values in CDHDR table.
CALL FUNCTION 'YTEST_TABLE_CNG_WRITE_DOCUMENT'
EXPORTING
objectid = 'YTEST_TABLE_CNG'
tcode = sy-tcode
utime = sy-uzeit
udate = sy-datum
username = sy-uname
planned_change_number = ' '
object_change_indicator = 'U'
planned_or_real_changes = 'U'
no_change_pointers = 'U'
upd_icdtxt_ytest_table_cng = 'U'
n_ytest_table = gs_test
o_ytest_table = ls_test
upd_ytest_table = 'U'
lv_opt = ' '
TABLES
icdtxt_ytest_table_cng = gt_change.