# SAP BAPI for Accounting Document Posting: BAPI_ACC_DOCUMENT_POST Generator

> Source: https://netray.co/tools/sap-ecc-bapi-acc-document-post-abap-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Enter company code, dates, accounts and amount to get an ABAP report that checks or posts a G/L or vendor invoice document with BAPI_ACC_DOCUMENT_POST.

## Category

developer

## Erp

SAP ECC 6.0

## Audience

- ABAP developer
- SAP FI consultant
- Integration developer

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Mode

**Key:** mode

**Type:** select

**Options:**

- CHECK
- POST

**Default:** CHECK

**Help:** CHECK calls BAPI_ACC_DOCUMENT_CHECK and never commits.

#### Company code

**Key:** bukrs

**Type:** text

**Default:** 1000

#### Document type

**Key:** doc_type

**Type:** select

**Options:**

- SA
- KR

**Default:** KR

#### Posting date (YYYYMMDD)

**Key:** pstng_date

**Type:** text

**Default:** 20261022

#### Document date (YYYYMMDD)

**Key:** doc_date

**Type:** text

**Default:** 20261020

#### Currency

**Key:** waers

**Type:** text

**Default:** USD

#### Header and item text

**Key:** header_txt

**Type:** text

**Default:** Freight invoice Oct 2026

#### Reference (XBLNR)

**Key:** ref

**Type:** text

**Default:** INV-88231

#### Line 1 G/L account (debit)

**Key:** gl1

**Type:** text

**Default:** 476000

#### Line 1 cost center

**Key:** kostl

**Type:** text

**Default:** 4110

#### Amount (positive)

**Key:** amount

**Type:** text

**Default:** 1250.00

#### Line 2 type

**Key:** line2_type

**Type:** select

**Options:**

- VENDOR
- GL

**Default:** VENDOR

#### Line 2 vendor or offset G/L account

**Key:** offset

**Type:** text

**Default:** 100045

#### Tax code (shown in tax comment only)

**Key:** tax_code

**Type:** text

**Default:** V0

**Blocks:**

#### ABAP report: BAPI_ACC_DOCUMENT_CHECK / POST

**Body:** REPORT z_bapi_acc_document_post.

* Posts (or only checks) a two-line FI document with BAPI_ACC_DOCUMENT_POST.
* Positive amount = debit, negative amount = credit. Lines must net to zero.

DATA: ls_header  TYPE bapiache09,
      ls_glacc   TYPE bapiacgl09,
      lt_glacc   TYPE STANDARD TABLE OF bapiacgl09,
      ls_payable TYPE bapiacap09,
      lt_payable TYPE STANDARD TABLE OF bapiacap09,
      ls_curr    TYPE bapiaccr09,
      lt_curr    TYPE STANDARD TABLE OF bapiaccr09,
      ls_return  TYPE bapiret2,
      lt_return  TYPE STANDARD TABLE OF bapiret2,
      lv_amount  TYPE bapiaccr09-amt_doccur,
      lv_gl1     TYPE bapiacgl09-gl_account,
      lv_offset  TYPE bapiacap09-vendor_no,
      lv_kostl   TYPE bapiacgl09-costcenter,
      lv_objtype TYPE bapiache09-obj_type,
      lv_objkey  TYPE bapiache09-obj_key,
      lv_objsys  TYPE bapiache09-obj_sys,
      lv_error   TYPE c.

START-OF-SELECTION.

  lv_amount = '{{amount}}'.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = '{{gl1}}'
    IMPORTING
      output = lv_gl1.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = '{{offset}}'
    IMPORTING
      output = lv_offset.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = '{{kostl}}'
    IMPORTING
      output = lv_kostl.

* Header
  ls_header-bus_act    = 'RFBU'.
  ls_header-username   = sy-uname.
  ls_header-comp_code  = '{{bukrs}}'.
  ls_header-doc_date   = '{{doc_date}}'.
  ls_header-pstng_date = '{{pstng_date}}'.
  ls_header-doc_type   = '{{doc_type}}'.
  ls_header-ref_doc_no = '{{ref}}'.
  ls_header-header_txt = '{{header_txt}}'.

