Filling a New Type of IDoc by Running an ABAP Program

I’d like to share a short note on how to create and populate a new type of IDoc by executing an ABAP program.

0. Objective

Create a new IDoc type and an ABAP program, which, when executed, will populate the IDoc with data from a custom table.

1. Creating the Table

Create a table whose values will be included in a segment of your IDoc.

See the note: Creating a Custom Table and Its Maintenance View in SAP

In my example, I’ll use a table with the technical name ZTBL_IDOC1.

2. Creating a New IDoc Segment

See: Defining Segments

Create a new segment for your IDoc using transaction WE31. In my case, the structure of the new segment will resemble the structure of the table whose values I plan to transfer to this segment.

Once the new segment is created, unlock it by selecting Edit -> Set release from the menu.

3. Creating a New IDoc Type

See: Defining New IDoc Types
See: Defining and Using a Basic Type

Using transaction WE30, create a new IDoc type and include the previously created segment in it.

Also, unlock the new IDoc type by selecting Edit -> Set release from the menu.

4. Creating a New Message Type and Assigning It to the IDoc Type

See: Assigning Basic Types to Message Types

Create a new message type and assign it to the newly created IDoc type. Perform these steps in transactions WE81 and WE82.

5. Adding the New Message Type to the Receiving System

See: Partner Profiles in the Standard Dialog

In transaction WE20, add the new message type for the system where the new IDoc is intended to be sent.

6. Setting Up the Distribution Model

See:  Modelling Distribution
See: Setting Up Communication

Configure the distribution model using transaction BD64, adding the new message type.

See: SAP IDocs. Подготовка HR мастер-данных для переноса в другую HR систему. Часть 1

See: SAP IDocs. Подготовка HR мастер-данных для переноса в другую HR систему. Часть 2

7. Creating the ABAP Program to Populate the IDoc

The source code was taken from here and slightly adapted.

REPORT zdemo_idoc.
 
TABLES: ztbl_idoc1.
DATA : s_ctrl_rec LIKE edidc, "Idoc Control Record
       s_zsegment LIKE zdm_sgmt. "CUSTOMER Header Data
 
DATA : t_tbl_idoc1 LIKE ztbl_idoc1 OCCURS 0 WITH HEADER LINE.
 
DATA : t_edidd LIKE edidd OCCURS 0 WITH HEADER LINE. "Data Records
DATA : lt_comm_idoc LIKE edidc OCCURS 0 WITH HEADER LINE. "Generated Communication IDOc
 
CONSTANTS: c_idoctp LIKE edidc-idoctp VALUE 'ZBASIC_IT'. "
CONSTANTS :c_segnam LIKE edidd-segnam VALUE 'ZDM_SGMT'.  "
 
 
SELECT-OPTIONS: s_pernr  FOR ztbl_idoc1-pernr OBLIGATORY.
 
PARAMETERS: c_mestyp LIKE edidc-mestyp DEFAULT 'ZDEMO_MESSTYPE', "Message type
            c_rcvprt LIKE edidc-rcvprt DEFAULT 'LS', "System type
            c_logsys LIKE edidc-rcvprn DEFAULT 'ERPCLNT810'. "Reciever
 
START-OF-SELECTION.
  PERFORM generate_data_records.
  PERFORM generate_control_record.
  PERFORM send_idoc.
 
FORM generate_data_records .
  SELECT * FROM ztbl_idoc1
          INTO TABLE t_tbl_idoc1
                  WHERE pernr IN s_pernr.
  IF sy-subrc <> 0.
  ENDIF.
  PERFORM arrange_data_records.
ENDFORM.
 
FORM generate_control_record .
  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
ENDFORM.
 
FORM send_idoc .
  CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'
    EXPORTING
      master_idoc_control            = s_ctrl_rec
*     OBJ_TYPE                       = ''
*     CHNUM                          = ''
    TABLES
      communication_idoc_control     = lt_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 lt_comm_idoc.
      WRITE:/ 'IDoc Number - ', lt_comm_idoc-docnum.
    ENDLOOP.
  ENDIF.
ENDFORM.
FORM arrange_data_records .
 
  SORT t_tbl_idoc1 BY pernr.
 
  LOOP AT t_tbl_idoc1.
    s_zsegment-pernr = t_tbl_idoc1-pernr.
    s_zsegment-begda = t_tbl_idoc1-begda.
    s_zsegment-endda = t_tbl_idoc1-endda.
    s_zsegment-zfield1 = t_tbl_idoc1-zfield1.
    s_zsegment-zfield2 = t_tbl_idoc1-zfield2.
    s_zsegment-zfield3 = t_tbl_idoc1-zfield3.
    t_edidd-segnam = c_segnam.
    t_edidd-sdata = s_zsegment.
    APPEND t_edidd.
    CLEAR t_edidd.
  ENDLOOP.
ENDFORM.

8. Testing

The following video demonstrates the sequence of actions:

  • Data is collected from the custom table and used to populate segments of the new IDoc via the ABAP program;
  • The same program calls the function module MASTER_IDOC_DISTRIBUTE to create the IDoc and add it to the outbound queue;
See: Call of MASTER_IDOC_DISTRIBUTE
0:00
/2:39

As a result, three IDocs were created with three entries in their segments, matching the records in the Z* table. After processing with program RSEOUT00, the IDoc status changed from "30 - IDoc ready for dispatch (ALE service)" to "03 - Data passed to port OK".

Thank you for your attention!