Skip to Content

Good day!

This is a very quick post. I could not find an example when I needed so I decided to write this quick post. The FORM inside the report returns the last change of a characteristics value from material batch.

Tcode:MSC2N.

In case you need to add more details you have to adapt the code analyzing SE38: LCHRGF05, FORM: init_class.

REPORT  ZCDHDR_AUSP_MCH1.

*&---------------------------------------------------------------------*
*& AUTHOR: IATCO SERGIU
*& DATE: 05.05.2017
*&---------------------------------------------------------------------*

REPORT  ZCDHDR_AUSP_MCH1.
TABLES: AUSP, INOB, CABN, MCH1, CDPOS, CDHDR.


DATA: &UDATE LIKE CDHDR-UDATE.
DATA: &UTIME LIKE CDHDR-UTIME.

PARAMETERS  P_CHR LIKE CABN-ATNAM.
PARAMETERS: P_MAT LIKE MCH1-MATNR.
PARAMETERS: P_CHA LIKE MCH1-CHARG.

START-OF-SELECTION.

PERFORM F_CHANGENR_LAST USING
                              P_CHR
                              P_MAT
                              P_CHA
                             &UDATE
                             &UTIME.

 WRITE: / &UDATE, &UTIME.

FORM F_CHANGENR_LAST USING P_CHR LIKE CABN-ATNAM
                           P_MAT LIKE MCH1-MATNR
                           P_CHA LIKE MCH1-CHARG
                     CHANGING
                           _UDATE TYPE UDATE
                           _UTIME TYPE UTIME.
DATA: _AUSP TYPE AUSP.
DATA: _CDPOS TYPE CDPOS.
DATA: _CHR LIKE CABN-ATNAM.
DATA: _MATNR_CHARG LIKE INOB-OBJEK.
CONCATENATE P_MAT P_CHA INTO _MATNR_CHARG.

SELECT SINGLE * FROM MCH1 WHERE MATNR = P_MAT AND
                                CHARG = P_CHA.

CHECK SY-SUBRC EQ 0.

SELECT * INTO CORRESPONDING FIELDS OF _AUSP
  FROM INOB
  JOIN AUSP ON INOB~CUOBJ = AUSP~OBJEK
  JOIN CABN ON CABN~ATINN = AUSP~ATINN
  WHERE
  CABN~ATNAM = P_CHR AND
  INOB~OBJEK = _MATNR_CHARG AND
  CABN~ATEIN = 'X'.
ENDSELECT.

CONCATENATE _AUSP-OBJEK _AUSP-MAFID INTO _CDPOS-OBJECTID.
CONCATENATE _AUSP-OBJEK _AUSP-ATINN '%' INTO _CDPOS-TABKEY.

_CDPOS-OBJECTCLAS = 'CLASSIFY'.
_CDPOS-TABNAME = 'ABAUSP'.

DATA: L_CDPOS LIKE CDPOS.
DATA: I_CDPOS LIKE STANDARD TABLE OF L_CDPOS.

SELECT * FROM CDPOS INTO CORRESPONDING FIELDS OF TABLE I_CDPOS
*         ORDER BY CHANGNR DESCENDING " NOT ALLOWED FOR POOLED AND CLUSTER
         WHERE
            OBJECTCLAS = _CDPOS-OBJECTCLAS AND
            OBJECTID = _CDPOS-OBJECTID AND
            TABNAME = _CDPOS-TABNAME AND
            TABKEY LIKE _CDPOS-TABKEY.
*            TABKEY GE _CDPOS-TABKEY.

SORT I_CDPOS BY OBJECTCLAS OBJECTID CHANGENR DESCENDING TABNAME TABKEY ASCENDING.

FIELD-SYMBOLS: <FR> LIKE L_CDPOS.
CHECK I_CDPOS[] IS NOT INITIAL.

 READ TABLE I_CDPOS INDEX 1 ASSIGNING <FR>.
 WRITE: / <FR>-CHANGENR.

DATA: L_CDHDR LIKE CDHDR.
SELECT SINGLE * FROM CDHDR INTO L_CDHDR
                   WHERE
                     OBJECTCLAS = <FR>-OBJECTCLAS AND
                     OBJECTID =   <FR>-OBJECTID AND
                     CHANGENR =   <FR>-CHANGENR.

_UDATE = L_CDHDR-UDATE.
_UTIME = L_CDHDR-UTIME.

ENDFORM.

Same report using ABAP classes.

REPORT ZCDHDR_AUSP_MCH1_OO.

