ABAP code to get change document infos

ABAP code to get change document infos

As it is not possible to do joins with cluster table, this is the code to get change documents data merged between tables CDHDR and CDPOS.

The goal is to get an internal table like :


  TYPES : BEGIN OF ts_cdhdr,
            objectclas TYPE cdhdr-objectclas,
            objectid   TYPE cdhdr-objectid,
            changenr   TYPE cdhdr-changenr,
            username   TYPE cdhdr-username,
            udate      TYPE cdhdr-udate,
            utime      TYPE cdhdr-utime,
            tcode      TYPE cdhdr-tcode,
          END OF ts_cdhdr.

  TYPES : BEGIN OF ts_cdpos,
            objectclas TYPE cdpos-objectclas,
            objectid   TYPE cdpos-objectid,
            changenr   TYPE cdpos-changenr,
            tabname    TYPE cdpos-tabname,
            tabkey     TYPE cdpos-tabkey,
            fname      TYPE cdpos-fname,
            chngind    TYPE cdpos-chngind,
            value_new  TYPE cdpos-value_new,
            value_old  TYPE cdpos-value_old,
          END OF ts_cdpos.

  TYPES : BEGIN OF ts_chgdoc,
            objectid  TYPE cdpos-objectid,
            udate     TYPE cdhdr-udate,
            utime     TYPE cdhdr-utime,
            tabname   TYPE cdpos-tabname,
            tabkey    TYPE cdpos-tabkey,
            fname     TYPE cdpos-fname,
            chngind   TYPE cdpos-chngind,
            value_new TYPE cdpos-value_new,
            value_old TYPE cdpos-value_old,
            tcode     TYPE cdhdr-tcode,
          END OF ts_chgdoc.

  DATA : lt_cdhdr    TYPE STANDARD TABLE OF ts_cdhdr,
         ls_cdhdr    TYPE ts_cdhdr,
         lt_cdpos    TYPE STANDARD TABLE OF ts_cdpos,
         ls_cdpos    TYPE ts_cdpos,
         lt_objectid TYPE STANDARD TABLE OF cdobjectv,
         lt_chgdoc   TYPE STANDARD TABLE OF ts_chgdoc,
         ls_chgdoc   TYPE ts_chgdoc.

  DATA : lt_cdhdr    TYPE STANDARD TABLE OF ts_cdhdr,
         ls_cdhdr    TYPE ts_cdhdr,
         lt_cdpos    TYPE STANDARD TABLE OF ts_cdpos,
         ls_cdpos    TYPE ts_cdpos,
         lt_objectid TYPE STANDARD TABLE OF cdobjectv,
         lt_chgdoc   TYPE STANDARD TABLE OF ts_chgdoc,
         ls_chgdoc   TYPE ts_chgdoc.

  " Convert condition number to OBJECT ID format
  LOOP AT gt_price_cond_at_date INTO ls_price_cond.
    APPEND ls_price_cond-knumh TO lt_objectid.
  ENDLOOP.

  SORT lt_objectid.
  DELETE ADJACENT DUPLICATES FROM lt_objectid COMPARING ALL FIELDS.


  " Retrieve data from CDHDR
  SELECT objectclas
         objectid
         changenr
         username
         udate
         utime
         tcode
    FROM cdhdr
    INTO TABLE lt_cdhdr
    FOR ALL ENTRIES IN lt_objectid
    WHERE objectclas = 'COND_A'
      AND objectid   = lt_objectid-table_line.


  IF sy-subrc = 0.

    " For binary search
    SORT lt_cdhdr BY objectclas
                     objectid
                     changenr.

    " Retrieve data from CDPOS
    SELECT objectclas
           objectid
           changenr
           tabname
           tabkey
           fname
           chngind
           value_new
           value_old
      FROM cdpos
      INTO TABLE lt_cdpos
      FOR ALL ENTRIES IN lt_cdhdr
      WHERE objectclas = 'COND_A'
        AND objectid   = lt_cdhdr-objectid.


    " Build change document table :
    LOOP AT lt_cdpos INTO ls_cdpos.

      READ TABLE lt_cdhdr INTO ls_cdhdr WITH KEY objectclas = ls_cdpos-objectclas
                                                 objectid   = ls_cdpos-objectid
                                                 changenr   = ls_cdpos-changenr
                                                 BINARY SEARCH.
      IF sy-subrc = 0.
        ls_chgdoc-objectid    = ls_cdpos-objectid .
        ls_chgdoc-udate       = ls_cdhdr-udate    .
        ls_chgdoc-utime       = ls_cdhdr-utime    .
        ls_chgdoc-tabname     = ls_cdpos-tabname  .
        ls_chgdoc-tabkey      = ls_cdpos-tabkey   .
        ls_chgdoc-fname       = ls_cdpos-fname    .
        ls_chgdoc-chngind     = ls_cdpos-chngind  .
        ls_chgdoc-value_new   = ls_cdpos-value_new.
        ls_chgdoc-value_old   = ls_cdpos-value_old.
        ls_chgdoc-tcode       = ls_cdhdr-tcode    .

        APPEND ls_chgdoc TO lt_chgdoc.
      ENDIF.

    ENDLOOP.

  ENDIF.

About the author

fjourneau administrator

Hi, I'm Florian Journeau, SAP ABAP R3 Freelance, based in Toulouse, France. You want to know more about me, have a look on my CV : cv.fjourneau.net.

Leave a Reply