* Line 1: G/L account with cost center (debit)
  ls_glacc-itemno_acc = 1.
  ls_glacc-gl_account = lv_gl1.
  ls_glacc-costcenter = lv_kostl.
  ls_glacc-item_text  = '{{header_txt}}'.
  APPEND ls_glacc TO lt_glacc.

  ls_curr-itemno_acc = 1.
  ls_curr-currency   = '{{waers}}'.
  ls_curr-amt_doccur = lv_amount.
  APPEND ls_curr TO lt_curr.

* Line 2: vendor or offsetting G/L account (credit)
  IF '{{line2_type}}' = 'VENDOR'.
    ls_payable-itemno_acc = 2.
    ls_payable-vendor_no  = lv_offset.
    ls_payable-item_text  = '{{header_txt}}'.
    APPEND ls_payable TO lt_payable.
  ELSE.
    CLEAR ls_glacc.
    ls_glacc-itemno_acc = 2.
    ls_glacc-gl_account = lv_offset.
    ls_glacc-item_text  = '{{header_txt}}'.
    APPEND ls_glacc TO lt_glacc.
  ENDIF.

  ls_curr-itemno_acc = 2.
  ls_curr-currency   = '{{waers}}'.
  ls_curr-amt_doccur = lv_amount * -1.
  APPEND ls_curr TO lt_curr.

* Tax: to post tax, add a line to table ACCOUNTTAX (BAPIACTX09) with
* the tax code, set TAX_CODE on the base lines, and add the tax amount
* line in CURRENCYAMOUNT with AMT_BASE. Example tax code: {{tax_code}}

  IF '{{mode}}' = 'CHECK'.
    CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'
      EXPORTING
        documentheader = ls_header
      TABLES
        accountgl      = lt_glacc
        accountpayable = lt_payable
        currencyamount = lt_curr
        return         = lt_return.
  ELSE.
    CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
      EXPORTING
        documentheader = ls_header
      IMPORTING
        obj_type       = lv_objtype
        obj_key        = lv_objkey
        obj_sys        = lv_objsys
      TABLES
        accountgl      = lt_glacc
        accountpayable = lt_payable
        currencyamount = lt_curr
        return         = lt_return.
  ENDIF.

  LOOP AT lt_return INTO ls_return.
    WRITE: / ls_return-type, ls_return-id, ls_return-number, ls_return-message.
    IF ls_return-type = 'E' OR ls_return-type = 'A'.
      lv_error = 'X'.
    ENDIF.
  ENDLOOP.

  IF '{{mode}}' = 'POST'.
    IF lv_error IS INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
* OBJ_KEY = document number (10) + company code (4) + fiscal year (4)
      WRITE: / 'Posted, object key:', lv_objkey.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    ENDIF.
  ENDIF.

**Notes:** Confirm in SE37 that BAPI_ACC_DOCUMENT_POST on your release uses DOCUMENTHEADER (BAPIACHE09), ACCOUNTGL (BAPIACGL09), ACCOUNTPAYABLE (BAPIACAP09) and CURRENCYAMOUNT (BAPIACCR09). Your field status group may require more fields (business area, profit center, assignment, baseline date). Use document type KR only with a vendor line. Run CHECK first in a test company code.

## Content

**Intro:** This generator builds an ABAP report for SAP ECC 6.0 that validates or posts a two-line FI document through BAPI_ACC_DOCUMENT_CHECK or BAPI_ACC_DOCUMENT_POST. It covers the two most requested cases: a G/L to G/L journal (SA) and a vendor invoice with an expense line on a cost center (KR).

**Sections:**

#### How to use it

**Body:** Start in CHECK mode. Fill company code, document type, dates in YYYYMMDD, currency, the expense G/L and cost center, the amount, and either a vendor number or an offset G/L. Paste the report into SE38 and run it: the list shows every RETURN message. When the check is clean, switch to POST, run again and note the object key. Account numbers and cost centers pass through CONVERSION_EXIT_ALPHA_INPUT so 476000 becomes 0000476000 as the BAPI expects. Dates must be in internal format YYYYMMDD, not the user date format.

**Bullets:**