*&---------------------------------------------------------------------*
*& AUTHOR: IATCO SERGIU
*& DATE: 05.05.2017
*&---------------------------------------------------------------------*

REPORT  ZCDHDR_AUSP_MCH1_OO.

DATA: &UDATE LIKE CDHDR-UDATE.
DATA: &UTIME LIKE CDHDR-UTIME.

PARAMETERS  P_CHR LIKE CABN-ATNAM.
PARAMETERS: P_MAT LIKE MCH1-MATNR.
PARAMETERS: P_CHA LIKE MCH1-CHARG.

CLASS CDHDR_AUSP DEFINITION.
  PUBLIC SECTION.
    METHODS: LAST_CHANGE
     IMPORTING
        I_CHR TYPE ANY
        I_MAT TYPE ANY
        I_CHA TYPE ANY
       EXPORTING
        E_UDATE TYPE ANY
        E_UTIME TYPE ANY.
ENDCLASS.

CLASS CDHDR_AUSP IMPLEMENTATION.
  METHOD LAST_CHANGE.
    DATA: _AUSP  TYPE AUSP.
    DATA: _CDPOS TYPE CDPOS.
    DATA: _CHR   TYPE  ATNAM.

    DATA: _MATNR_CHARG TYPE CUOBN.
    DATA: WA_MCH1 TYPE MCH1.

    CONCATENATE I_MAT I_CHA INTO _MATNR_CHARG.

    SELECT SINGLE * FROM MCH1
                    INTO WA_MCH1
                    WHERE MATNR = P_MAT AND
                          CHARG = P_CHA.

    CHECK SY-SUBRC EQ 0.

    SELECT * INTO CORRESPONDING FIELDS OF _AUSP
      FROM INOB
      JOIN AUSP ON INOB~CUOBJ = AUSP~OBJEK
      JOIN CABN ON CABN~ATINN = AUSP~ATINN
      WHERE
      CABN~ATNAM = I_CHR AND
      INOB~OBJEK = _MATNR_CHARG AND
      CABN~ATEIN = 'X'.
    ENDSELECT.

    CONCATENATE _AUSP-OBJEK _AUSP-MAFID INTO _CDPOS-OBJECTID.
    CONCATENATE _AUSP-OBJEK _AUSP-ATINN '%' INTO _CDPOS-TABKEY.

    _CDPOS-OBJECTCLAS = 'CLASSIFY'.
    _CDPOS-TABNAME = 'ABAUSP'.

    DATA: I_CDPOS TYPE STANDARD TABLE OF CDPOS.

    SELECT * FROM CDPOS INTO CORRESPONDING FIELDS OF TABLE I_CDPOS
*             ORDER BY CHANGNR DESCENDING " NOT ALLOWED FOR POOLED AND CLUSTER
             WHERE
                OBJECTCLAS = _CDPOS-OBJECTCLAS AND
                OBJECTID =   _CDPOS-OBJECTID   AND
                TABNAME =    _CDPOS-TABNAME    AND
                TABKEY LIKE _CDPOS-TABKEY.
*                TABKEY GE _CDPOS-TABKEY.

    SORT I_CDPOS BY OBJECTCLAS OBJECTID CHANGENR DESCENDING TABNAME TABKEY ASCENDING.

    FIELD-SYMBOLS: <FR> TYPE CDPOS.
    CHECK I_CDPOS[] IS NOT INITIAL.

    READ TABLE I_CDPOS INDEX 1 ASSIGNING <FR>.
*     WRITE: / <FR>-CHANGENR.

    DATA: WA_CDHDR TYPE CDHDR.

    SELECT SINGLE * FROM CDHDR INTO WA_CDHDR
                       WHERE
                         OBJECTCLAS = <FR>-OBJECTCLAS AND
                         OBJECTID =   <FR>-OBJECTID   AND
                         CHANGENR =   <FR>-CHANGENR.

    E_UDATE = WA_CDHDR-UDATE.
    E_UTIME = WA_CDHDR-UTIME.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA O_CDHDR_AUSP TYPE REF TO CDHDR_AUSP.
CREATE OBJECT: O_CDHDR_AUSP.

  CALL METHOD O_CDHDR_AUSP->LAST_CHANGE
    EXPORTING
        I_CHR = P_CHR
        I_MAT = P_MAT
        I_CHA = P_CHA
    IMPORTING
        E_UDATE = &UDATE
        E_UTIME = &UTIME.

WRITE: / &UDATE, &UTIME.

Selection screen:

Output:

Please notice that this is not a fully functional report. The code is provided in a simple report in order to show the way the code works.

Regards,

Sergiu Iatco

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