For different aims (+to launch an alert, to
track modification, to
follow-up documents, to retrieve parameters old values after mistakes+
). You sometimes
need to look for the document history.
What I mean when I say
document
? This is the SAP Terminology which can mean in our context, the material master, the purchase order, the material routing, the GL account, the asset, the workcenter ...etc So both, Master data & Transaction data changes, can be log if your system is customize.
Concept
Basically, there are 2 main tables (not enough
well known) which can be
accessed through ABAP or Function Module.
So nothing technically pointed in this blog,
only just very useful!
Following the classical SAP table structure,
you’ll find
CDHDR
(Change
document header) and
CDPOS
(Change document items). ( HR Module: PCDHDR and PCDPOS )
They can be read using a classical ‘ style="">Select’
statement or using respectively the following FM
'CHANGEDOCUMENT_READ_HEADERS'
'CHANGEDOCUMENT_READ_POSITIONS'
As those tables log the SAP document changes,
we can imagine how much large they
are, especially CDPOS ! And unfortunately, as this one is a
“cluster”
table, no “Join” query is possible.
And this is the main inconvenient;
you’ll have to be very careful in
building your query, filling the key field the better you can. ( Tips :
Use the
“For All entries” Statement )
Now in order to illustrate, we’ll see
through 2 different examples, how
being aware of the utility of these tables can be interesting and useful.
Sample Code
.
SELECT matnr
INTO TABLE t_mara
FROM mara WHERE matnr IN material.
clear t_mara.
LOOP AT t_mara.
PERFORM changedocument_read.
ENDLOOP.
LOOP AT itab.
CLEAR: wtext, w_matnr.
CASE itab-chngind.
WHEN 'U'.
wtext(50) = 'Updated field'.
wtext+51 = itab-ftext.
CONDENSE wtext.
WHEN 'D'.
wtext = text-021.
WHEN 'E'.
wtext(5) = itab-ftext.
wtext+51 = ' text : E'.
CONDENSE wtext.
WHEN 'I'.
wtext = ' text : I'.
ENDCASE.
RESERVE 5 LINES.
IF wchangenr NE itab-changenr.
wchangenr = itab-changenr.
w_matnr = itab-matnr.
WRITE:/ itab-udate UNDER 'Change Date',
itab-utime UNDER 'Time',
itab-username UNDER 'User Name',
itab-matnr UNDER 'Material No',
wtext UNDER 'Changes'.
ELSEIF itab-matnr NE w_matnr.
WRITE:/ wtext UNDER 'Changes'.
ENDIF.
CASE itab-chngind.
WHEN 'U'.
ASSIGN itab-f_old(itab-outlen) TO .
ENDCASE.
SKIP.
ENDLOOP.
TOP-OF-PAGE.
WRITE: / sy-repid,
50 'Material Changes'.
Skip to line 4.
ULINE.
WRITE:/01 'Change Date',
15 'Time',
30 'User Name',
45 'Material No',
70 'Changes'.
ULINE.
&----
*& Form CHANGEDOCUMENT_READ
&----
----
FORM changedocument_read.
CLEAR cdhdr.
CLEAR cdpos.
cdhdr-objectclas = 'MATERIAL'.
cdhdr-objectid = t_mara-matnr.
CALL FUNCTION 'CHANGEDOCUMENT_READ_HEADERS'
EXPORTING
date_of_change = cdhdr-udate
objectclass = cdhdr-objectclas
objectid = cdhdr-objectid
time_of_change = cdhdr-utime
username = cdhdr-username
TABLES
i_cdhdr = icdhdr
EXCEPTIONS
no_position_found = 1
OTHERS = 2.
CHECK sy-subrc EQ 0.
DELETE icdhdr WHERE change_ind EQ 'I'.
CHECK NOT icdhdr[] IS INITIAL.
LOOP AT icdhdr.
CHECK icdhdr-udate IN period.
CHECK icdhdr-username IN name.
CALL FUNCTION 'CHANGEDOCUMENT_READ_POSITIONS'
EXPORTING
changenumber = icdhdr-changenr
IMPORTING
header = cdhdr
TABLES
editpos = icdshw
EXCEPTIONS
no_position_found = 1
OTHERS = 2.
CHECK sy-subrc EQ 0.
LOOP AT icdshw.
CHECK icdshw-text_case EQ space.
MOVE-CORRESPONDING icdshw TO itab.
MOVE-CORRESPONDING icdhdr TO itab.
MOVE icdshw-tabkey+3 TO itab-matnr.
APPEND itab.
ENDLOOP.
ENDLOOP.
ENDFORM. "CHANGEDOCUMENT_READ
</pre>
Final Words
This concept of change documents log is applicable
to different type of SAP documents. But it depends on your SAP customization.
At a technical level, we can notice that only changes to fields whose data elements are marked as relevant for the creation of change documents in SE11 lead to the creation of change documents. Modifications logging only takes place if the fields whose contents were changed refer to a data element that was flagged as relevant for the change document.
For those who want to go further on this topic, I invite them to check out function group SCD0, SCD1, SCD2 ( contains all the document handling FM's ). This means you can also use this functionality in your own application...