Showing posts with label BAPI. Show all posts
Showing posts with label BAPI. Show all posts

Jul 3, 2026

A Aample Program Using BAPI_RESERVATION_CHANGE to Change Quantity and Storage Location

Storage location changes via this BAPI aren't as clean as quantity changes — SAP requires you to also populate MOVE_ITEM correctly or the storage location update silently gets ignored while quantity still updates fine. That's the part people usually miss and then spend an hour debugging why the reservation shows on the wrong bin/plant.

BAPI_RESERVATION_CHANGE requires you to explicitly flag every field you're changing in ITEMCONTROL — omitting the flag means the value in ITEMDATA is ignored even if populated.

For storage location changes specifically, you need MOVEMENT_ALLOWED context (goods movement not yet posted against that item) — if GI already happened against the reservation item, the BAPI will reject the storage location change via RETURN.

abap
REPORT zbapi_reservation_change.

DATA: lv_rsnum   TYPE rsnum VALUE '0000001234',  " your reservation number
      lv_rspos   TYPE rspos VALUE '0001'.        " reservation item number

DATA: lt_itemdata    TYPE STANDARD TABLE OF bapi2093_res_item,
      lt_itemcontrol TYPE STANDARD TABLE OF bapi2093_res_item_ctrl,
      lt_return      TYPE STANDARD TABLE OF bapiret2.

DATA: ls_itemdata    TYPE bapi2093_res_item,
      ls_itemcontrol TYPE bapi2093_res_item_ctrl.

* --- Optional but recommended: read current item first ---
DATA ls_resb TYPE resb.
SELECT SINGLE * FROM resb
  INTO ls_resb
  WHERE rsnum = lv_rsnum
    AND rspos = lv_rspos.

IF sy-subrc <> 0.
  WRITE: / 'Reservation item not found'.
  RETURN.
ENDIF.

* --- Fill ITEMDATA: the new values ---
ls_itemdata-res_item  = lv_rspos.
ls_itemdata-material   = ls_resb-matnr.
ls_itemdata-plant      = ls_resb-werks.
ls_itemdata-stge_loc   = 'STG2'.        " new storage location
ls_itemdata-entry_qnt  = '50'.          " new quantity
ls_itemdata-entry_uom  = ls_resb-meins.
APPEND ls_itemdata TO lt_itemdata.

* --- Fill ITEMCONTROL: flags telling SAP which fields to actually apply ---
ls_itemcontrol-res_item = lv_rspos.
ls_itemcontrol-stge_loc = abap_true.    " flag: storage location changed
ls_itemcontrol-entry_qnt = abap_true.   " flag: quantity changed
APPEND ls_itemcontrol TO lt_itemcontrol.

* --- Call the BAPI ---
CALL FUNCTION 'BAPI_RESERVATION_CHANGE'
  EXPORTING
    reservation = lv_rsnum
  TABLES
    itemdata    = lt_itemdata
    itemcontrol = lt_itemcontrol
    return      = lt_return.

* --- Check for errors before committing ---
READ TABLE lt_return TRANSPORTING NO FIELDS
  WITH KEY type = 'E'.
IF sy-subrc = 0.
  LOOP AT lt_return INTO DATA(ls_return) WHERE type CA 'EA'.
    WRITE: / ls_return-message.
  ENDLOOP.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = abap_true.
  WRITE: / 'Reservation updated successfully'.
ENDIF.

 


Aug 16, 2024

How to Change Cost Center in Accounting Document

Changing the KOSTL (Cost Center) field in the BSEG table using a BAPI is not directly possible since BSEG is a line-item table and is managed by SAP standard transactions. The BSEG table is updated indirectly through various financial postings and cannot be directly modified using a standard BAPI.

Alternative Approach:

If you need to change the cost center in financial documents, the correct approach would involve reversing the original document and posting a new document with the correct cost center. However, if your goal is to correct or change the cost center without reversing and reposting the document, consider using the following methods:

1. Use BAPI for Document Reversal and Reposting

  • BAPI_ACC_DOCUMENT_REV_POST: This BAPI is used to reverse the original accounting document.
  • BAPI_ACC_DOCUMENT_POST: This BAPI allows you to repost the document with the correct cost center.

2. Use a Direct Update via a Custom ABAP Program (Not Recommended)

  • Directly updating the BSEG-KOSTL field is not advisable since it can lead to inconsistencies in the database and violates SAP’s data integrity principles.

