ABAP – Change Documents example with Z-table

SAPIn this example I’ll follow on the theory of Change Documents presented in previous article (ABAP – Change Documents (theory)) and I’ll show how to create(or update an existing) Z-table where each modification will be logged into CDHRD and CDPOS tables

Z-Table modifications

In our example we will create a completely new Z-table

The table consists of three fields:

  • EMPID (Employee id)
  • ENAME (Employee name)
  • ELOC (Employee Location)

We will also create new Z-data element for each of the fields above and in the ‘Further characteristics’ tab of each data element you want to have the changes being logged in CDHDR and CDPOS select the check box ‘Change document’.

Change Document creation

Run transaction SCDO.

Click on the ‘Create’ button, enter the name of the change document object and click on the ‘Continue’ button

On the screen that appears enter some description into the ‘Text’ field and name of the Z-table.

Save the changes, go back to list of all available Change Documents and find your new Change Document. Select it by clicking on its name and press ‘Generate update pgm’ button

On the screen that is displayed enter name of the function group that will contain function modules to handle the CDHDR and CDPOS manipulation for your Change Document (for your Z-table) and press ‘Generate’ button

The screen that appears contains information about your new function module(name will be like <Name of your Change Document>_WRITE_DOCUMENT) and other related info.

Click on the ‘Save’ button and from now on you can log your changes using the new function module in CDHDR and CDPOS tables.

Coding part

Let’s check what are the import parameters of your new function module:

… and tables

In the list you can see this function module has 2 parameters named

  • N_<your_Z_table> … in our case N_ZCA_EMPLOYEE
  • O_<your_Z_table> … in our case O_ZCA_ZEMPLOYEE

Where

  • N stands for new data
  • O stands for old data.

Important note

  • Log INSERT Z-table entries
    • Pass data to the new parameter only (N_ZCA_EMPLOYEE).
    • The old parameter (O_ZCA_EMPLOYEE) is ignored.
  • Log UPDATE Z-table entries
    • Pass the new data to the new parameter (N_ZCA_EMPLOYEE)
    • Pass the old data to the old parameter. (O_ZCA_EMPLOYEE)
  • Log DELETE Z-table entries
    • Pass the data to the old parameter (O_ZCA_EMPLOYEE)

Log INSERT table entry

DATA:
     ls_emp_new TYPE zca_employee,
     ls_emp_old TYPE zca_employee,
     lt_cdtxt TYPE TABLE OF cdtxt,
     lv_objectid TYPE cdhdr-objectid.

ls_emp_new-empid = '1'.
ls_emp_new-ename = 'John Malkovich'.
ls_emp_new-eloc  = 'Paris'.

INSERT zca_employee FROM ls_emp_new.

lv_objectid = ls_emp_new-empid.

CALL FUNCTION 'Z_EMPLOYEE_WRITE_DOCUMENT'
  EXPORTING
    objectid          = lv_objectid
    tcode             = sy-tcode
    utime             = sy-uzeit
    udate             = sy-datum
    username          = sy-uname
    n_zca_employee    = ls_emp_new
    o_zca_employee    = ls_emp_old
    upd_zca_employee  = 'I'
  TABLES
    icdtxt_z_employee = lt_cdtxt.

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

After execution of the piece of code above the ZCA_EMPLOYEE table will contain the following data

Execute Report RSSCD200 for your Z-table (ZCA_EMPLOYEE) and you should see the following

Log UPDATE table entry

ls_emp_new-empid = '1'.
ls_emp_new-ename = 'Johny Depp'.
ls_emp_new-eloc  = 'Los Angeles'.

MODIFY zca_employee FROM ls_emp_new.

ls_emp_old-empid = '1'.
ls_emp_old-ename = 'John Malkovich'.
ls_emp_old-eloc  = 'Paris'.

lv_objectid = ls_emp_new-empid.

CALL FUNCTION 'Z_EMPLOYEE_WRITE_DOCUMENT'
  EXPORTING
    objectid          = lv_objectid
    tcode             = sy-tcode
    utime             = sy-uzeit
    udate             = sy-datum
    username          = sy-uname
    n_zca_employee    = ls_emp_new
    o_zca_employee    = ls_emp_old
    upd_zca_employee  = 'U'
  TABLES
    icdtxt_z_employee = lt_cdtxt.

Note: In case of Update pass the value of the parameter UPD_ZCA_EMPLOYEE = ‘U’.

After execution of the piece of code above the ZCA_EMPLOYEE table will contain the following data

Execute Report RSSCD200 for your Z-table (ZCA_EMPLOYEE) and you should see the following

Log DELETE table entry

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

lv_objectid = ls_emp_old-empid.

CALL FUNCTION 'Z_EMPLOYEE_WRITE_DOCUMENT'
  EXPORTING
    objectid          = lv_objectid
    tcode             = sy-tcode
    utime             = sy-uzeit
    udate             = sy-datum
    username          = sy-uname
    n_zca_employee    = ls_emp_new
    o_zca_employee    = ls_emp_old
    upd_zca_employee  = 'D'
  TABLES
    icdtxt_z_employee = lt_cdtxt.

Note: In case of Deletion pass the value of the parameter UPD_ZCA_EMPLOYEE = ‘D’.

After execution of the piece of code above the ZCA_EMPLOYEE table will contain the following data

Execute Report RSSCD200 for your Z-table (ZCA_EMPLOYEE) and you should see the following

Table Maintenance events

The log changes can be automatically written by your table maintenance view if you implement the above code in table maintenance events: row creation/update/deletion.

If you are not sure how to do it please visit my tutorial on how to maintain events of table maintenance view

2 thoughts on “ABAP – Change Documents example with Z-table

  1. great post, now i want do this in a maintenance view and you say if possible but i don’t know which even are the correct one for implement this code, can you help me there ?

  2. Hi Edgar,
    according to list of available events I guess you can achieve this using the following events:
    02 – After saving the data in database
    04 – After deleting the data displayed
    05 – Creating new entry
    08 – After correcting the contents of a selected field.

Leave a Reply