BAPI_SALESORDER_CHANGE Example Generator for SAP ABAP
This generator writes an ABAP program for SAP ECC 6.0 that changes a sales order with BAPI_SALESORDER_CHANGE: quantity or requested date through the schedule line, rejection of an item, a new item, or the customer PO number in the header. It fills the data and X structures correctly and commits only when the return table has no errors.
Fill in
QTY quantity, DATE requested date, REJ reject item, ADD new item, PO header PO number
Program
REPORT ZSD_SO_CHANGE_BAPI.
PARAMETERS: p_vbeln TYPE vbak-vbeln DEFAULT '0000012345',
p_posnr TYPE vbap-posnr DEFAULT '000010',
p_mode TYPE char4 DEFAULT 'QTY',
p_qty TYPE bapischdl-req_qty DEFAULT '25',
p_date TYPE sy-datum DEFAULT '20261115',
p_abgru TYPE vbap-abgru DEFAULT 'Z1',
p_matnr TYPE bapisditm-material DEFAULT 'MAT-1001',
p_werks TYPE bapisditm-plant DEFAULT '1000',
p_bstkd TYPE bapisdh1-purch_no_c DEFAULT 'PO-77812'.
DATA: lv_vbeln TYPE vbak-vbeln,
ls_hdr TYPE bapisdh1,
ls_hdrx TYPE bapisdh1x,
lt_item TYPE STANDARD TABLE OF bapisditm,
ls_item TYPE bapisditm,
lt_itemx TYPE STANDARD TABLE OF bapisditmx,
ls_itemx TYPE bapisditmx,
lt_sch TYPE STANDARD TABLE OF bapischdl,
ls_sch TYPE bapischdl,
lt_schx TYPE STANDARD TABLE OF bapischdlx,
ls_schx TYPE bapischdlx,
lt_return TYPE STANDARD TABLE OF bapiret2,
ls_return TYPE bapiret2,
lv_error TYPE c.
START-OF-SELECTION.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = p_vbeln
IMPORTING
output = lv_vbeln.
ls_hdrx-updateflag = 'U'.
CASE p_mode.
WHEN 'QTY'.
* Order quantity is changed on the schedule line, not only TARGET_QTY
ls_item-itm_number = p_posnr.
APPEND ls_item TO lt_item.
ls_itemx-itm_number = p_posnr.
ls_itemx-updateflag = 'U'.
APPEND ls_itemx TO lt_itemx.
ls_sch-itm_number = p_posnr.
ls_sch-sched_line = '0001'.
ls_sch-req_qty = p_qty.
APPEND ls_sch TO lt_sch.
ls_schx-itm_number = p_posnr.
ls_schx-sched_line = '0001'.
ls_schx-updateflag = 'U'.
ls_schx-req_qty = 'X'.
APPEND ls_schx TO lt_schx.
WHEN 'DATE'.
ls_item-itm_number = p_posnr.
APPEND ls_item TO lt_item.
ls_itemx-itm_number = p_posnr.
ls_itemx-updateflag = 'U'.
APPEND ls_itemx TO lt_itemx.
ls_sch-itm_number = p_posnr.
ls_sch-sched_line = '0001'.
ls_sch-req_date = p_date.
APPEND ls_sch TO lt_sch.
ls_schx-itm_number = p_posnr.
ls_schx-sched_line = '0001'.
ls_schx-updateflag = 'U'.
ls_schx-req_date = 'X'.
APPEND ls_schx TO lt_schx.
WHEN 'REJ'.
ls_item-itm_number = p_posnr.
ls_item-reason_rej = p_abgru.
APPEND ls_item TO lt_item.
ls_itemx-itm_number = p_posnr.
ls_itemx-updateflag = 'U'.
ls_itemx-reason_rej = 'X'.
APPEND ls_itemx TO lt_itemx.
WHEN 'ADD'.
ls_item-itm_number = p_posnr.
ls_item-material = p_matnr.
ls_item-plant = p_werks.
APPEND ls_item TO lt_item.
ls_itemx-itm_number = p_posnr.
ls_itemx-updateflag = 'I'.
ls_itemx-material = 'X'.
ls_itemx-plant = 'X'.
APPEND ls_itemx TO lt_itemx.
ls_sch-itm_number = p_posnr.
ls_sch-sched_line = '0001'.
ls_sch-req_qty = p_qty.
ls_sch-req_date = p_date.
APPEND ls_sch TO lt_sch.
ls_schx-itm_number = p_posnr.
ls_schx-sched_line = '0001'.
ls_schx-updateflag = 'I'.
ls_schx-req_qty = 'X'.
ls_schx-req_date = 'X'.
APPEND ls_schx TO lt_schx.
WHEN 'PO'.
ls_hdr-purch_no_c = p_bstkd.
ls_hdrx-purch_no_c = 'X'.
WHEN OTHERS.
MESSAGE 'Unknown change type' TYPE 'E'.
ENDCASE.
CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
EXPORTING
salesdocument = lv_vbeln
order_header_in = ls_hdr
order_header_inx = ls_hdrx
TABLES
return = lt_return
order_item_in = lt_item
order_item_inx = lt_itemx
schedule_lines = lt_sch
schedule_linesx = lt_schx.
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 lv_error = 'X'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
WRITE: / 'Changes rolled back'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_return.
WRITE: / 'Sales order', lv_vbeln, 'saved'.
ENDIF.Verify in SE37 and SE11 that BAPISDH1(X), BAPISDITM(X) and BAPISCHDL(X) carry the fields used on your release. The code changes schedule line 0001; for items with several schedule lines, read VBEP first and pass the right SCHED_LINE. The rejection reason must exist in TVAG (customising). A test run is possible with the SIMULATION parameter set to 'X' (the BAPI then does not save).
Get a mass-change program reviewed
Send us your change scenario and we return a reviewed program that changes many orders from a file with logging and a test mode.
Optional. The tool and every export work without this. No spam.
How the BAPI expects changes
Every data structure has a matching X structure. ORDER_HEADER_INX needs UPDATEFLAG = U for any change, even if only items change. Each item in ORDER_ITEM_IN needs a line in ORDER_ITEM_INX with ITM_NUMBER and UPDATEFLAG (U change, I insert, D delete) and an X in each field you want to change. Schedule lines work the same way through SCHEDULE_LINES and SCHEDULE_LINESX with ITM_NUMBER and SCHED_LINE. Fields without an X are ignored, which is the most common reason a change seems to do nothing.
- Header: BAPISDH1 / BAPISDH1X.
- Items: BAPISDITM / BAPISDITMX.
- Schedule lines: BAPISCHDL / BAPISCHDLX with REQ_QTY and REQ_DATE.
How to use it
Enter the order, item and change type, plus only the values that type needs. QTY and DATE change schedule line 0001, REJ sets the reason for rejection, ADD inserts a new item with one schedule line, and PO changes the customer PO number in the header. Copy the program into SE38 and run it. The return messages are listed; on any E or A message the program rolls back, otherwise it commits with WAIT so the change is saved before the program ends.
Worked example
Order 12345, item 000010, mode QTY, quantity 25. The program sends the item with UPDATEFLAG U and schedule line 0001 with REQ_QTY 25 and REQ_QTY marked X. The return table typically contains a success message that the order was saved and possibly warnings from availability check. After the commit, VA03 shows 25 on the item and VBEP-WMENG for schedule line 0001 holds the new requested quantity; confirmed quantity follows the ATP result.
Gotchas
Changing TARGET_QTY on the item alone does not change the order quantity of a standard order; the quantity lives on the schedule line. The order number must have leading zeros, hence the ALPHA conversion. If the order is locked in VA02 by someone else, the BAPI returns an error and nothing is saved. A success message in RETURN does not mean the data is on the database until BAPI_TRANSACTION_COMMIT has run. Partner and condition changes need PARTNERCHANGES and CONDITIONS_IN with their X tables.
Frequently Asked Questions
How do I change the sales order quantity with BAPI_SALESORDER_CHANGE?
Pass the item in ORDER_ITEM_IN and ORDER_ITEM_INX with UPDATEFLAG U, and the schedule line in SCHEDULE_LINES with REQ_QTY plus SCHEDULE_LINESX with SCHED_LINE, UPDATEFLAG U and REQ_QTY = X. Then call BAPI_TRANSACTION_COMMIT. Changing only TARGET_QTY usually has no effect on a standard sales order.
Why do I need BAPI_TRANSACTION_COMMIT after the BAPI?
BAPIs do not commit themselves so that several calls can form one logical unit. BAPI_SALESORDER_CHANGE only registers the update; BAPI_TRANSACTION_COMMIT triggers the update task, and WAIT = X makes the program wait until it finishes. If RETURN has E or A messages, call BAPI_TRANSACTION_ROLLBACK instead.
How do I reject a sales order item with the BAPI?
Set REASON_REJ in ORDER_ITEM_IN to a rejection reason from customising (table TVAG) and set REASON_REJ = X and UPDATEFLAG = U in ORDER_ITEM_INX for that ITM_NUMBER. To remove the rejection, send REASON_REJ blank with the X flag set.
Which BAPI reads sales order details?
BAPISDORDER_GETDETAILEDLIST returns header, items, schedule lines, partners and conditions for a list of orders, controlled by the I_BAPI_VIEW flags. Read the current schedule lines with it before a change if items can have more than one schedule line.
If you want customer service to ask about and, with approval, change sales orders in plain English on your own servers, we can build that over your ECC system.
Related Tools
SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
Fill in order type, sales area, customer, material and quantity to get an ABAP skeleton that creates a sales order through the standard BAPI.
Developer ToolsSAP ECC BAPI_PO_CHANGE ABAP Generator
Generate a working ABAP program that changes an SAP purchase order item with BAPI_PO_CHANGE, with POITEM/POITEMX, schedule line date conversion, test run and commit handling.
SAPSAP MM and QM BAPI and API List Reference
A searchable list of the common SAP MM and QM BAPIs and S/4HANA OData APIs, what each does, and whether it needs a commit.
More Developer Tools tools
See all- SAP ECC Enhancement Transactions and Exit Tables Reference (SMOD, CMOD, SE18, MODSAP, SXC_EXIT)
- SAP ECC Data Dictionary and Repository Tables Reference (DD02L, DD03L, T100, E070, TSTC)
- SAP ECC ABAP Runtime Errors (ST22 Dumps) and Error Log Transactions Reference
- SAP ECC Background Job History ABAP Query Generator (TBTCO + TBTCP)
- SAP ECC Background Job Creation ABAP Generator (JOB_OPEN, SUBMIT, JOB_CLOSE)
- SAP ECC IDoc Segment Data Reader ABAP Generator (EDIDC, EDID4, EDIDS)
- SAP ECC Custom Inbound IDoc Function Module Skeleton Generator
- SAP ECC BAPI_MATERIAL_SAVEDATA Material Creation ABAP Generator
- SAP ECC BAPI_ACC_DOCUMENT_POST G/L and Vendor Posting ABAP Generator
Keep exploring
Free tools
- SAP ECC BAPI_PO_CHANGE ABAP GeneratorGenerate a working ABAP program that changes an SAP purchase order item with BAPI_PO_CHANGE, with POITEM/POITEMX, schedule line date conversion, test run and commit handling.
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)Fill in order type, sales area, customer, material and quantity to get an ABAP skeleton that creates a sales order through the standard BAPI.
- ABAP BAPI Return Handling and Commit GeneratorWrap any BAPI call with BAPIRET2 error checking, message text building and a safe commit or rollback.
ERP fixes and how-tos
- SAP MM BAPI for PO Create and Change: BAPI_PO_CREATE1 and BAPI_PO_CHANGEHow to use BAPI_PO_CREATE1 and BAPI_PO_CHANGE: header, item and schedule structures, the X-structures, error handling in RETURN and the commit step.
- How to Use BAPI_TRANSACTION_COMMIT CorrectlyBAPIs never commit their own update. Here is when to call BAPI_TRANSACTION_COMMIT with WAIT, and why a BAPI can run fine and still save nothing.
- How to add AI to SAP ECC 6.0 on-prem without moving data to a cloudReference architecture for a private on-prem AI assistant on SAP ECC 6.0: RFC and BAPI reads, SLT replica, retrieval index, local LLM, permission mirroring.
Integrations
- How to automate email into SAP ECC 6.0Turn inbound emails into SAP ECC 6.0 transactions: customer POs into sales orders, supplier confirmations, RFQs and invoices, with review and BAPI posting.
- How to add AI agents to SAP ECC 6.0How to add AI agents to SAP ECC 6.0: a tool layer over BAPIs, RFC and IDocs, human approval before any posting, and an on-prem model that never leaves your network.