DEMO:MB1B311移库BAPI_GOODSMVT_CREATE_SAP刘梦_新浪博客
    *&---------------------------------------------------------------------*
*& Report  ZDEMO_MB1B
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
report  zdemo_mb1b.
parameters: p_matnr type matnr.
parameters: p_werks type werks_d.
parameters: p_movef type lgort_d."move from
parameters: p_movet type lgort_d."move to
parameters: p_sernr type gernr.
*&BAPI
data: ls_goodsmvt_header      like  bapi2017_gm_head_01,
      lv_goodsmvt_code        like  bapi2017_gm_code.
data:
     lt_goodsmvt_item         like table of bapi2017_gm_item_create,
     ls_goodsmvt_item         like  bapi2017_gm_item_create,
     lt_goodsmvt_serialnumber like table of bapi2017_gm_serialnumber,
     ls_goodsmvt_serialnumber like  bapi2017_gm_serialnumber  .
data:lv_materialdocument      type  bapi2017_gm_head_ret-mat_doc,
     lv_matdocumentyear       type  bapi2017_gm_head_ret-doc_year.
data:lt_return                type table of   bapiret2.
data:lv_item_line             type int4.
field-symbols        like line of lt_goodsmvt_item.
start-of-selection.
  lv_goodsmvt_code-gm_code = '04'.
  ls_goodsmvt_header-pstng_date = sy-datum.
  ls_goodsmvt_header-doc_date = sy-datum.
  ls_goodsmvt_header-pr_uname = sy-uname.
  ls_goodsmvt_item-material    = p_matnr.
  ls_goodsmvt_item-plant       = p_werks.
  ls_goodsmvt_item-stge_loc    = p_movef.
  ls_goodsmvt_item-move_type   = '311'.
  ls_goodsmvt_item-move_plant  = p_werks.
  ls_goodsmvt_item-move_stloc  = p_movet.
  ls_goodsmvt_item-entry_qnt   = 1.
  read table lt_goodsmvt_item assigning 
        with key  material    = p_matnr
                  plant       = p_werks
                  stge_loc    = p_movef
                  move_type   = '311'
                  move_plant  = p_werks
                  move_stloc  = p_movet.
  if sy-subrc = 0.
    -entry_qnt = -entry_qnt   1.
  else.
    append ls_goodsmvt_item to lt_goodsmvt_item.
  endif.
  describe table lt_goodsmvt_item lines lv_item_line.
  ls_goodsmvt_serialnumber-matdoc_itm = lv_item_line.
  ls_goodsmvt_serialnumber-serialno   =  p_sernr.
  append ls_goodsmvt_serialnumber to lt_goodsmvt_serialnumber.
  call function 'BAPI_GOODSMVT_CREATE'
    exporting
      goodsmvt_header       = ls_goodsmvt_header
      goodsmvt_code         = lv_goodsmvt_code
    importing
      materialdocument      = lv_materialdocument
      matdocumentyear       = lv_matdocumentyear
    tables
      goodsmvt_item         = lt_goodsmvt_item[]
      goodsmvt_serialnumber = lt_goodsmvt_serialnumber
      return                = lt_return[].
  .
  call function 'BAPI_TRANSACTION_COMMIT'
    exporting
      wait = 'X'.
  write lv_materialdocument.