3. Use Special Purpose BAPIs

  • In specific cases, you may use specialized BAPIs related to the type of posting, such as:
    • BAPI_ACC_GL_POSTING_REV_POST: If dealing with General Ledger documents.
    • BAPI_ACC_INVOICE_RECEIPT_POST: For invoices and vendor-related postings.

Conclusion

Direct modification of BSEG-KOSTL via a standard BAPI is not supported. The best approach involves reversing the original document and posting a corrected version using standard BAPIs, ensuring that data integrity is maintained within the SAP system.

Jan 22, 2016

Change Delivery Quantity Using Function Module BAPI_OUTB_DELIVERY_CHANGE

Codes:
REPORT zchangeqty.
* DO update
DATA gw_do_header  TYPE bapiobdlvhdrchg.
DATA gw_do_ctrl    TYPE bapiobdlvhdrctrlchg.
DATA gv_delivery   TYPE vbeln_vl.
DATA gt_doitem     TYPE STANDARD TABLE OF bapiobdlvitemchg.
DATA gt_doitemctrl TYPE STANDARD TABLE OF bapiobdlvitemctrlchg.
DATA gt_doretn     TYPE STANDARD TABLE OF bapiret2.
DATA lw_doitem     TYPE bapiobdlvitemchg.
DATA lw_doitemctrl TYPE bapiobdlvitemctrlchg.
DATA lw_doretn     TYPE bapiret2.
DATA lw_lips       TYPE lips.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input  '80000349'
  IMPORTING
    output gv_delivery.

"header
gw_do_header-deliv_numb gv_delivery.

"header control
gw_do_ctrl-deliv_numb gv_delivery.

"items
CLEAR lw_doitem.
lw_doitem-deliv_numb gv_delivery.
lw_doitem-dlv_qty    '9'.      "<----new qty from 10 to 9
SELECT SINGLE FROM lips INTO lw_lips
  WHERE vbeln lw_doitem-deliv_numb.
IF sy-subrc 0.
  lw_doitem-fact_unit_nom   lw_lips-umvkz.
  lw_doitem-fact_unit_denom lw_lips-umvkn.
  lw_doitem-conv_fact       lw_lips-umrev.
  lw_doitem-deliv_item      lw_lips-posnr.
  lw_doitem-sales_unit      lw_lips-vrkme.
ENDIF.
APPEND lw_doitem TO gt_doitem.

"item control
lw_doitemctrl-deliv_numb gv_delivery.
lw_doitemctrl-deliv_item lw_doitem-deliv_item.
lw_doitemctrl-chg_delqty 'X'.
APPEND lw_doitemctrl TO gt_doitemctrl.

CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
  EXPORTING
    header_data    gw_do_header
    header_control gw_do_ctrl
    delivery       gv_delivery
  TABLES
    item_data      gt_doitem
    item_control   gt_doitemctrl
    return         gt_doretn.

READ TABLE gt_doretn INTO lw_doretn WITH KEY type 'E'.
IF sy-subrc 0.
  "Error handling
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait 'X'.
ENDIF.


Before:















After:












Jan 8, 2015

Running More Than One Function Module Using SE37

When we are testing BAPI, we need to run the BAPI Transaction Commit as well.
We can do that simultaneously using SE37. This is how we do it.

For example, we are going to run BAPI_GOODSMVT_CREATE (Create Goods Movement) and BAPI_TRANSACTION_COMMIT (Commit Transaction)

Step 1: Select Function Module > Execute > Test Sequences



Step 2: Fill in all the Function Modules



Step 3: Execute







 The result:


Aug 8, 2012

Pricing Conditions Upload with BAPI

Below is sample code to simulate Pricing Conditions upload using BAPI


The structure of condition table A073
MANDT MANDT CLNT 3 0 Client
KAPPL KAPPL CHAR 2 0 Application
KSCHL KSCHA CHAR 4 0 Condition type
VKORG VKORG CHAR 4 0 Sales Organization
VTWEG VTWEG CHAR 2 0 Distribution Channel
MATNR MATNR CHAR 18 0 Article Number
VRKME VRKME UNIT 3 0 Sales unit
DATBI KODATBI DATS 8 0 Validity end date of the condition record
DATAB KODATAB DATS 8 0 Validity start date of the condition record
KNUMH KNUMH CHAR 10 0 Condition record number


*&---------------------------------------------------------------------*
*& Report  ZTES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ztes.

CONSTANTS: lc_kschl_vkp0 TYPE kscha VALUE 'VKP0'.

