在SAP实施时,很多公司存在的一个现象:即一张物料凭证包含多行物料移动信息,因为有些特殊原因比如退货等,需要将某一行数据给冲销。退回给供应商。SAP提供了冲销的BAPI BAPI_GOODSMVT_CANCEL 这个函数可以整张冲销物料凭证,也可以冲销物料凭证里面的某一行 DEMO如下:
DATA: li_goodsmvt_headret TYPE bapi2017_gm_head_ret, lt_item TYPE TABLE OF bapi2017_gm_item_04 , ls_item type bapi2017_gm_item_04 . DATA: it_return TYPE TABLE OF bapiret2 , ls_return type bapiret2 . DATA : lv_type TYPE string. data :new_mblnr type mseg-mblnr. ""如只需要冲销某一行则添加行号,如果整张冲销则不需要填写行号即可。 ls_item-MATDOC_ITEM = '0002'. APPEND ls_item to lt_item. CALL FUNCTION 'BAPI_GOODSMVT_CANCEL' EXPORTING materialdocument = '5000001154' "物料凭证 matdocumentyear = '2025' "年 goodsmvt_pstng_date = '20250828' goodsmvt_pr_uname = sy-uname IMPORTING goodsmvt_headret = li_goodsmvt_headret TABLES return = it_return goodsmvt_matdocitem = lt_item. new_mblnr = li_goodsmvt_headret-mat_doc. "新生成的凭证 LOOP AT it_return INTO ls_return WHERE type NA 'AEX'."如果不包含aex的错误 就代表成功 lv_type = 'S'. EXIT. ENDLOOP. IF it_return IS INITIAL ."为空也是代表成功 lv_type = 'S'. ENDIF. IF lv_type = 'S'. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = abap_true. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. ENDIF. cl_demo_output=>write( it_return ). cl_demo_output=>write( lt_item ). cl_demo_output=>write( new_mblnr ). cl_demo_output=>write( li_goodsmvt_headret ). cl_demo_output=>display( ).