ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

Call a standard BAPI: create a sales order, read the RETURN table, then commit or roll back

The problem

An interface must create a standard sales order for a sold-to customer with one item.

How we solve it

  1. 1Fill the dataheader, item, partner
  2. 2Call the BAPICREATEFROMDAT2
  3. 3Commit or roll backread RETURN

The result

The order number is returned when it worked, otherwise the error text.

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

1. The requirement

An interface must create a standard sales order for a sold-to customer with one item. It must never leave a half-created order, and it must tell the caller the new order number or the error text.

2. How we design it

A BAPI is a standard function module that is safe to call from outside the screen. It does not commit by itself. You fill the header, the item and the partner tables, and for each table also an X-structure that says which fields you filled. After the call you read the RETURN table: if there is an error message (type E or A) you roll back, otherwise you commit.

Why we decided it this way

  1. 1Fill the X-structures as wellThe BAPI only takes the fields that are marked with an X in the matching INX structure. Without it your values are ignored.
  2. 2Check E and A in RETURNA BAPI does not raise exceptions for business errors. It reports them as messages in RETURN.
  3. 3Commit with WAITBAPI_TRANSACTION_COMMIT with wait = abap_true waits until the update is finished, so the order can be read in the next step.

Before you start

  • A test customer, material and sales area (VKORG, VTWEG, SPART) in a development or test system.
  • Order type OR (standard order) exists in your system.

3. The code, step by step

Step 1 of 3: Fill header, item and partner with their X-structures

All input structures are filled and marked.

ABAP · Class · ZCL_ORDER_CREATE (method create, part 1)
" Header and header X-structure
CLASS zcl_order_create DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    CLASS-METHODS create
      IMPORTING iv_customer     TYPE kunnr
                iv_material     TYPE matnr
                iv_quantity     TYPE kwmeng
      EXPORTING ev_order        TYPE vbeln_va
                ev_error_text   TYPE string.
ENDCLASS.

CLASS zcl_order_create IMPLEMENTATION.
  METHOD create.
    DATA: ls_header  TYPE bapisdhd1,
          ls_headerx TYPE bapisdhd1x,
          lt_items   TYPE STANDARD TABLE OF bapisditm WITH EMPTY KEY,
          lt_itemsx  TYPE STANDARD TABLE OF bapisditmx WITH EMPTY KEY,
          lt_partner TYPE STANDARD TABLE OF bapiparnr WITH EMPTY KEY,
          lt_return  TYPE STANDARD TABLE OF bapiret2 WITH EMPTY KEY.

    ls_header  = VALUE #( doc_type = 'OR' sales_org = '1000' distr_chan = '10' division = '00' ).
    ls_headerx = VALUE #( updateflag = 'I' doc_type = abap_true sales_org = abap_true
                          distr_chan = abap_true division = abap_true ).
    " Item, item X-structure and the sold-to partner

    lt_items  = VALUE #( ( itm_number = '000010' material = iv_material target_qty = iv_quantity ) ).
    lt_itemsx = VALUE #( ( itm_number = '000010' updateflag = 'I' material = abap_true target_qty = abap_true ) ).
    lt_partner = VALUE #( ( partn_role = 'AG'
                            partn_numb = |{ iv_customer ALPHA = IN }| ) ).
Part by part (plain text)
  1. Header and header X-structure. The header holds the order type and the sales area. In the INX structure every field you filled gets an X, and UPDATEFLAG = 'I' says 'insert a new document'.
  2. Item, item X-structure and the sold-to partner. ITM_NUMBER 000010 is the first item. The item X-structure repeats the item number and marks material and quantity. The partner table gets the sold-to party with role AG. The customer number is converted so it has its leading zeros.

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

The order number is returned when it worked, otherwise the error text.

What you should see

Created order 0000012345 or Error: Material FG-100 does not exist in plant 1000

Try it and check the result

  • Run in a test system with a real customer and material. Check the result in VA03 with the returned number.
  • Pass a material that does not exist: the error text comes back and no order exists.
  • In SE37 test the BAPI directly: the order is not saved until you also run BAPI_TRANSACTION_COMMIT in the same test sequence.

Common mistakes

  • Forgetting the commit means the order disappears at the end of your program. Forgetting to check RETURN means you commit an order that was not created.
  • Order numbers and some fields have conversion exits. Always convert customer, material and order numbers to the internal format first.
  • Test run mode and simulations do not call the save user exits (see SAP note 1938126), so a check built into a user exit is not tested that way.
  • Never call COMMIT WORK yourself between the BAPI call and the BAPI commit. Use the BAPI commit function.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-26. Release: ECC or S/4HANA on premise with SD. Customer, material and sales area values are examples; use data that exists in your system. Not for ABAP Cloud.

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.