ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

ABAP: call a BAPI, read the RETURN table, then commit or roll back

The problem

An interface program must create a sales order from incoming data.

How we solve it

  1. 1Fill the dataheader, items, partners
  2. 2Test runTESTRUN = X
  3. 3Real runcreate
  4. 4Commit or roll backBAPI_TRANSACTION_*

The result

With valid data the order is created and committed.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

An interface program must create a sales order from incoming data. It must never save a half-finished order, it must tell the user exactly what went wrong, and it must first be possible to run it as a test without saving anything.

2. How we design it

A BAPI does not save by itself. It prepares the document and fills the RETURN table with messages. We call it in test mode first. If RETURN has no error or abort message, we call it again for real and then call BAPI_TRANSACTION_COMMIT. If not, we call BAPI_TRANSACTION_ROLLBACK and show the messages. The header, items and partners come from the calling program.

Why we decided it this way

  1. 1Test run before the real runErrors in the data are found without creating a document.
  2. 2Check RETURN for types E and AA BAPI reports errors as messages, not as exceptions. Ignoring RETURN is the most common BAPI bug.
  3. 3Explicit commit or rollbackThe BAPI leaves the transaction open on purpose; the caller decides.

Before you start

  • An on-premise SAP system and a test client. Do not run this in a productive client.
  • Valid master data for your system: a sales document type (for example TA), a sales organization, distribution channel, division, a customer for roles AG and WE and a material. The values below are placeholders you must replace.

3. The code, step by step

Step 1 of 4: Fill header, items and partners

The data the BAPI needs.

ABAP · Report ZDEMO_BAPI_SO
" Structures and tables
REPORT zdemo_bapi_so.

DATA: ls_header   TYPE bapisdhd1,
      lt_items    TYPE STANDARD TABLE OF bapisditm WITH EMPTY KEY,
      lt_partners TYPE STANDARD TABLE OF bapiparnr WITH EMPTY KEY,
      lt_sched    TYPE STANDARD TABLE OF bapischdl WITH EMPTY KEY,
      lt_return   TYPE STANDARD TABLE OF bapiret2  WITH EMPTY KEY,
      lv_vbeln    TYPE bapivbeln-vbeln.

START-OF-SELECTION.
  ls_header = VALUE #( doc_type = 'TA' sales_org = '1000' distr_chan = '10' division = '00' ).
  lt_items = VALUE #( ( itm_number = '000010' material = 'TEST-MAT-1' target_qty = 5 ) ).
  lt_sched = VALUE #( ( itm_number = '000010' req_qty = 5 ) ).
  lt_partners = VALUE #( ( partn_role = 'AG' partn_numb = '0000100001' )
                         ( partn_role = 'WE' partn_numb = '0000100001' ) ).
Part by part (plain text)
  1. Structures and tables. The header holds document type and organizational data. Each item has an item number, a material and a target quantity; the order quantity itself is passed as a schedule line (REQ_QTY). The partners table names the sold-to (AG) and ship-to (WE) customer. Every value here is a placeholder: use values that exist in your system. Depending on your configuration the BAPI may require more fields, for example a plant or a schedule line.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

With valid data the order is created and committed. With wrong data nothing is saved and the error messages are listed.

What you should see

Valid data: 'Sales order 0000012345 created' (your number differs) and the order exists in VA03. Wrong data, for example an unknown material: the error messages from RETURN are printed and no order exists.

Try it and check the result

  • In a test client replace the placeholders with real values from your master data. Run the report: the test run passes and the order is created.
  • Change the material to a number that does not exist: the test run prints the error message from RETURN and stops. Nothing is created.
  • Open VA03 and display the printed order number to confirm it was saved.

Common mistakes

  • Never skip the RETURN check. A BAPI can finish without an exception and still have failed.
  • Without BAPI_TRANSACTION_COMMIT the order is lost at the end of the program.
  • Do not run a BAPI in test mode and then trust the order number. The test run does not create a document.
  • Required fields depend on your configuration. Use the messages in RETURN to find missing values, and compare with creating the same order in VA01.
  • When many orders are created in a loop, call BAPI_TRANSACTION_COMMIT (or ROLLBACK) after each BAPI call, so one failed order does not affect the others.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: Classic ABAP on SAP NetWeaver 7.40 SP08 or higher (ECC 6.0 EHP7+) and S/4HANA on-premise (SAP GUI). Not available in ABAP Cloud development. Check on your release.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.