Friday, June 12, 2020

Updating OutboundDelivery in SAP SD via bapi

     My business requirement : it was to update VL02N outbound delivery if quantity is available based on certain logic from z-program gate entry program so i have to use bapi :  'BAPI_OUTB_DELIVERY_CHANGE'.
  
        

              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.
              data p_input TYPE vbeln,
       p_qty type lfimg.
           P_INPUT w_token-delno.
           P_QTY  w_token-lgmng.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input  p_input
  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    p_qty .
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.
  DATAl_msg_str(100).
  CALL FUNCTION 'FORMAT_MESSAGE'
    EXPORTING
      id        LW_DORETN-id
      lang      sy-langu
      no        LW_DORETN-number
*      v1        = message_v1
*      v2        = message_v2
*      v3        = message_v3
*      v4        = message_v4
    IMPORTING
      msg       l_msg_str   " Message text
    EXCEPTIONS
      not_found 1
      OTHERS    2.
  MESSAGE L_MSG_STR TYPE 'E'.

ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait 'X'.
ENDIF.