Register Login

Create IDOC through Function Module

Updated Nov 02, 2024

SAP IDoc (Intermediate Document) is a standard data structure that is used to transfer data between SAP systems or between an SAP system and an external system. It is frequently utilized to facilitate asynchronous communication in situations such as application link enabling (ALE) and electronic data interchange (EDI).

Key Components of an IDoc

The key components of IDoc are:

  • Control Record (EDIDC): Stores metadata like sender, receiver, and IDoc type.
  • Data Record (EDIDD): Contains the actual business data.
  • Status Record (EDIDS): Tracks the processing status of the IDoc.

Creating a Custom Function Module for IDoc Generation

In this article we will create a custom function module  Y_ISSUE_ROCO_IDOC that generates an IDOC to distribute route information for orders.

1. Define the Custom Function Module

Go to T-code SE37 to create a custom function module in the SAP Command Field.

FUNCTION Y_ISSUE_ROCO_IDOC.
* Define the interface to accept input data required for the IDoc
IMPORTING
  VALUE(I_MODE) LIKE Z1ROCO-ZMODE
  VALUE(I_ROUTE) LIKE Z1ROCO-ROUTE
  VALUE(I_CUT_OFF) LIKE Z1ROCO-CUT_OFF OPTIONAL
  VALUE(I_BEZEI) LIKE Z1ROCO-BEZEI OPTIONAL
  VALUE(I_TROUTE_MON) LIKE Z1ROCO-TROUTE_MON OPTIONAL
  VALUE(I_PGI_IND) LIKE Z1ROCO-PGI_IND OPTIONAL.

* Define internal tables for IDoc control and data records
DATA: W_EDIDC LIKE EDIDC OCCURS 5 WITH HEADER LINE,
      W_SDATA LIKE EDIDD-SDATA,
      L_EDIDC LIKE EDIDC,
      L_SEND_FLAG TYPE C.
...

2. Check if IDoc Should Be Distributed

The ALE_MODEL_DETERMINE_IF_TO_SEND function module determines if the IDoc message should be sent based on the message type.

CALL FUNCTION 'ALE_MODEL_DETERMINE_IF_TO_SEND'
  EXPORTING
    MESSAGE_TYPE       = 'ZZROCO'
  IMPORTING
    IDOC_MUST_BE_SENT  = L_SEND_FLAG
  EXCEPTIONS
    OWN_SYSTEM_NOT_DEFINED = 1
    OTHERS = 2.
IF SY-SUBRC <> 0.
  RETURN.
ENDIF.

3. Determine Recipient Systems

The function module ALE_MODEL_INFO_GET retrieves recipient system details for routing the IDoc.

CALL FUNCTION 'ALE_MODEL_INFO_GET'
  EXPORTING
    MESSAGE_TYPE = 'ZZROCO'
  TABLES
    MODEL_DATA   = T_BDI_MODEL
  EXCEPTIONS
    NO_MODEL_INFO_FOUND      = 1
    OWN_SYSTEM_NOT_DEFINED   = 2
    OTHERS                   = 3.
IF SY-SUBRC <> 0.
  RETURN.
ENDIF.

4. Populate Segment Data

Assign input values to IDoc segment fields, like I_MODE, I_ROUTE, and other optional fields.

MOVE I_MODE TO Z1ROCO-ZMODE.
MOVE I_ROUTE TO Z1ROCO-ROUTE.
IF I_CUT_OFF IS NOT INITIAL.
  MOVE I_CUT_OFF TO Z1ROCO-CUT_OFF.
ENDIF.
MOVE Z1ROCO TO: W_SDATA, T_EDIDD-SDATA.
MOVE 'Z1ROCO' TO T_EDIDD-SEGNAM.
APPEND T_EDIDD.

5. Configure IDoc Control Record

The control record specifies the routing information, message type, and IDoc type.

READ TABLE T_BDI_MODEL INDEX 1.
IF SY-SUBRC = 0.
  MOVE 'ZZROCO' TO L_EDIDC-MESTYP.
  MOVE 'ZSDROCO' TO L_EDIDC-IDOCTP.
  MOVE 'LS' TO L_EDIDC-RCVPRT.
  MOVE T_BDI_MODEL-RCVSYSTEM TO L_EDIDC-RCVPRN.
ENDIF.

6. Distribute the IDoc

The IDoc is distributed to the receiving system by the MASTER_IDOC_DISTRIBUTE function. To complete the IDoc distribution, the COMMIT WORK command is required.

CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE' IN UPDATE TASK
  EXPORTING
    MASTER_IDOC_CONTROL    = L_EDIDC
  TABLES
    COMMUNICATION_IDOC_CONTROL = W_EDIDC
    MASTER_IDOC_DATA           = T_EDIDD
  EXCEPTIONS
    ERROR_IN_IDOC_CONTROL       = 1
    ERROR_WRITING_IDOC_STATUS   = 2.
COMMIT WORK.

Testing the IDoc Generation Function Module

Once the function module is implemented, test it to verify that the IDoc is created and distributed correctly following these points:

  • In a development environment, call the function module.
  • Checking the WE02 or WE05 IDoc status.
  • Make sure the designated recipient system receives the IDoc.

Conclusion

Creating IDocs using SAP function modules allows for customized data-sharing scenarios. Standard function modules such as MASTER_IDOC_DISTRIBUTE, ALE_MODEL_INFO_GET, and ALE_MODEL_DETERMINE_IF_TO_SEND allow you to effectively create and distribute IDocs according to particular needs.


×