Developer ToolsFree generatorPDF, Excel, Word exportSAP ECC / S/4HANA

BAPI_SALESORDER_CREATEFROMDAT2 ABAP Code Generator

Integrations from web shops, EDI or other systems often create SAP sales orders through BAPI_SALESORDER_CREATEFROMDAT2. This generator writes the skeleton, including the X structures, partners, schedule line, return check and commit.

Fill in

Generated code
Download:

BAPI call

DATA: ls_header   TYPE bapisdhd1,
      ls_headerx  TYPE bapisdhd1x,
      lt_items    TYPE STANDARD TABLE OF bapisditm,
      ls_item     TYPE bapisditm,
      lt_itemsx   TYPE STANDARD TABLE OF bapisditmx,
      ls_itemx    TYPE bapisditmx,
      lt_partner  TYPE STANDARD TABLE OF bapiparnr,
      ls_partner  TYPE bapiparnr,
      lt_sched    TYPE STANDARD TABLE OF bapischdl,
      ls_sched    TYPE bapischdl,
      lt_schedx   TYPE STANDARD TABLE OF bapischdlx,
      ls_schedx   TYPE bapischdlx,
      lt_return   TYPE STANDARD TABLE OF bapiret2,
      ls_return   TYPE bapiret2,
      lv_vbeln    TYPE bapivbeln-vbeln.

* Header
ls_header-doc_type   = 'OR'.
ls_header-sales_org  = '1000'.
ls_header-distr_chan = '10'.
ls_header-division   = '00'.
ls_header-purch_no_c = 'PO-2026-0451'.
ls_headerx-updateflag  = 'I'.
ls_headerx-doc_type    = 'X'.
ls_headerx-sales_org   = 'X'.
ls_headerx-distr_chan  = 'X'.
ls_headerx-division    = 'X'.
ls_headerx-purch_no_c  = 'X'.

* Partners (customer numbers need leading zeros)
ls_partner-partn_role = 'AG'.
ls_partner-partn_numb = '100001'.
APPEND ls_partner TO lt_partner.
ls_partner-partn_role = 'WE'.
ls_partner-partn_numb = '100001'.
APPEND ls_partner TO lt_partner.

* Item
ls_item-itm_number = '000010'.
ls_item-material   = 'FG-1001'.
ls_item-target_qty = 10.
ls_item-target_qu  = 'EA'.
APPEND ls_item TO lt_items.
ls_itemx-itm_number = '000010'.
ls_itemx-updateflag = 'I'.
ls_itemx-material   = 'X'.
ls_itemx-target_qty = 'X'.
ls_itemx-target_qu  = 'X'.
APPEND ls_itemx TO lt_itemsx.

* Schedule line
ls_sched-itm_number = '000010'.
ls_sched-req_qty    = 10.
ls_sched-req_date   = '20261130'.
APPEND ls_sched TO lt_sched.
ls_schedx-itm_number = '000010'.
ls_schedx-updateflag = 'I'.
ls_schedx-req_qty    = 'X'.
ls_schedx-req_date   = 'X'.
APPEND ls_schedx TO lt_schedx.

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
  EXPORTING
    order_header_in     = ls_header
    order_header_inx    = ls_headerx
  IMPORTING
    salesdocument       = lv_vbeln
  TABLES
    return              = lt_return
    order_items_in      = lt_items
    order_items_inx     = lt_itemsx
    order_partners      = lt_partner
    order_schedules_in  = lt_sched
    order_schedules_inx = lt_schedx.

Check the return table and commit

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

IF sy-subrc = 0.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = 'X'.
  WRITE: / 'Sales order created:', lv_vbeln.
ENDIF.

Test in a sandbox client first. Convert customer numbers with CONVERSION_EXIT_ALPHA_INPUT and material numbers with the conversion exit for your release. Pricing, output and credit checks run as in VA01, so the order can still be blocked. Verify structure names and fields in SE37 and SE11 on your release.

Order integration design review

Optional: a short call on the error handling and approval design of an order integration.

Optional. The tool and every export work without this. No spam.

How to use the generator

Fill the fields with your organization data and one test item, then copy the code into an ABAP program or a function module wrapper. Replace the single item block with a loop if you create multiple items, incrementing the item number in steps of ten. Read the return table every time, because the BAPI reports problems as messages and not as exceptions.

The X structures

The BAPI uses a changing flag pattern. Every value field in the data structure has a matching field in the X structure, and you set it to X for each field you pass, with UPDATEFLAG set to I on create. Forgetting the X flag is the most common reason a value you filled is silently ignored. The same pattern applies to items and schedule lines.

Commit and error handling

The BAPI does not commit by itself. You must call BAPI_TRANSACTION_COMMIT, with WAIT set to X if you read the order straight after, or roll back when the return table has an error or abort message. Message type W can be acceptable, but log it. For high volume, test locking and performance, and think about idempotency with the customer PO number so a retry does not create a duplicate.

  • Check type E and A messages
  • Commit with WAIT = X
  • Convert numbers with exits
  • Use the customer PO for duplicate checks

Frequently Asked Questions

Which BAPI creates a sales order in SAP?

BAPI_SALESORDER_CREATEFROMDAT2 is the standard function module for creating a sales order. It takes header, item, partner and schedule line data, plus the matching X structures, and returns the new document number and a message table.

Why is my field ignored by the BAPI?

Most often the matching flag in the X structure was not set to X. Set UPDATEFLAG to I for creation and mark each filled field in the X structure. Also confirm the field exists in the structure on your release.

Do I need to call BAPI_TRANSACTION_COMMIT?

Yes. The create BAPI only prepares the update. Without BAPI_TRANSACTION_COMMIT the order is not saved. Use rollback if the return table has an error or abort message so locks and buffered data are released.

Is there a newer option for S/4HANA?

SAP offers OData and SOAP APIs such as the Sales Order (A2X) API on the SAP Business Accelerator Hub. They are the usual choice for new external integrations, while the BAPI remains common in ABAP-side code. Check your release.

If you are building an order integration into SAP and need it reviewed, with human approval on write-back, Netray can take a look at the design. Ask for a review.