- BUS_ACT 'RFBU' is the business transaction for FI postings from this BAPI.
- ITEMNO_ACC links each account line to its CURRENCYAMOUNT line.
- The document is saved only after BAPI_TRANSACTION_COMMIT.

#### Signs, amounts and the object key

**Body:** The BAPI has no debit or credit indicator. AMT_DOCCUR is signed: positive is a debit, negative is a credit, and all lines in CURRENCYAMOUNT must sum to zero per currency type. In the vendor invoice case line 1 debits the expense account with +1250.00 and line 2 credits the vendor with -1250.00. After a successful post, OBJ_TYPE is BKPFF and OBJ_KEY holds the document number, company code and fiscal year in one 18-character string, which you can split to open the document in FB03.

#### Clearing, extra fields and tax

**Body:** BAPI_ACC_DOCUMENT_POST creates open items but does not clear them, so a payment-with-clearing like F-32 or F-37 cannot be done with it. For clearing, the usual route is the POSTING_INTERFACE_START, POSTING_INTERFACE_CLEARING and POSTING_INTERFACE_END function modules, which drive the clearing transaction in batch-input style. For fields the BAPI structures lack, pass them in EXTENSION2 and move them in an implementation of BAdI ACC_DOCUMENT. For tax, add an ACCOUNTTAX line and a matching tax amount with AMT_BASE; the comment in the code shows where.

**Bullets:**

- Payment terms and baseline date go in BAPIACAP09 (PMNTTRMS, BLINE_DATE).
- A reconciliation-account override is not needed for a normal vendor line.

#### How to check the result

**Body:** Open the document in FB03 with the number from OBJ_KEY. BKPF holds the header (BLART, BUDAT, XBLNR), BSEG the lines. The vendor line shows in FBL1N as an open item and the expense line in FBL3N and in KSB1 for the cost center. If CHECK passes but POST or the commit fails, look first at document number assignment, which only the posting step performs, for example a missing number range interval for the document type and fiscal year in FBN1. The RETURN table lists every message, so read all of them, not just the first.

**Cta:** If your finance team wants to ask ECC about postings and open items in plain English on your own servers, with any posting going through BAPIs like this after review, we can set that up.

**Faq:**

#### Which SAP BAPI is used for invoice posting in FI?

**Answer:** For a direct FI vendor invoice (like FB60), BAPI_ACC_DOCUMENT_POST with document type KR, an ACCOUNTPAYABLE line and an ACCOUNTGL expense line. For a logistics invoice against a purchase order (like MIRO), use BAPI_INCOMINGINVOICE_CREATE instead, because it performs the PO and goods receipt matching that the accounting BAPI does not.

#### Can BAPI_ACC_DOCUMENT_POST post with clearing?

**Answer:** No. It posts open items but cannot clear existing ones. Clearing such as F-32, F-37 or an outgoing payment with clearing is usually done with POSTING_INTERFACE_START, POSTING_INTERFACE_CLEARING and POSTING_INTERFACE_END, which process the clearing transaction in the background. Test the field mapping on your release carefully.

#### What is the difference between BAPI_ACC_DOCUMENT_CHECK and BAPI_ACC_DOCUMENT_POST?

**Answer:** Both take the same header and line tables. CHECK runs the validations and returns messages without creating a document. POST also returns OBJ_TYPE, OBJ_KEY and OBJ_SYS and prepares the posting, which is written when you call BAPI_TRANSACTION_COMMIT. Some checks, such as document number assignment, only happen at POST.

#### Why do I get a balance error from BAPI_ACC_DOCUMENT_POST?

**Answer:** The CURRENCYAMOUNT lines do not net to zero, usually because both lines were entered as positive. Credits must be negative. Also check that every ITEMNO_ACC in ACCOUNTGL or ACCOUNTPAYABLE has exactly one CURRENCYAMOUNT line per currency type, and that tax lines are included in the total.

## Search terms

sap bapi accounting document posting, sap bapi for invoice posting, bapi_acc_document_post example, bapi_acc_document_check, sap bapi for posting data, sap bapi post with clearing, bapiache09 bus_act rfbu, bapi vendor invoice posting abap

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
