Showing posts with label Program Sample. Show all posts
Showing posts with label Program Sample. Show all posts

Jul 6, 2026

Replace Some Characters in the Middle of Text.

Requirement:

To replace the 5th character in a 10-character string (or any string), use the ABAP overlay statement or the replace section command. In ABAP, character offsets are zero-based, meaning the 5th character is at offset

Solution:

Here are the two best approaches for doing this:

Method 1: Using OVERLAY (Best for replacing specific offsets)
abap
DATA: gv_string TYPE string VALUE '1234567890'.

" The 5th character is at offset 4 (length 1). Replace it with 'X'
OVERLAY gv_string WITH '    X' ONLY ' '.

Method 2: Using REPLACE SECTION (Best for explicit length)
abap
DATA: gv_string TYPE string VALUE '1234567890'.

REPLACE SECTION OFFSET 4 LENGTH 1 OF gv_string WITH 'X'.

Method 3: Inline Expression (ABAP 7.40+)
abap
DATA(gv_string) = '1234567890'.

gv_string = |{ gv_string(4) }X{ gv_string+5 }|.

Output for all methods: 1234X67890

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 21, 2026

SAP ABAP EWM Sample Code for Goods Movement, Reversal, GR and GI

This Function module works for Goods Movement, Reversal, GR and GI 

FUNCTION zdifm_goods_movement .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IV_DOCNO) TYPE  /SCDL/DL_DOCNO_INT
*"     VALUE(IV_REVERSAL) TYPE  XFELD OPTIONAL
*"  EXPORTING
*"     VALUE(ET_MESSAGE) TYPE  /SCDL/DM_MESSAGE_TAB
*"----------------------------------------------------------------------

  DATA: lo_dlv_save TYPE REF TO /scwm/cl_dlv_management_prd,
        lo_message  TYPE REF TO /scdl/cl_dm_message,
        lt_dlv      TYPE        /scwm/dlv_docid_item_tab,
        ls_dlv      TYPE        /scwm/dlv_docid_item_str,
        lv_gmcat    TYPE        /scdl/dl_doccat.


  lo_dlv_save = /scwm/cl_dlv_management_prd=>get_instance( ).

  SELECT i~docid, i~itemid, h~docno, h~doccat
    FROM /scdl/db_proci_i AS i
    INNER JOIN /scdl/db_proch_i AS h
      ON h~docid = i~docid
    INTO TABLE @DATA(lt_pdi)
    WHERE h~docno = @iv_docno.


  CHECK sy-subrc IS INITIAL.

  LOOP AT lt_pdi INTO DATA(ls_pdi).

    IF sy-tabix = 1.
      lo_dlv_save->get_warehouse(
               EXPORTING iv_doccat = ls_pdi-doccat
                         iv_docid  = ls_pdi-docid
               IMPORTING ev_whno   = DATA(lv_lgnum) ).
    ENDIF.

    ls_dlv-docid  = ls_pdi-docid.
    ls_dlv-itemid = ls_pdi-itemid.
    ls_dlv-doccat = ls_pdi-doccat.
    APPEND ls_dlv TO lt_dlv.

  ENDLOOP.


  CHECK lt_dlv[] IS NOT INITIAL.

  /scwm/cl_tm=>cleanup( iv_lgnum = lv_lgnum ).

  "Reversal GM
  IF iv_reversal IS NOT INITIAL.
    CASE ls_pdi-doccat.
* Goods receipt
      WHEN wmegc_doccat_pdi.                    "PDI Inbound delivery
        "GRR Reverse Goods Receipt/SCDL/IF_DL_DOC_C=>SC_DOCCAT_GRR
        lv_gmcat = /scwm/if_docflow_c=>sc_grr.
* Goods issue
      WHEN wmegc_doccat_pdo                     "PDO Outbound del
        OR /scdl/if_dl_doc_c=>sc_doccat_out_fd. "FDO Document type delivery
        "GI (Goods Issue)  /SCDL/IF_DL_DOC_C=>SC_DOCCAT_GI
        lv_gmcat = /scwm/if_docflow_c=>sc_gir.
    ENDCASE.

  ELSE.                                       "Create Goods Movement
    CASE ls_pdi-doccat.
* Goods receipt
      WHEN wmegc_doccat_pdi.                    "PDI Inbound delivery
        "GR (Goods Receipt)  /SCDL/IF_DL_DOC_C=>SC_DOCCAT_GR
        lv_gmcat = /scwm/if_docflow_c=>sc_gr.
* Goods issue
      WHEN wmegc_doccat_pdo                     "PDO Outbound del
        OR /scdl/if_dl_doc_c=>sc_doccat_out_fd. "FDO Document type delivery
        "GI (Goods Issue)  /SCDL/IF_DL_DOC_C=>SC_DOCCAT_GI
        lv_gmcat = /scwm/if_docflow_c=>sc_gi.
    ENDCASE.

  ENDIF.

  /scwm/cl_goods_movement=>/scwm/if_gm_dlv~post_dlv(
                 EXPORTING it_dlv     = lt_dlv
                           iv_gmcat   = lv_gmcat
                 IMPORTING eo_message = lo_message ).

  "Message handling
  IF lo_message IS BOUND.
    et_message = lo_message->get_messages( ).
    IF et_message[] IS INITIAL.
      ##NEEDED
      lo_dlv_save->save( IMPORTING ev_rejected = DATA(lv_flag_rejected)
                                   et_message  = et_message ).
    ENDIF.
  ENDIF. "lo_message IS BOUND.


  LOOP AT et_message INTO DATA(ls_message)
                     WHERE msgty CA 'AXE'.
    EXIT.
  ENDLOOP.

  IF sy-subrc EQ 0 AND
     ls_message IS NOT INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = abap_true.
  ENDIF.

*   /scwm/cl_tm=>cleanup( iv_lgnum = lv_lgnum ).
  CALL METHOD /scwm/cl_tm=>cleanup( ).
  CALL FUNCTION '/SCWM/HUMAIN_REFRESH'.


ENDFUNCTION.