Blog Layout

SAP Idoc parser in Python

flabouch • May 12, 2022

As mentioned in the previous post about EDI, in order to simplify and automate our work, we have developed a library that parse an Idoc and return a python list of dictionaries. Before entering in the details let’s review the basics.

 

What is a SAP Idoc?


IDocs are used in SAP applications to transfer message(information) between SAP system and other systems (Could be another SAP or any other system).

IDoc is a SAP object that carries data of a business transaction. IDoc is an acronym for Intermediate Document.


Source: IDoc Basics For Functional Consultants | SAP Blogs

 

What is the structure of an Idocs?


The Idoc structure consists of 3 parts:

  • The administration part(Control Record)- which has the type of idoc, message type, the current status, the sender, receiver etc. This is referred to as the Control record.
  • The application data (Data Record) – Which contains the data. These are called the data records/segments.
  • The Status information (Status Record)- These give you information about the various stages the idoc has passed through.


You can view an I-DOC using transaction WE02 or WE05.


Source: SAP IDOC Tutorial: Definition, Structure, Types, Format & Tables (guru99.com)


What is a Segment?


A Segment defines the format and structure of a data record in Idoc. Segments are reusable components.

For each segment SAP creates:

  • Segment Type (version independent)
  • Segment Definition (version dependent)
  • Segment Documentation


The last 3 characters is the version of the segment.


Source: SAP IDOC Tutorial: Definition, Structure, Types, Format & Tables (guru99.com)

 

Where to find Idoc documentation?

 

You can get the documentation of a specific Idoc type by using the transaction WE60, and entering the Idoc type and then press F9.


SAP WE60 tcode

The documentation can be extracted as a .txt file format, which will be used by our Idoc parser. To extract the documentation, go to System > List > Save > Local File and choose the unconverted format.


SAP WE60 Idoc documentation extract

What does an Idoc look like?

 

I used a picture available on Oracle website as I didn’t want to leak any information that I should not. I assume Oracle did the check and sanitised the data before publishing the picture.



SAP Idoc example

Picture source: https://docs.oracle.com/cd/E11223_01/doc.910/e11210/structure-sample-idoc.htm#MCSER-GUID-3F5039AF-53CB-4BBD-9DC2-83FED462C121

 

As you can see on the picture, the format of an Idoc is clean but to extract the required data you definitively need the documentation, as each properties in a segment (a line in the file above) can be find based on the number of characters and position in the line.

 

If you are importing an Idoc into SAP, no problem, SAP do provide a native parser. Now if you need to process the Idoc to create a xml or csv, or any other format, it becomes more difficult.

In order to simplify our daily job we have created a parser in python.


How does the parser work?

 

Our parser required two documents, the Idoc documentation from SAP (the one you can get from WE60) and the Idoc. Of course the documentation need to be the one describing the Idoc type.

 

In return, our parser will provide a dictionary list with the below format:

 

[{'segment': 'E1EDL20', 'reference': '000001', 'parent': '000000', 'properties': [{'name': 'VBELN', 'text': 'Sales and Distribution Document Number', 'value': 'xxxxxx'}, {'name': 'VSTEL', 'text': 'Shipping Point/Receiving Point', 'value': 'xxxxx'}, {'name': 'VKORG', 'text': 'Sales Organization', 'value': 'xxxxx'}, {'name': 'LSTEL', 'text': 'Loading Point', 'value': ''}, ….

 

For each segment in the idoc, the list returned includes the line number (reference), the link with the parent if any (parent line number), and the details of each properties.

It’s then easy to go thru the full list, grab the required data (including the link parent-children), and generate the output in the format expected by the receiving system.

 

If you are interested to test the library for one of your project, just contact us.


SII Spain Integration thru API
14 Jul, 2023
SII in Spain: What is it, why does your company need to comply, and how to do it?
Integrating VIES VAT Validation with your ERP
By flabouch 19 May, 2022
How to integrate and automate VIES VAT number validation of EU commission with your ERP.
Trading partner integration
By flabouch 30 Apr, 2022
Business partner integration can be challenging, due to different technology on both side and most likely due to business process difference and non-flexibility.
Share by: