BAPI in SAP ABAP

In this article, we are going to see BAPI in SAP ABAP | BAPI Related Notes in SAP ABAP | BAPI with Example in SAP ABAP | BAPI Step by Step Guide for Beginners so you will come to know all the information in below blog post.

BAPI (Business Application Programming Interface) is a standardized programming interface in SAP that enables external systems to integrate and interact with SAP applications. BAPIs are object-oriented methods that allow developers to access business processes and data in SAP systems without directly modifying the underlying SAP tables.

BAPIs are part of the SAP Business Object Repository (BOR) and are based on SAP’s Business Object technology. They provide a stable and well-documented way to interact with SAP functionalities, making them essential for SAP integration scenarios.

Key Features of BAPI

  1. Standardized Interface
    • BAPIs follow strict SAP standards, ensuring consistency and reliability.
    • They are released by SAP, meaning they are backward-compatible and supported in future releases.
  2. Remote-Enabled
    • BAPIs can be called remotely using RFC (Remote Function Call), making them ideal for integrating SAP with non-SAP systems.
  3. Business Object-Oriented
    • Each BAPI is associated with a Business Object (e.g., CustomerSalesOrderMaterial), ensuring logical grouping of related functions.
  4. Transaction Support
    • Some BAPIs support transactions (BAPI_TRANSACTION_COMMIT and BAPI_TRANSACTION_ROLLBACK), allowing multiple operations to be executed as a single unit.
  5. Error Handling
    • BAPIs return structured messages (RETURN parameter) to indicate success or failure.

Types of BAPIs

  1. Instance BAPIs
    • Operate on a specific instance of a Business Object (e.g., GetDetail for a particular sales order).
  2. Static BAPIs
    • Not tied to a specific instance (e.g., GetList to retrieve all customers).
  3. Factory BAPIs
    • Used to create new instances of Business Objects (e.g., Create for a new purchase order).
  4. Query BAPIs
    • Retrieve data based on filter conditions (e.g., Replicate for copying data).

How to Use BAPIs in ABAP

ABAP Program Using BAPI_SALESORDER_CHANGE to Modify a Sales Order

REPORT z_update_sales_order_bapi.

* Data Declarations
DATA: lv_sales_doc     TYPE bapivbeln-vbeln VALUE '0000123456',  " Sales Order
      ls_header_in     TYPE bapisdh1,
      lt_item_in       TYPE TABLE OF bapisditm,
      ls_item_in       TYPE bapisditm,
      lt_schedule_in   TYPE TABLE OF bapischdl,
      ls_schedule_in   TYPE bapischdl,
      lt_partner_in    TYPE TABLE OF bapiparnr,
      ls_partner_in    TYPE bapiparnr,
      lt_return        TYPE TABLE OF bapiret2,
      lt_header_inx    TYPE TABLE OF bapisdh1x,
      ls_header_inx    TYPE bapisdh1x,
      lt_item_inx      TYPE TABLE OF bapisditmx,
      ls_item_inx      TYPE bapisditmx,
      lt_schedule_inx  TYPE TABLE OF bapischdlx,
      ls_schedule_inx  TYPE bapischdlx.

* 1. Prepare Header Data (Optional changes)
ls_header_in-doc_type = 'OR'.  " Order type
APPEND ls_header_in TO lt_header_in.

* Header Change Indicator Structure (X = Change)
ls_header_inx-updateflag = 'U'.  " Update
ls_header_inx-doc_type   = 'X'.  " X means this field will be updated
APPEND ls_header_inx TO lt_header_inx.

* 2. Prepare Item Data (Change quantity)
ls_item_in-itm_number = '000010'.    " Item to change
ls_item_in-material   = 'MAT-001'.  " Optional: material number
ls_item_in-target_qty = '50'.       " New quantity
APPEND ls_item_in TO lt_item_in.

* Item Change Indicator Structure
ls_item_inx-itm_number = '000010'.
ls_item_inx-updateflag = 'U'.       " Update
ls_item_inx-material   = 'X'.       " Change material if provided
ls_item_inx-target_qty = 'X'.       " Change quantity
APPEND ls_item_inx TO lt_item_inx.

* 3. Prepare Schedule Line Data (Change delivery date)
ls_schedule_in-itm_number = '000010'.
ls_schedule_in-sched_line = '0001'.
ls_schedule_in-req_date   = sy-datum + 30.  " New delivery date
APPEND ls_schedule_in TO lt_schedule_in.

* Schedule Line Change Indicator Structure
ls_schedule_inx-itm_number = '000010'.
ls_schedule_inx-sched_line = '0001'.
ls_schedule_inx-updateflag = 'U'.
ls_schedule_inx-req_date   = 'X'.
APPEND ls_schedule_inx TO lt_schedule_inx.

* 4. Prepare Partner Data (Optional: Change sold-to party)
ls_partner_in-partn_role = 'AG'.    " Sold-to party
ls_partner_in-partn_numb = '1001'.  " New partner number
APPEND ls_partner_in TO lt_partner_in.

* Call BAPI to update sales order
CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
  EXPORTING
    salesdocument    = lv_sales_doc
  TABLES
    return           = lt_return
    order_header_in  = lt_header_in
    order_header_inx = lt_header_inx
    order_item_in    = lt_item_in
    order_item_inx   = lt_item_inx
    schedule_lines   = lt_schedule_in
    schedule_linesx  = lt_schedule_inx
    partnerchanges   = lt_partner_in.

* Check for errors
READ TABLE lt_return WITH KEY type = 'E' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
  WRITE: / 'Error occurred while updating sales order:', lv_sales_doc.
  LOOP AT lt_return INTO DATA(ls_error) WHERE type CA 'EAX'.
    WRITE: / ls_error-type, ls_error-id, ls_error-number, ls_error-message.
  ENDLOOP.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  WRITE: / 'Changes rolled back due to errors'.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = 'X'.
  WRITE: / 'Sales order', lv_sales_doc, 'updated successfully.'.
ENDIF.

Explanation of Key Components

  1. BAPI_SALESORDER_CHANGE Parameters:
    • salesdocument: The sales order number to be modified.
    • order_header_in: Used to update header-level data (e.g., order type).
    • order_item_in: Used to modify item details (e.g., quantity, material).
    • schedule_lines: Used to adjust schedule lines (e.g., delivery date).
    • partnerchanges: Used to update partner assignments (e.g., sold-to party).
  2. Error Handling:
    • The RETURN table contains success/error messages.
    • If errors occur, the program rolls back changes using BAPI_TRANSACTION_ROLLBACK.
    • If successful, it commits changes using BAPI_TRANSACTION_COMMIT.
  3. Modifications Supported:
    • Change item quantity (target_qty).
    • Update delivery date (req_date).
    • Change partner details (partn_rolepartn_numb).

How to Test the Program

  1. Replace lv_salesorder with a valid sales order number.
  2. Adjust item number (itm_number) to match an existing line item.
  3. Run the program in SE38 or SA38.
  4. Check VA02 (Sales Order Display) to check the validity.

Leave a Comment