SAP Invoice IDoc INVOIC02 – Structure, User-Exit and BADI

chrysler invoice 46370 o e1442487543248

Idoc INVOIC02: This article will introduce you to outbound IDoc for Invoice/Billing document in SAP.

It will cover the IDoc Invoic02 structures, XML Schema, Enhancement for INVOICE Idoc and ABAP Code to generate INVOICE Idoc with BAPI

  • Idoc INVOIC02 Structure
  • User Exit and BADI for Idoc INVOIC02
  • Generate Invoice IDoc with BAPI

Idoc INVOIC02 Structure

SAP INVOICE IDoc Basic Type Structure

First at all, let’s detail the main segments of INVOIC02 :

  • E1EDK01 for IDoc: Document header general data
  • E1EDKA1 for IDoc: Document Header Partner Information
  • E1EDKT1 for IDoc: Document Header Text Identification
  • E1EDKT2 for IDoc: Document Header Texts Lines
  • E1EDK14 for IDoc: Document Header Organizational Data
  • E1EDP01 for IDoc: Document Item General Data
Invoic02 IDoc Basic Type

The full INVOIC02 Basic Type is:

In Order to go further, you can check the full documentation on how to manageInbound Processing of IDocs Received.

XML Schema for Invoice IDoc

This link contains the Idoc INVOIC02 XML Schema.( For obvious security reason, I put the document in Zipped format).

User Exit and BADI for Idoc INVOIC02

User Exit for SAP Invoice Idoc

EXIT_SAPLVEDF_001 (ZXEDFU01)

This User Exit offers to make additional Control Data IDoc_Output_Invoice.
So the parameters of this User Exist are:

*"  IMPORTING
*"     VALUE(CONTROL_RECORD_OUT) LIKE  EDIDC STRUCTURE  EDIDC
*"     VALUE(DVBDKR) LIKE  VBDKR STRUCTURE  VBDKR OPTIONAL
*"     VALUE(DOBJECT) LIKE  NAST STRUCTURE  NAST OPTIONAL
*"  EXPORTING
*"     VALUE(CONTROL_RECORD_OUT) LIKE  EDIDC STRUCTURE  EDIDC
*"  TABLES
*"      DXVBPA STRUCTURE  VBPA OPTIONAL
*"      DTVBDPR STRUCTURE  VBDPR OPTIONAL
*"  EXCEPTIONS
*"      ERROR_MESSAGE_RECEIVED
*"      DATA_NOT_RELEVANT_FOR_SENDING
*"----------------------------------------------------------------------

User Exit EXIT_SAPLVEDF_002 (ZXEDFU02)

This User Exist allows Customer Enhancement in Data Segments when generating Billing Document Output.

It called when generating the INVOIC02 Idoc SEGMENT BY SEGMENT.

The following Code is an example of ABAP implementation of the user exit ZXEDFU02

  DATA: ls_E1EDKA1  LIKE e1edka1,
        ls_edidd    LIKE edidd.
  DATA: lv_index    TYPE I.
        
  READ TABLE int_edidd INTO ls_edidd index lv_index.

  CHECK ls_edidd-segnam EQ 'E1EDKA1'  AND
        ls_edidd-sdata(2) EQ 'WE'.

  ls_E1EDKA1-parvw = 'AG'.

  ls_edidd-sdata = ls_E1EDKA1.
  APPEND ls_edidd TO int_edidd.

BADI for SAP Invoice Idoc INVOICE02

SD_INVOICE_IDOC_OUTPUT_INT: Internal BADI for IDOC output processing

Hence, This BADI has 4 methods:

  • CONTROL_RECORD_OUT_PREPARE: check and modify EDIDD ( Idoc Data Table )
  • IDOC_DATA_APPEND: allows to add more data segment for IDoc
  • INVOICE_READ helps read the SAP Invoice.
  • PACKAGING_DATA_READ reads the packaging Data.

SAP Invoice Tables

In order to fill the enhancement of INVOIC02, you can rely on the most important tables for SAP Invoice

  • VBRP,Billing Document: Item Data
  • VBRK,Billing Document: Header Data
  • VBFK,Invoice: header Data
  • VBFP,Invoice: Item Data
  • RBKP,Document Header: Invoice Receipt

Generate Invoice IDoc with BAPI

With the following ABAP Program Snippet, you can generate an Idoc INVOIC.INVOIC02 in less than a couple of minutes ( between creating of ABAP program/function, copy/paste the activation ).

The main BAPI used to generate an outbound Idoc INVOIC02 for Invoice is the BAPIIDOC_OUTPUT_INVOIC.

Also This BAPI will fill the prepare the Idoc and fill the segments required for INVOIC02 Idoc ( and even extension if user exist or BADI are implemented ).

To explain the following ABAP Coden here the steps:

  1. Generate EDIDD tables for INVOICES with IDOC_OUTPUT_INVOIC
  2. Distribute the IDoc, So a IDoc number is generated with standard BAPI MASTER_IDOC_DISTRIBUTE
  3. If no error occurs when creating the IDoc, we call a commit work with both DB_COMMIT BAPI And COMMIT WORK.
DATA : ls_nast                 TYPE nast,
       ls_ctrl_in              TYPE edidc,
       ls_ctrl_out             TYPE edidc.

DATA : lt_idoc_data            TYPE TABLE OF edidd,
       ls_idoc_header          TYPE edidc,
       lt_comm_idoc_control    TYPE TABLE OF edidc,
       lt_created_comm_idocs   TYPE TABLE OF edidc.

CLEAR ls_nast.
ls_nast-mandt = sy-mandt.
ls_nast-objky = lv_invoicekey. " @Change with Invoice number

clear ls_ctrl_in.
ls_ctrl_in-IDOCTP = 'INVOIC02'.
ls_ctrl_in-MESTYP = 'INVOIC'. " @ You can put your extension

CLEAR lt_idoc_data[].
CALL FUNCTION 'IDOC_OUTPUT_INVOIC'
  EXPORTING
    object                        = ls_nast
    control_record_in             = ls_ctrl_in
  IMPORTING
    control_record_out            = ls_ctrl_out
*   OBJECT_TYPE                   =
  TABLES
    int_edidd                     = lt_idoc_data[]
  EXCEPTIONS
    error_message_received        = 1
    data_not_relevant_for_sending = 2
    OTHERS                        = 3.

CLEAR lt_comm_idoc_control[].

CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'
  EXPORTING
    master_idoc_control            = ls_idoc_header
  TABLES
    communication_idoc_control     = lt_comm_idoc_control
    master_idoc_data               = lt_idoc_data
  EXCEPTIONS
    error_in_idoc_control          = 01
    error_writing_idoc_status      = 02
    error_in_idoc_data             = 03
    sending_logical_system_unknown = 04.
IF sy-subrc EQ 0.
  CALL FUNCTION 'DB_COMMIT'.
  COMMIT WORK.
ENDIF.