Blog > IDoc modification made easy with ABAP Object Oriented Programming

IDoc modification made easy with ABAP Object Oriented Programming

Mateusz Adamus SAP Integration Consultant
icon__calendar 2020-07-29

Introduction

In this blog, you will learn how to easily perform the create, read, update and delete (CRUD) operations on IDoc segments, thanks to the use of ABAP Object Oriented Programming.

Reading time: 4 minutes

The standard way

The Intermediate Document (IDoc) format is widely used throughout the SAP system, with thousands of message types and countless segment types. Still, the standard way of handling the CRUD operations relies on performing modifications directly on the internal table that contains all of the message’s segments. This means that you, the developer, are responsible for not only modifying the values in the segments, but also for maintaining the correct structure of the IDoc (segments sequence). This can be cumbersome when dealing with different IDoc types/extensions and adds unnecessary work.

 

The example below shows the standard logic responsible for creating an invoice IDoc, as seen in the function ISU_IDOC_INVOICE_CREATE:


 

ABAP objects

To simplify the code responsible for IDoc CRUD operations, you can implement a Facade pattern. The proposed facade consists of a set of three custom classes, and the relationship between them is shown on the UML diagram below:

IDoc with ABAP OOP UML

UML diagram of Facade objects’ relationships

 

In short, each of the classes is responsible for:

  • ZCL_IDOC_EDIDD – the root segment of the IDoc and generic IDoc operations,
  • ZCL_IDOC_EDIDD_SEGMENT – any other segment of the IDoc and generic segment operations,
  • ZCX_IDOC_EXCEPTIONS – exceptions raised in case of an incorrect operation, e.g. adding a segment of an incorrect type.

 

The ABAP OOP way

Before any modification is done to the IDoc segments, you need to create an object, which will represent the message. You can do this using the CREATE_WITH_DATA method of the ZCL_IDOC_EDIDD class.



As you can see in the example above, the first two parameters define the message type and extension. This way, the LO_IDOC object is able to retrieve all information about the structure of the message that is required for maintaining the correct sequence of the segments.

The IT_EDIDD parameter is optional. You should populate it with the EDIDD internal table of the IDoc you wish to modify.

When the IDoc object is created, you are ready to modify its contents. You can use the ADD_SEGMENT method to add a new segment to the IDoc. Remember about catching exceptions that may be raised during the operation, for example if the segment is not part of the message type.



As you can see, there is no need to worry about the position of the added segment. The LO_IDOC object will handle it on its own, adding the segment in the correct position under the root segment of the IDoc or under the last parent segment (if a non-root parent is required).

If the parent segment is not found, an exception will be raised.

You can also add a child segment directly under a specific parent segment.



This way, you are in total control under which parent the new segment will appear in the IDoc. But how to find the segment you are interested in? For this, you need to use the GET_SEGMENTS method, which returns a collection of segments. Have a look at the example below:



Modification of any of the segments is as simple as finding it, retrieving its SDATA structure, modifying it and putting it back into the segment.



Last but not least, the delete operation.

If you want to remove any of the segments from the IDoc, simply use the REMOVE_SEGMENT method. The segment will be removed even if it is not placed directly under the specified parent. This means that if you use the LO_IDOC object for the method call, the segment will be removed regardless of its position in the IDoc.



It is worth mentioning that when you remove a segment from the IDoc, all of its children are removed as well. So, again, there is no need to bother with maintaining the correct structure of the message.

After you are finished with the modifications, all is left to do is to retrieve the resulting internal table containing the IDoc segments. You can do this by calling the GET_EDIDD method.


Summary

With the use of just three simple ABAP classes, you are able to simplify the implementation of IDoc CRUD operations, leaving handling of the correctness of the segments’ sequence to the system.

You can download the solution from GitHub.

Mateusz Adamus SAP Integration Consultant
Working with SAP environment since 2007. Involved in several international projects as an ABAP developer and programming team leader. Mateusz has broad experience in designing and implementing custom SAP solutions. Supporter of all solutions that allow to streamline work processes.