Sometimes clients need to log any changes made by user on customer created screen and Z table in MM01/MM02.

You can do it by activate user-exit at save (for example):

at user-exit insert your code:

            CALL FUNCTION ‘ZMM_ZMMLABEL_CREATE_LOG’
            EXPORTING
              i_matnr     = rmmg1matnr
              i_werks     = rmmg1werks
              i_change_id = (‘I’ for insert new record, ‘D’ when you delete record, ‘U’ for update)

              i_zmmlabel  = l_zmmlabel.

and you must create FM:

   FUNCTION zmm_zmmlabel_create_log .
*”———————————————————————-
*”*”Update Function Module:
*”
*”*”Local Interface:
*”  IMPORTING
*”     VALUE(I_MATNR) LIKE  RMMG1-MATNR
*”     VALUE(I_WERKS) TYPE  RMMG1-WERKS
*”     VALUE(I_CHANGE_ID) TYPE  CDHDR-CHANGE_IND
*”     VALUE(I_ZMMLABEL) TYPE  ZMMLABEL
*”———————————————————————-
  DATA: lt_zmmlabel   LIKE TABLE OF zmmlabel,
        zmmlabel_old  LIKE zmmlabel,
        objectid      LIKE cdhdrobjectid,
        objectclass   LIKE cdhdrobjectclas,
        tablename     LIKE cdpostabname,
        change_id     LIKE cdhdrchange_ind.

*  MOVE-CORRESPONDING i_zmmlabel TO zmmlabel.

  tablename   = ‘ZMMLABEL’.
  objectid    = i_matnr.
  objectclass = ‘MATERIAL’.

* Manage change document
  IF i_change_id = ‘D’.
    CLEAR i_zmmlabel.

    SELECT * FROM zmmlabel INTO TABLE lt_zmmlabel
     WHERE matnr   = i_matnr
       AND werks   = i_werks.

    IF sysubrc = 0.
      LOOP AT lt_zmmlabel INTO zmmlabel_old.

        CALL FUNCTION ‘YMM_CREATE_CHANGE_DOCUMENT’
          EXPORTING
            objectclass      = objectclass
            objectid         = objectid
            tablename        = tablename
            workarea_new     = i_zmmlabel
            workarea_old     = zmmlabel_old
*            change_indicator = i_change_id
            change_indicator = ‘U’. ” if you need log all changes, please use U mode
        COMMIT WORK.

        CLEAR zmmlabel_old.
      ENDLOOP.

      DELETE zmmlabel FROM TABLE lt_zmmlabel.

      COMMIT WORK.
*      COMMIT WORK AND WAIT.
    ENDIF.

  ELSEIF i_change_id = ‘U’.

    SELECT SINGLE * FROM zmmlabel INTO zmmlabel_old
     WHERE matnr   = i_matnr
       AND werks   = i_werks
       AND counter = zmmlabelcounter.

    CALL FUNCTION ‘YMM_CREATE_CHANGE_DOCUMENT’
      EXPORTING
        objectclass      = objectclass
        objectid         = objectid
        tablename        = tablename
        workarea_new     = i_zmmlabel
        workarea_old     = zmmlabel_old
        change_indicator = i_change_id.

    COMMIT WORK.

    MODIFY zmmlabel FROM i_zmmlabel.

    COMMIT WORK.

*    COMMIT WORK AND WAIT.

  ELSEIF i_change_id = ‘I’.
    CLEAR zmmlabel_old.

    CALL FUNCTION ‘YMM_CREATE_CHANGE_DOCUMENT’
      EXPORTING
        objectclass      = objectclass
        objectid         = objectid
        tablename        = tablename
        workarea_new     = i_zmmlabel
        workarea_old     = zmmlabel_old
*            change_indicator = i_change_id
        change_indicator = ‘U’. ” if you need log all changes, please use U mode

    COMMIT WORK.

    MODIFY zmmlabel FROM i_zmmlabel.

    COMMIT WORK.

*    COMMIT WORK AND WAIT.

  ENDIF.
ENDFUNCTION.

   FUNCTION YMM_CREATE_CHANGE_DOCUMENT.
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     VALUE(OBJECTCLASS) LIKE  CDHDR-OBJECTCLAS
*”     VALUE(OBJECTID) LIKE  CDHDR-OBJECTID
*”     VALUE(TABLENAME) LIKE  CDPOS-TABNAME
*”     VALUE(WORKAREA_NEW) DEFAULT SPACE
*”     VALUE(WORKAREA_OLD) DEFAULT SPACE
*”     VALUE(CHANGE_INDICATOR) DEFAULT ‘U’
*”———————————————————————-

  call function ‘CHANGEDOCUMENT_OPEN’
       exporting
            objectclass      = objectclass
            objectid         = objectid
       exceptions
            sequence_invalid = 1
            others           = 2.
  call function ‘CHANGEDOCUMENT_SINGLE_CASE’
       exporting
            tablename              = tablename
            workarea_old           = workarea_old
            workarea_new           = workarea_new
            change_indicator       = change_indicator
       exceptions
            nametab_error          = 1
            open_missing           = 2
            position_insert_failed = 3
            others                 = 4.
  case sysubrc.
    when 1. message a850(m3) with ‘NAMETAB-ERROR’.
    when 2. message a850(m3) with ‘OPEN MISSING’.
    when 3. message a850(m3) with ‘INSERT ERROR’.
    when 4. message a850(m3) with ‘SINGLE ERROR’.
    when others.
  endcase.
*
  udate = sydatum.
  utime = syuzeit.
  tcode = sytcode.
  username = syuname.
*
  call function ‘CHANGEDOCUMENT_CLOSE’
       exporting
            objectclass             = objectclass
            objectid                = objectid
            date_of_change          = udate
            time_of_change          = utime
            tcode                   = tcode
            username                = username
            object_change_indicator = change_indicator
       exceptions
            header_insert_failed    = 1
            object_invalid          = 2
            open_missing            = 3
            no_position_inserted    = 4
            others                  = 5.

  case sysubrc.
    when 1. message a850(m3) with ‘INSERT HEADER FAILED’.
    when 2. message a850(m3) with ‘OBJECT INVALID’.
    when 3. message a850(m3) with ‘OPEN MISSING’.
    when 5. message a850(m3) with ‘CLOSE ERROR’.
    when others.
  endcase.

ENDFUNCTION.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply