Saturday 29 December 2018

How to add an user-defined segment to an IDoc

This post will show you how to create a new Segment and append it to a IDoc type as an extension.

Introduction:


When you create a new extension for an IDoc, transfer the data, then insert again into IDoc, it sometime might lead to Status Error 26 “Get details from previous status records with status 26”.

It is because of hierarchy error. In another word, you insert the record into wrong position.

This post will use a trick to avoid that problem.

Scenario:


You have to transfer some custom data via IDoc  ARBCIG_DELINS for Scheduling Agreement Delivery. You need to implement the below steps to achieve it:

1. Create a new Zsegment
2. Create extension for Idoc type
3. Maintain Output Types and Assignment to IDoc Types in WE82
4. Maintain Partner Profile in WE20
5. Write the code in USER EXIT ZXM06U60 (for Scheduling Agreement)

1. Create a new Z-Segment (Tcode WE31)

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

Then set it released by press F3, Go to Menu->Edit->Set Release

2. Create Extension for Basic Type ARBCIG_DELINS (Tcode WE30)

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

3. Maintain Output Types and Assignment to IDoc Types in WE82 (Tcode WE82)

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

4. Maintain Partner Profile in WE20  (Tcode WE20)

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

5. Write the code in USER EXIT ZXM06U60 (for Scheduling Agreement)

Because our ZSegment position is right under  E1EDP10 Segment, therefore we need to find   the exact index no. where to insert the new segment. We use the following code:

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

CALL FUNCTION 'IDOCTYPE_READ'
    EXPORTING
      PI_IDOCTYP         = 'ARBCIG_DELINS'
      PI_CHECK_AUTHORITY = ' '
    TABLES
      PT_SYNTAX          = LT_EDI_IAPI02
    EXCEPTIONS
      OBJECT_NOT_FOUND   = 1
      DB_ERROR           = 2
      NO_AUTHORITY       = 3
      OTHERS             = 4.
  IF SY-SUBRC <> 0.
    EXIT.
  ELSE.
    READ TABLE LT_EDI_IAPI02 INTO LS_EDI_IAPI02 WITH KEY SEGTYP = 'E1EDP10' .
    IF SY-SUBRC <> 0 .
      EXIT.
    ELSE.
      LV_NR = LS_EDI_IAPI02-NR .
    ENDIF.
  ENDIF.

** Identify the exact index no. where to insert the new segment.
  LOOP AT DINT_EDIDD ASSIGNING FIELD-SYMBOL(<LF_IDOCS>) .
    LW_INDEX = SY-TABIX.
    READ TABLE LT_EDI_IAPI02 INTO LS_EDI_IAPI02 WITH KEY SEGTYP = <LF_IDOCS>-SEGNAM.
    IF LS_EDI_IAPI02-NR > LV_NR AND LV_COMPL = SPACE.
      LV_COMPL = 'X' .
      EXIT.
    ENDIF.
  ENDLOOP.

Then we check the Zsegment is existing or not and Append or Insert corresponding.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

  READ TABLE LT_EDIDD ASSIGNING FIELD-SYMBOL(<LF_TEST>)
                        WITH KEY SEGNAM = 'Z1EDP10'.

    IF SY-SUBRC = 0.
      <LF_TEST>-SDATA = LS_Z1EDP10.
    ELSE.
      MOVE <LF_E1EDP10_TM> TO LS_EDIDD.
      CLEAR LS_EDIDD-SDATA.

      LS_EDIDD-SEGNAM = 'Z1EDP10'.
      LS_EDIDD-SDATA = LS_Z1EDP10.
      LW_INDEX = LW_INDEX + 1.

      IF LV_COMPL = 'X' .
        INSERT LS_EDIDD INTO DINT_EDIDD INDEX LW_INDEX.
      ELSE.
        APPEND LS_EDIDD TO DINT_EDIDD.
      ENDIF.
    ENDIF.

Finally, the values of Zsegment will come through into IDoc.

SAP ABAP Tutorial and Material, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Certification

No comments:

Post a Comment