Principle For Creating IDoc

IDocs are usually created iun a three step process, consisting of retrieving the data, converting them to IDoc format and delivering the IDoc to a port.
Collect data from R/3 database
This is the most individual task in outbound processing. You have to identify the database tables and data dependencies, which are needed in the IDoc to be sent. The smartest way is usually to select the data from database into an internal table using SELECT * FROM dbtable INTO itab  ... WHERE ...
Wrap data in IDoc format
The collected data must be transformed into ASCII data and filled into the predefined IDoc segment structures. The segment definitions are done with transaction WE31 and the segments allowed in an IDoc type are set up in transaction WE30. Segment once defined with WE31 are automatically created as SAP DDIC structures. They can be viewed with SE11, however they cannot be edited
Create the IDoc header
Every IDoc must be accompanied by a header record. This record must contain at least the IDoc type to identify the syntactical structure of the data and it must contain the name and role of the sender and the receiver. This header information is checked against the partner definitions for outbound. Only if a matching partner definition exists, the IDoc can be sent. Partner definitions are set up with transaction WE20.
Send data to port
When the partner profile check passes, the IDoc is forwarded to a logical port, which is also assigned in the partner profile. This port is set up with transaction WE21 and defines the  medium to transport the IDoc, eg. file or RFC. The RFC destinations are set up with transaction SM57 and must also be entered in table TBDLS with an SM31 view. Directories for outbound locations of files are set up with transaction FILE and directly in WE21. It also allows to use a function module which generate file names. STandard functions for that purpose begin like EDI_FILE*.