DATA: lv_datum       TYPE sydatum,
lv_count       TYPE kopos,
lv_unit        TYPE kpein,
ls_bapicondct  TYPE bapicondct,
ls_bapicondhd  TYPE bapicondhd,
ls_bapicondit  TYPE bapicondit,
lt_bapicondct  TYPE STANDARD TABLE OF bapicondct,
lt_bapicondhd  TYPE STANDARD TABLE OF bapicondhd,
lt_bapicondit  TYPE STANDARD TABLE OF bapicondit,
lt_bapicondqs  TYPE STANDARD TABLE OF bapicondqs,
lt_bapicondvs  TYPE STANDARD TABLE OF bapicondvs,
lt_bapiret2    TYPE STANDARD TABLE OF bapiret2,
ls_bapiret2    TYPE bapiret2,
lt_bapiknumhs  TYPE STANDARD TABLE OF bapiknumhs,
lt_mem_initial TYPE STANDARD TABLE OF cnd_mem_initial.

break dc_idenny.
lv_datum = '20120812'"sy-datum.
lv_count = 1.

*LOOP AT gt_vbap INTO wa_vbap.

* BAPI Structure for Condition Tables
ls_bapicondct-operation  = '009'.
ls_bapicondct-table_no   = '073'.
ls_bapicondct-applicatio = 'V'.
ls_bapicondct-cond_usage = 'A'.
ls_bapicondct-cond_type  = lc_kschl_vkp0.

"Set the varkey from A700 table key fields
DATA lw_a073 LIKE a073.
lw_a073-VKORG = '1001'.
lw_a073-VTWEG = '03'.
lw_a073-MATNR = '000000100000072002'.
lw_a073-VRKME = 'AA1'.
CONCATENATE lw_a073-vkorg lw_a073-vtweg lw_a073-matnr lw_a073-vrkme
       INTO ls_bapicondct-varkey.
*  ls_bapicondct-varkey = space.

ls_bapicondct-valid_to   = '99991231'.
ls_bapicondct-valid_from = lv_datum.
ls_bapicondct-cond_no    = '$000000001'.

**** BAPI Structure of KONH with English Field Names
ls_bapicondhd-operation  = '009'.
ls_bapicondhd-cond_no    = '$000000001'.
ls_bapicondhd-created_by = sy-uname.
ls_bapicondhd-creat_date = sy-datum.
ls_bapicondhd-cond_usage = 'A'.
ls_bapicondhd-table_no   = '073'.
ls_bapicondhd-applicatio = 'V'.
ls_bapicondhd-cond_type  = lc_kschl_vkp0.
ls_bapicondhd-varkey     = ls_bapicondct-varkey.
ls_bapicondhd-valid_to   = '99991231'.
ls_bapicondhd-valid_from = lv_datum.

*** BAPI Structure of KONP with English Field Names
CLEAR ls_bapicondit.
ls_bapicondit-operation  = '009'.
ls_bapicondit-cond_no    = '$000000001'.
ls_bapicondit-cond_count = lv_count.
ls_bapicondit-applicatio = 'V'.
ls_bapicondit-cond_type  = lc_kschl_vkp0.
ls_bapicondit-scaletype  = 'A'.
ls_bapicondit-scalebasin = 'B'.
ADD 1 TO ls_bapicondit-scale_qty.
ls_bapicondit-calctypcon = 'B'.
lv_unit = '1'"wa_vbap-kwmeng.
ls_bapicondit-cond_p_unt = lv_unit.
ls_bapicondit-cond_value = '1.14'"wa_vbap-value.
ls_bapicondit-condcurr   = 'RM'.   "wa_vbap-waerk.

APPEND: ls_bapicondct TO lt_bapicondct,
ls_bapicondhd TO lt_bapicondhd,
ls_bapicondit TO lt_bapicondit.

*ENDLOOP.

*** BAPI for pricing Condition Records
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
  TABLES
    ti_bapicondct  = lt_bapicondct
    ti_bapicondhd  = lt_bapicondhd
    ti_bapicondit  = lt_bapicondit
    ti_bapicondqs  = lt_bapicondqs
    ti_bapicondvs  = lt_bapicondvs
    to_bapiret2    = lt_bapiret2
    to_bapiknumhs  = lt_bapiknumhs
    to_mem_initial = lt_mem_initial
  EXCEPTIONS
    update_error   = 1
    OTHERS         = 2.

IF sy-subrc EQ 0.

  WRITE: /1 'Return Messages for Condition create'(t03).
  LOOP AT lt_bapiret2 INTO ls_bapiret2.
    WRITE: /1 ls_bapiret2-message.
  ENDLOOP.
  ULINE.

  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait   = 'X'
    IMPORTING
      return = ls_bapiret2.
ENDIF.