Page restrictions apply Attachments:30 Added by Guest, last edited by Craig Cmehil on Dec 14, 2009 (view change) show comment Go to start of metadata 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 Illustration Pre-requisite Step by step explanation on creation of change document object Using change document object's Function Module Update Functionality: Insert functionality: Delete Functionality: Reading Change logs Report Output: Related Content 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 - 1. Change document object 2. 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
01.\* fetch all the details from the Ztable. 02.SELECT SINGLE * FROM YTEST_TABLE 03.INTO GS_TEST 04.WHERE ZEMP_NO = P_EMP_NO. 05.IF SY-SUBRC = 0. 06.\*move the old entries 07.CLEAR LS_TEST. 08.LS_TEST = GS_TEST. 09.\*New Values 10.GS_TEST-ZEMP_NO = P_EMP_NO 11.GS_TEST-ZENAME = 'new_name1' 12.GS_TEST-ZMAIL_ID = 'new_name1@mail.com' 13.GS_TEST-ZCONTACT_NO = "02211336655" 14.APPEND GS_TEST TO GT_TEST. 15.\*Populate change tables 16.CLEAR GS_CHANGE. 17.GS_CHANGE-TEILOBJID = 'YTEST_TABLE_CNG'. 18.GS_CHANGE-TEXTART = 'TEST_2'. 19.GS_CHANGE-TEXTSPR = 'EN'. 20.GS_CHANGE-UPDKZ = 'U'. 21.APPEND GS_CHANGE TO GT_CHANGE. 22.\*Call the FM to log the values in CDHDR table. 23.CALL FUNCTION 'YTEST_TABLE_CNG_WRITE_DOCUMENT' 24.EXPORTING 25.OBJECTID = 'YTEST_TABLE_CNG' 26.TCODE = SY-TCODE 27.UTIME = SY-UZEIT 28.UDATE = SY-DATUM 29.USERNAME = SY-UNAME 30.PLANNED_CHANGE_NUMBER = ' ' 31.OBJECT_CHANGE_INDICATOR = 'U' 32.PLANNED_OR_REAL_CHANGES = 'U' 33.NO_CHANGE_POINTERS = 'U' 34.UPD_ICDTXT_YTEST_TABLE_CNG = 'U' 35.N_YTEST_TABLE = GS_TEST 36.O_YTEST_TABLE = LS_TEST 37.UPD_YTEST_TABLE = 'U' 38.LV_OPT = ' ' 39.TABLES 40.ICDTXT_YTEST_TABLE_CNG = GT_CHANGE. After execution the CDHDR table is logged with one entry for our change object
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
01.CLEAR: GS_TEST, 02.LS_TEST. 03.GS_TEST-ZEMP_NO = P_EMP_NO. 04.GS_TEST-ZENAME = 'name_ins'. 05.GS_TEST-ZMAIL_ID = 'name_ins@mail.com'. 06.GS_TEST-ZCONTACT_NO = '1112131415'. 07.APPEND GS_TEST TO GT_TEST. 08. 09.*populate change table 10.CLEAR GS_CHANGE. 11.GS_CHANGE-TEILOBJID = 'YTEST_TABLE_CNG'. 12.GS_CHANGE-TEXTART = 'TEST_1'. 13.GS_CHANGE-TEXTSPR = 'EN'. 14.GS_CHANGE-UPDKZ = 'I'. 15.APPEND GS_CHANGE TO GT_CHANGE. 16. 17.*call the fM to log the values in CDHDR table. 18.CALL FUNCTION 'YTEST_TABLE_CNG_WRITE_DOCUMENT' 19.EXPORTING 20.OBJECTID = 'YTEST_TABLE_CNG' 21.TCODE = SY-TCODE 22.UTIME = SY-UZEIT 23.UDATE = SY-DATUM 24.USERNAME = SY-UNAME 25.PLANNED_CHANGE_NUMBER = ' ' 26.OBJECT_CHANGE_INDICATOR = 'I' 27.PLANNED_OR_REAL_CHANGES = 'I' 28.NO_CHANGE_POINTERS = 'I' 29.UPD_ICDTXT_YTEST_TABLE_CNG = 'I' 30.N_YTEST_TABLE = GS_TEST 31.O_YTEST_TABLE = LS_TEST 32.UPD_YTEST_TABLE = 'I' 33.LV_OPT = ' ' 34.TABLES 35.ICDTXT_YTEST_TABLE_CNG = GT_CHANGE. The details logged in CDHDR and CDPOS are as below.
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
01.* fetch all the details from the ztable. 02.SELECT SINGLE * FROM YTEST_TABLE 03.INTO GS_TEST 04.WHERE ZEMP_NO = P_EMP_NO. 05. 06.*move the old entries 07.IF SY-SUBRC = 0. 08.CLEAR LS_TEST. 09.LS_TEST = GS_TEST. 10.CLEAR GS_TEST. 11. 12.*populate change table 13.CLEAR GS_CHANGE. 14.GS_CHANGE-TEILOBJID = 'YTEST_TABLE_CNG'. 15.GS_CHANGE-TEXTART = 'TEST_3'. 16.GS_CHANGE-TEXTSPR = 'EN'. 17.GS_CHANGE-UPDKZ = 'D'. 18.APPEND GS_CHANGE TO GT_CHANGE. 19. 20.*call the fM to log the values in CDHDR table. 21.CALL FUNCTION 'YTEST_TABLE_CNG_WRITE_DOCUMENT' 22.EXPORTING 23.OBJECTID = 'YTEST_TABLE_CNG' 24.TCODE = SY-TCODE 25.UTIME = SY-UZEIT 26.UDATE = SY-DATUM 27.USERNAME = SY-UNAME 28.PLANNED_CHANGE_NUMBER = ' ' 29.OBJECT_CHANGE_INDICATOR = 'D' 30.PLANNED_OR_REAL_CHANGES = 'D' 31.NO_CHANGE_POINTERS = 'D' 32.UPD_ICDTXT_YTEST_TABLE_CNG = 'D' 33.N_YTEST_TABLE = GS_TEST 34.O_YTEST_TABLE = LS_TEST 35.UPD_YTEST_TABLE = 'D' 36.LV_OPT = 'X' 37.TABLES 38.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 01.DATA: LS_CDRED LIKE LINE OF GT_CDRED. 02. 03.CALL FUNCTION 'CHANGEDOCUMENT_READ_HEADERS' 04.EXPORTING 05.ARCHIVE_HANDLE = 0 06.DATE_OF_CHANGE = '00000000' 07.OBJECTCLASS = 'YTEST_TABLE_CNG' 08.OBJECTID = ' ' 09.TIME_OF_CHANGE = '000000' 10.USERNAME = SY-UNAME 11.LOCAL_TIME = ' ' 12.DATE_UNTIL = '99991231' 13.TIME_UNTIL = '235959' 14.NOPLUS_ASWILDCARD_INOBJID = ' ' 15.TABLES 16.I_CDHDR = GT_CDHDR 17.EXCEPTIONS 18.NO_POSITION_FOUND = 1 19.WRONG_ACCESS_TO_ARCHIVE = 2 20.TIME_ZONE_CONVERSION_ERROR = 3 21.OTHERS = 4. 22.IF SY-SUBRC <> 0. 23.MESSAGE 'No entries found' TYPE 'E'. 24.ENDIF. 25. 26.IF NOT GT_CDHDR IS INITIAL. 27. 28.LOOP AT GT_CDHDR INTO GS_CDHDR. 29. 30.CALL FUNCTION 'CHANGEDOCUMENT_READ_POSITIONS' 31.EXPORTING 32.CHANGENUMBER = GS_CDHDR-CHANGENR 33.TABLES 34.EDITPOS = GT_CDSHW 35.EDITPOS_WITH_HEADER = GT_CDRED 36.EXCEPTIONS 37.NO_POSITION_FOUND = 1 38.WRONG_ACCESS_TO_ARCHIVE = 2 39.OTHERS = 3. 40.IF SY-SUBRC <> 0. 41.MESSAGE 'No item details found' TYPE 'E'. 42.ELSE. 43.LOOP AT GT_CDRED INTO LS_CDRED. 44.MOVE-CORRESPONDING GS_CDHDR TO GS_OUT. 45.MOVE-CORRESPONDING LS_CDRED TO GS_OUT. 46.APPEND GS_OUT TO GT_OUT. 47.CLEAR: GS_OUT, 48.LS_CDRED. 49.ENDLOOP. 50.CLEAR: GS_CDHDR. 51.ENDIF. 52.ENDLOOP. 53.ENDIF. Report Output: