# BAPI_SALESORDER_CHANGE Example Generator for SAP ABAP

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

## Short answer

Generate a working ABAP program that updates an SAP sales order with BAPI_SALESORDER_CHANGE, with the correct X structures, schedule line handling, error check and commit or rollback.

## Category

developer

## Erp

SAP ECC 6.0

## Audience

- ABAP developer
- SAP SD consultant

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Report name

**Key:** report

**Type:** text

**Default:** ZSD_SO_CHANGE_BAPI

#### Sales order

**Key:** order

**Type:** text

**Default:** 0000012345

#### Item number

**Key:** item

**Type:** text

**Default:** 000010

#### Change type

**Key:** mode

**Type:** select

**Options:**

- QTY
- DATE
- REJ
- ADD
- PO

**Default:** QTY

**Help:** QTY quantity, DATE requested date, REJ reject item, ADD new item, PO header PO number

#### New quantity

**Key:** qty

**Type:** number

**Default:** 25

#### New requested date (YYYYMMDD)

**Key:** date

**Type:** text

**Default:** 20261115

#### Rejection reason (ABGRU)

**Key:** reason

**Type:** text

**Default:** Z1

#### Material for new item

**Key:** material

**Type:** text

**Default:** MAT-1001

#### Plant for new item

**Key:** plant

**Type:** text

**Default:** 1000

#### Customer PO number

**Key:** po_number

**Type:** text

**Default:** PO-77812

**Blocks:**

#### Program

**Body:** REPORT {{report}}.

PARAMETERS: p_vbeln TYPE vbak-vbeln          DEFAULT '{{order}}',
            p_posnr TYPE vbap-posnr          DEFAULT '{{item}}',
            p_mode  TYPE char4               DEFAULT '{{mode}}',
            p_qty   TYPE bapischdl-req_qty   DEFAULT '{{qty}}',
            p_date  TYPE sy-datum            DEFAULT '{{date}}',
            p_abgru TYPE vbap-abgru          DEFAULT '{{reason}}',
            p_matnr TYPE bapisditm-material  DEFAULT '{{material}}',
            p_werks TYPE bapisditm-plant     DEFAULT '{{plant}}',
            p_bstkd TYPE bapisdh1-purch_no_c DEFAULT '{{po_number}}'.

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.

**Notes:** 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).

## Content

**Intro:** 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.

**Sections:**

#### How the BAPI expects changes

**Body:** 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.

**Bullets:**

- Header: BAPISDH1 / BAPISDH1X.
- Items: BAPISDITM / BAPISDITMX.
- Schedule lines: BAPISCHDL / BAPISCHDLX with REQ_QTY and REQ_DATE.

#### How to use it

**Body:** 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

**Body:** 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

**Body:** 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.

**Cta:** 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.

**Faq:**

#### How do I change the sales order quantity with BAPI_SALESORDER_CHANGE?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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.

## Search terms

sap bapi update sales order, sap bapi bapi_salesorder_change, bapi_salesorder_change example, bapi_salesorder_change change quantity, bapi_salesorder_change reason for rejection, sap bapi commit

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