Showing posts with label ABAP MM. Show all posts
Showing posts with label ABAP MM. 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.

 


Jun 1, 2026

Sample Program of Inbound IDoc MRESCR/MRESCR01

To process the MRESCR01 inbound IDoc (used to create material reservations), SAP typically uses the standard function module IDOC_INPUT_MRESCR or a BAPI-wrapper via process code MRES or BAPI in WE42.

Below is an ABAP program sample that reads a flat file from the application server and manually forces IDoc creation using IDOC_INBOUND_ASYNCHRONOUS
abap
*&---------------------------------------------------------------------*
*& Report Z_INBOUND_IDOC_MRESCR01
*&---------------------------------------------------------------------*
*& Sample program to generate and process MRESCR01 inbound IDoc
*&---------------------------------------------------------------------*
REPORT z_inbound_idoc_mrescr01.

* DATA DECLARATIONS
DATA: lv_file     TYPE string VALUE '/usr/sap/tmp/reservation_data.txt',
      lv_lines    TYPE i,
      ls_edidc    TYPE edidc,
      lt_edidd    TYPE TABLE OF edidd,
      ls_edidd    TYPE edidd,
      lt_control  TYPE TABLE OF edidc,
      lv_msg_text TYPE string.

* SELECTION SCREEN
PARAMETERS: p_file LIKE lgpa-filepath DEFAULT '/usr/sap/tmp/reservation_data.txt'.

START-OF-SELECTION.

  " 1. Read flat file from application server
  PERFORM read_flat_file CHANGING lt_edidd.

  " 2. Build Control Record
  PERFORM build_control_record CHANGING ls_edidc.

  " 3. Call Standard Inbound Function
  CALL FUNCTION 'IDOC_INBOUND_ASYNCHRONOUS'
    EXPORTING
      pi_edidc      = ls_edidc
    TABLES
      pit_edidd     = lt_edidd
    EXCEPTIONS
      error_message = 1
      OTHERS        = 2.

  IF sy-subrc = 0.
    COMMIT WORK.
    WRITE: / 'IDoc successfully created and triggered for processing.'.
  ELSE.
    lv_msg_text = sy-msgli.
    WRITE: / 'Error creating IDoc. Message:', lv_msg_text.
  ENDIF.

*&---------------------------------------------------------------------*
*& Form BUILD_CONTROL_RECORD
*&---------------------------------------------------------------------*
FORM build_control_record CHANGING cs_edidc TYPE edidc.

  cs_edidc-mestyp = 'MRESCR'.         " Message Type
  cs_edidc-idoctp = 'MRESCR01'.       " Basic IDoc Type
  cs_edidc-direct = '2'.              " Direction: 2 = Inbound
  cs_edidc-rcvprt = 'LS'.             " Partner Type: Logical System
  cs_edidc-rcvprn = 'SAPD01'.         " Receiver Partner Number (Your SAP system)
  cs_edidc-sndprt = 'LS'.             " Sender Partner Type
  cs_edidc-sndprn = 'LEGACY_SYS'.     " Sender Partner Number (Source System)

ENDFORM.

*&---------------------------------------------------------------------*
*& Form READ_FLAT_FILE
*&---------------------------------------------------------------------*
FORM read_flat_file CHANGING ct_edidd TYPE STANDARD TABLE of edidd.

  DATA: lv_string TYPE string,
        ls_header TYPE e1bpresbc,   " Standard BAPI segment for MRESCR01 header
        ls_item   TYPE e1bpresbc.   " Standard BAPI segment for item

  OPEN DATASET p_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc NE 0.
    WRITE: / 'Cannot open file:', p_file.
    EXIT.
  ENDIF.

  DO.
    READ DATASET p_file INTO lv_string.
    IF sy-subrc NE 0. EXIT. ENDIF.

    " Add file parsing logic here depending on your file structure
    " Example: Split CSV by comma and populate IDoc segments

    " Example for Header segment
    CLEAR ls_edidd.
    ls_edidd-segnam = 'E1BPRESBC'. " Segment Name
    ls_header-mov_type = '201'.
    ls_header-res_date = sy-datum.
    ls_edidd-sdata = ls_header.
    APPEND ls_edidd TO ct_edidd.

    " Example for Item segment
    CLEAR ls_edidd.
    ls_edidd-segnam = 'E1BPRESBC'.
    " Populate ls_item fields here ...
    ls_edidd-sdata = ls_item.
    APPEND ls_edidd TO ct_edidd.

  ENDDO.

  CLOSE DATASET p_file.

ENDFORM.

Next Steps for Implementation
  1. Define Message/IDoc Types: Ensure MRESCR (Message) and MRESCR01 (IDoc) are active in your system using transaction WE81 and WE30.
  2. Assign Inbound Function: Register your process code in WE42 (usually linked to IDOC_INPUT_MRESCR or BAPI_IDOC_INPUT1) so that the IDoc posts application data.
  3. Background Processing: If you set your Inbound Partner Profile (WE20) to collect IDocs instead of processing them immediately, you will need to schedule standard report RBDAPP01 to actually post the application data (status 64 to 53).

Mar 23, 2016

User-exit to verify Valuation Class in MM02

Requirement:
To put a verification/validation when Valuation Class in MM02 is modified/changed.
Check if the new Valuation Class is listed in table T023 for the same Material Group (MARA-MATKL). If not, issue error message.

Solution:
Using EXIT_SAPLMGMU_001. There is changing parameter structure CMARA in this FM which contains VOLUM field. you can change volume by changing this field. The project name is MGA00001 in SMOD transaction.

1. Transaction CMOD. Project name MGA00001


2. Choose Enhancement MGA00001. and click Components

3. Double click on EXIT_SAPLMGMU_001

4. Double click on Include ZXMG0U02

5. Write the Codes, saved and activate.


  DATA lv_t023_bklas TYPE t023-bklas.

  SELECT SINGLE bklas FROM t023 INTO lv_t023_bklas
    WHERE matkl = wmara-matkl.

  IF sy-subrc EQ 0.
    IF wmbew-bklas NE lv_t023_bklas.
      MESSAGE w398(00) WITH 'Invalid valuation class' wmbew-bklas space.
      RAISE APPLICATION_ERROR.
    ENDIF.
  ENDIF.


6. Activate the User-exit.


Testing & result:

1. MM02 for Material. Material 4000000285 with Material Grup 0510150

As known in Table T023, Valuation Class allowed is GP05

2. Valuation Class changed to GP01

3. When saving, error message occurs.