Sunday, September 27, 2020

Generates an IDOC for Customer Master, Sales Master and Sales item table in SAP ABAP.

Generates an IDOC for Customer Master, Sales Master  and Sales item table in SAP ABAP.



IDOC Report Details.

 &---------------------------------------------------------------------*

*& Report  ZRZ_ORDER_IDOC                                              *                                                                     *

*&---------------------------------------------------------------------*

 REPORT  ZRZ_ORDER_IDOC                          .

 ***********************************************************************

* PURPOSE OF REPORT *

***********************************************************************

* Generats an Idoc for Customer Master, Sales Master

* and Sales item table.

***********************************************************************

TABLES : ZCUSTOMERS, "Cutomer Header

         ZSOHEADERS, "Sales Header

         ZSOITEMS. "Sales Items

DATA : S_CTRL_REC LIKE EDIDC, "Idoc Control Record

       S_ZRZSEG1 LIKE ZRZSEG1, "CUSTOMER Header Data

       S_ZRZSEG2 LIKE ZRZSEG2, "SALES HEADER Data

       S_ZRZSEG3 LIKE ZRZSEG3. "SALES Detail Data

DATA : T_ZCUSTOMERS LIKE ZCUSTOMERS OCCURS 0 WITH HEADER LINE.

DATA : T_ZSOHEADERS LIKE ZSOHEADERS OCCURS 0 WITH HEADER LINE.

DATA : T_ZSOITEMS LIKE ZSOITEMS OCCURS 0 WITH HEADER LINE.

DATA : T_EDIDD LIKE EDIDD OCCURS 0 WITH HEADER LINE."Data Records

DATA : T_COMM_IDOC LIKE EDIDC OCCURS 0 WITH HEADER LINE.

"Generated Communication IDOc

CONSTANTS :

C_ZRZSEG1 LIKE EDIDD-SEGNAM VALUE 'ZRZSEG1',

C_ZRZSEG2 LIKE EDIDD-SEGNAM VALUE 'ZRZSEG2',

C_ZRZSEG3 LIKE EDIDD-SEGNAM VALUE 'ZRZSEG3'.

 

CONSTANTS: C_IDOCTP LIKE EDIDC-IDOCTP VALUE 'ZRZORDER'.

*** Selection Screen

SELECT-OPTIONS : S_KUNNR FOR ZCUSTOMERS-KUNNR OBLIGATORY,

                 S_VBELN FOR ZSOHEADERS-VBELN.

PARAMETERS :  C_MESTYP LIKE EDIDC-MESTYP DEFAULT 'ZRZSO_MT',

              "Message Type

              C_RCVPRT LIKE EDIDC-RCVPRT DEFAULT 'LS',

              "Partner type of receiver

              C_LOGSYS LIKE EDIDC-RCVPRN DEFAULT 'Y901',

              C_RCVPOR LIKE EDIDC-RCVPOR DEFAULT 'A000000226',

              C_SNDPRN LIKE EDIDC-SNDPRN DEFAULT 'LSSENDS',

              C_SNDPRT LIKE EDIDC-SNDPRT DEFAULT 'LS'.

"Destination System

 

***START-OF-SELECTION

START-OF-SELECTION.

  PERFORM GENERATE_DATA_RECORDS.

  PERFORM GENERATE_CONTROL_RECORD.

  PERFORM SEND_IDOC.

 ***********************************************************************

*&---------------------------------------------------------------------*

*&      Form  generate_data_records

*&---------------------------------------------------------------------*

FORM GENERATE_DATA_RECORDS .

  PERFORM FETCH_ZCUSTOMERS.

  PERFORM FETCH_ZSOHEADERS.

  PERFORM FETCH_ZSOITEMS.

  PERFORM ARRANGE_DATA_RECORDS.

ENDFORM.                    " generate_data_records

*&---------------------------------------------------------------------*

*&      Form  fetch_zcustomers

*&---------------------------------------------------------------------*

FORM FETCH_ZCUSTOMERS.

  SELECT *

  FROM ZCUSTOMERS

  INTO TABLE T_ZCUSTOMERS

  WHERE KUNNR IN S_KUNNR.

  IF SY-SUBRC NE 0.

    MESSAGE E398(00) WITH 'No Customers Found'.

  ENDIF.

ENDFORM.                    " fetch_zcustomers

*&---------------------------------------------------------------------*

*&      Form  fetch_zsoheaders

*&---------------------------------------------------------------------*

FORM FETCH_ZSOHEADERS.

  SELECT *

  FROM ZSOHEADERS

  INTO TABLE T_ZSOHEADERS

  WHERE VBELN IN S_VBELN

    AND KUNNR IN S_KUNNR.

  IF SY-SUBRC NE 0.

    MESSAGE I398(00) WITH 'No Sales orders found'.

  ENDIF.

ENDFORM.                    " fetch_zsoheaders

*&---------------------------------------------------------------------*

*&      Form  fetch_zsoitems

*&---------------------------------------------------------------------*

FORM FETCH_ZSOITEMS.

  IF NOT T_ZSOHEADERS[] IS INITIAL.

    SELECT *

    FROM ZSOITEMS

    INTO TABLE T_ZSOITEMS

    FOR ALL ENTRIES IN T_ZSOHEADERS

    WHERE VBELN = T_ZSOHEADERS-VBELN.

    IF SY-SUBRC NE 0.

      MESSAGE I398(00) WITH 'No Sales order items found'.

    ENDIF.

  ENDIF.

ENDFORM.                    " fetch_zsoitems

 *&---------------------------------------------------------------------*

*&      Form  generate_control_record

*&---------------------------------------------------------------------*

FORM GENERATE_CONTROL_RECORD .

  S_CTRL_REC-RCVPOR = C_RCVPOR. "Receiver Port

  S_CTRL_REC-MESTYP = C_MESTYP. "Message type

  S_CTRL_REC-IDOCTP = C_IDOCTP. "Basic IDOC type

  S_CTRL_REC-RCVPRT = C_RCVPRT. "Partner type of receiver

  S_CTRL_REC-RCVPRN = C_LOGSYS. "Partner number of receiver

 

  S_CTRL_REC-SNDPRT = C_SNDPRT. "Sender Partner type

  S_CTRL_REC-SNDPRN = C_SNDPRN. "Sender Partner Number

ENDFORM.                    " generate_control_record

 

*&---------------------------------------------------------------------*

*&      Form  send_idoc

*&---------------------------------------------------------------------*

FORM SEND_IDOC.

  CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'

    EXPORTING

      MASTER_IDOC_CONTROL            = S_CTRL_REC

    TABLES

      COMMUNICATION_IDOC_CONTROL     = T_COMM_IDOC

      MASTER_IDOC_DATA               = T_EDIDD

    EXCEPTIONS

      ERROR_IN_IDOC_CONTROL          = 1

      ERROR_WRITING_IDOC_STATUS      = 2

      ERROR_IN_IDOC_DATA             = 3

      SENDING_LOGICAL_SYSTEM_UNKNOWN = 4

      OTHERS                         = 5.

  IF SY-SUBRC <> 0.

    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

  ELSE.

    COMMIT WORK.

    LOOP AT T_COMM_IDOC.

      WRITE:/ 'IDoc Generated - ', T_COMM_IDOC-DOCNUM.

    ENDLOOP.

  ENDIF.

ENDFORM.                    " send_idoc

 *&---------------------------------------------------------------------*

*&      Form  arrange_data_records

*&---------------------------------------------------------------------*

FORM ARRANGE_DATA_RECORDS .

  DATA: W_INDEX1 LIKE SY-TABIX,

        W_INDEX2 LIKE SY-TABIX.

  SORT T_ZCUSTOMERS BY KUNNR.

  SORT T_ZSOHEADERS BY KUNNR VBELN.

  SORT T_ZSOITEMS BY VBELN POSNR.

 

  LOOP AT T_ZCUSTOMERS.

    S_ZRZSEG1-KUNNR = T_ZCUSTOMERS-KUNNR.

    S_ZRZSEG1-NAME1 = T_ZCUSTOMERS-NAME1.

    S_ZRZSEG1-ORT01 = T_ZCUSTOMERS-ORT01.

    S_ZRZSEG1-LAND1 = T_ZCUSTOMERS-LAND1.

    T_EDIDD-SEGNAM = C_ZRZSEG1.

    T_EDIDD-SDATA = S_ZRZSEG1.

    APPEND T_EDIDD.

    CLEAR T_EDIDD.

 

    CLEAR W_INDEX1.

    READ TABLE T_ZSOHEADERS WITH KEY KUNNR = T_ZCUSTOMERS-KUNNR BINARY SEARCH.

 

    IF SY-SUBRC = 0.

      W_INDEX1 = SY-TABIX.

      LOOP AT T_ZSOHEADERS FROM W_INDEX1.

        IF T_ZSOHEADERS-KUNNR NE T_ZCUSTOMERS-KUNNR.

          EXIT.

        ENDIF.

        S_ZRZSEG2-VBELN = T_ZSOHEADERS-VBELN.

        S_ZRZSEG2-KUNNR = T_ZSOHEADERS-KUNNR.

        T_EDIDD-SEGNAM = C_ZRZSEG2.

        T_EDIDD-SDATA = S_ZRZSEG2.

        APPEND T_EDIDD.

        CLEAR T_EDIDD.

 

        CLEAR W_INDEX2.

        READ TABLE T_ZSOITEMS WITH KEY VBELN = T_ZSOHEADERS-VBELN BINARY SEARCH.

        IF SY-SUBRC = 0.

          W_INDEX2 = SY-TABIX.

          LOOP AT T_ZSOITEMS FROM SY-TABIX.

            IF T_ZSOITEMS-VBELN NE T_ZSOHEADERS-VBELN.

              EXIT.

            ENDIF.

            S_ZRZSEG3-VBELN = T_ZSOITEMS-VBELN.

            S_ZRZSEG3-POSNR = T_ZSOITEMS-POSNR.

            S_ZRZSEG3-MATNR = T_ZSOITEMS-MATNR.

            S_ZRZSEG3-NETWR = T_ZSOITEMS-NETWR.

            S_ZRZSEG3-ZMENG = T_ZSOITEMS-ZMENG.

            T_EDIDD-SEGNAM = C_ZRZSEG3.

            T_EDIDD-SDATA = S_ZRZSEG3.

            APPEND T_EDIDD.

            CLEAR T_EDIDD.

          ENDLOOP.

        ENDIF.

      ENDLOOP.

    ENDIF.

  ENDLOOP.

ENDFORM.                    " arrange_data_records

No comments:

Post a Comment