SAP ABAP BAPI函数50系列我们已经完成了10个,达到了20%的进度。
今晚吃个鸡腿奖励一下自己~
现在,我们来学习函数模块 PP模块的BAPI函数bapi_mat_bom_existence_check,
函数作用是:检查是否存在物料的物料单组
2.应用场景
3.参数说明
3.1.导入
参数名称 关联类型 可选的 说明
MATERIAL BAPI1080_MBM_C-MATERIAL 可选 物料编号(18个字符)
PLANT BAPI1080_MBM_C-PLANT 可选 工厂
BOMUSAGE BAPI1080_BGR_C-BOM_USAGE 必选 BOM 用途
对于这个函数,如果您发现S4HANA系统的新语法和ECC系统或者R3系统有点差异,可以留言告诉我。
对于初级SAP ABAP顾问来说,是不容易的。
作者曾经也是SAP ABAP beginners ,到处去找Tutorial for Constant。
需要自学很久,没人给咱免费培训。
写个测试代码DEMO举例子。下面是代码。
代码示例
*定义bapi相关参数
DATA: ls_bom TYPE ty_s_bom,
ls_bom2 TYPE ty_s_bom,
lt_bom2 TYPE tt_bom,
lv_material TYPE csap_mbom-matnr,
lv_plant TYPE csap_mbom-werks,
lv_bom_usage TYPE csap_mbom-stlan,
lv_valid_from TYPE csap_mbom-datuv,
lv_change_no TYPE csap_mbom-aennr,
lv_revision_level TYPE csap_mbom-revlv,
ls_stko TYPE stko_api01,
lt_stpo TYPE TABLE OF stpo_api01,
lt_dep_data TYPE TABLE OF csdep_dat,
lt_dep_descr TYPE TABLE OF csdep_desc,
lt_dep_order TYPE TABLE OF csdep_ord,
lt_dep_source TYPE TABLE OF csdep_sorc,
lt_dep_doc TYPE TABLE OF csdep_doc,
lt_ltx_line TYPE TABLE OF csltx_line,
lt_stpu TYPE TABLE OF stpu_api01,
lt_sgt_bomc TYPE sgt_comp_segment_t,
ls_stpo TYPE stpo_api01,
ls_dep_data TYPE csdep_dat,
ls_dep_descr TYPE csdep_desc,
ls_dep_order TYPE csdep_ord,
ls_dep_source TYPE csdep_sorc,
ls_dep_doc TYPE csdep_doc,
ls_ltx_line TYPE csltx_line,
ls_stpu TYPE stpu_api01,
ls_sgt_bomc TYPE LINE OF sgt_comp_segment_t,
lv_warning TYPE capiflag-flwarning,
lv_bom_no TYPE stko_api02-bom_no,
lv_item_no TYPE i,
lv_item_number TYPE sposn,
flg_no_commit_work TYPE c,
ls_message TYPE bapiret2,
ls_old_bom TYPE ty_s_bom,
lv_index TYPE i,
lv_index_bom TYPE i,
lv_old_material TYPE matnr,
lv_deletion_flag TYPE bapimatall-del_flag,
ls_return TYPE bapireturn1,
lv_material_check TYPE bapimatall-material,
lt_return TYPE TABLE OF bapiret2,
ls_db_return TYPE bapiret2.
flg_no_commit_work = abap_true.
EXPORT flg_no_commit_work = flg_no_commit_work TO MEMORY ID ‘CS_CSAP’.
lt_bom2 = it_bom.
lv_item_no = 0.
LOOP AT it_bom INTO ls_bom WHERE material IS NOT INITIAL.
CLEAR: lv_material, lv_plant, lv_bom_no, lv_bom_usage, lv_valid_from, ls_stko, ls_stpo, lt_stpo, lv_warning, lv_change_no, lv_revision_level,
ls_old_bom, lv_deletion_flag , ls_return.
IF sy-tabix = 1 OR lv_old_material <> ls_bom-material.
lv_material = ls_bom-material.
lv_plant = ls_bom-plant.
lv_bom_usage = ls_bom-bomusage.
lv_valid_from = mv_valid_from_date.
* Fill STKO structure
ls_stko-base_unit = ls_bom-unit.
ls_stko-bom_status = ls_bom-bomstatus.
ls_stko-base_quan = ls_bom-quantity.
* Fill STPO table
LOOP AT lt_bom2 INTO ls_bom2 WHERE material = ls_bom-material AND plant = ls_bom-plant AND bomusage = ls_bom-bomusage.
lv_index_bom = lv_index_bom + 1.
lv_item_no = lv_item_no + 10.
lv_item_number = lv_item_no.
ls_stpo-item_categ = ls_bom2-itemcategory.
ls_stpo-item_no = lv_item_number.
ls_stpo-component = ls_bom2-component.
ls_stpo-comp_qty = ls_bom2-quantity.
ls_stpo-comp_unit = ls_bom2-unit.
APPEND ls_stpo TO lt_stpo.
ENDLOOP.
lv_material_check = ls_bom-material.
* Check existence of material
CALL FUNCTION ‘BAPI_MATERIAL_EXISTENCECHECK’
EXPORTING
material = lv_material_check
IMPORTING
deletion_flag = lv_deletion_flag
return = ls_return.
IF lv_deletion_flag = abap_true.
ls_message-id = gc_message_class.
ls_message-number = ‘011’.
ls_message-type = cl_esh_adm_constants=>gc_msgty_e.
ls_message-message_v1 = ls_bom-material.
ev_error = abap_true.
zcl_dataload_message_buffer=>add_from_bapiret2( is_return = ls_message ).
ELSEIF ls_return-type = cl_esh_adm_constants=>gc_msgty_e.
ls_message-id = gc_message_class.
ls_message-number = ‘027’.
ls_message-type = cl_esh_adm_constants=>gc_msgty_e.
ls_message-message_v1 = ls_bom-material.
ev_error = abap_true.
zcl_dataload_message_buffer=>add_from_bapiret2( is_return = ls_message ).
ELSE.
* Check if BOM already exists
CALL FUNCTION ‘BAPI_MAT_BOM_EXISTENCE_CHECK’
EXPORTING
material = lv_material_check
plant = ls_bom-plant
bomusage = ls_bom-bomusage
TABLES
return = lt_return.
IF lt_return IS INITIAL.
ls_message-id = gc_message_class.
ls_message-number = ‘014’.
ls_message-type = cl_esh_adm_constants=>gc_msgty_i.
ls_message-message_v1 = ls_bom-material.
zcl_dataload_message_buffer=>add_from_bapiret2( is_return = ls_message ).
ELSE.
CALL FUNCTION ‘CSAP_MAT_BOM_CREATE’
EXPORTING
material = lv_material
plant = lv_plant
bom_usage = lv_bom_usage
change_no = lv_change_no
revision_level = lv_revision_level
i_stko = ls_stko
fl_commit_and_wait = abap_false
IMPORTING
fl_warning = lv_warning
bom_no = lv_bom_no
TABLES
t_stpo = lt_stpo
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc IS NOT INITIAL.
ev_error = abap_true.
ls_message-id = gc_message_class.
ls_message-number = ‘005’.
ls_message-type = cl_esh_adm_constants=>gc_msgty_e.
ls_message-message_v1 = ls_bom-material.
zcl_dataload_message_buffer=>add_from_bapiret2( is_return = ls_message ).
ENDIF.
ENDIF.
ENDIF.
lv_old_material = ls_bom-material.
ENDIF.
ENDLOOP.
IF ev_error = abap_false.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’
IMPORTING
return = ls_db_return. ” Return Messages
flg_no_commit_work = abap_false.
EXPORT flg_no_commit_work = flg_no_commit_work TO MEMORY ID ‘CS_CSAP’.
lt_bom2 = it_bom.
* lv_item_no = 0.
LOOP AT it_bom INTO ls_bom WHERE material IS NOT INITIAL.
CLEAR: lv_material, lv_plant, lv_bom_no, lv_bom_usage, lv_valid_from, ls_stko, ls_stpo, lt_stpo, lv_warning, lv_change_no, lv_revision_level,
ls_old_bom, lv_deletion_flag , ls_return, lv_material_check.
lv_item_no = 0.
IF sy-tabix = 1 OR lv_old_material <> ls_bom-material.
lv_material = ls_bom-material.
lv_plant = ls_bom-plant.
lv_bom_usage = ls_bom-bomusage.
lv_valid_from = mv_valid_from_date.
* Fill STKO structure
ls_stko-base_unit = ls_bom-unit.
ls_stko-bom_status = ls_bom-bomstatus.
ls_stko-base_quan = ls_bom-quantity.
* Fill STPO table
LOOP AT lt_bom2 INTO ls_bom2 WHERE material = ls_bom-material AND plant = ls_bom-plant AND bomusage = ls_bom-bomusage.
lv_index_bom = lv_index_bom + 1.
lv_item_no = lv_item_no + 10.
lv_item_number = lv_item_no.
ls_stpo-item_categ = ls_bom2-itemcategory.
ls_stpo-item_no = lv_item_number.
ls_stpo-component = ls_bom2-component.
ls_stpo-comp_qty = ls_bom2-quantity.
ls_stpo-comp_unit = ls_bom2-unit.
APPEND ls_stpo TO lt_stpo.
ENDLOOP.
lv_material_check = ls_bom-material.
* Check if BOM already exists
CALL FUNCTION ‘BAPI_MAT_BOM_EXISTENCE_CHECK’
EXPORTING
material = lv_material_check
plant = ls_bom-plant
bomusage = ls_bom-bomusage
TABLES
return = lt_return.
IF lt_return IS NOT INITIAL.
CALL FUNCTION ‘CSAP_MAT_BOM_CREATE’
EXPORTING
material = lv_material
plant = lv_plant
bom_usage = lv_bom_usage
change_no = lv_change_no
valid_from = lv_valid_from
revision_level = lv_revision_level
i_stko = ls_stko
fl_commit_and_wait = abap_true
IMPORTING
fl_warning = lv_warning
bom_no = lv_bom_no
TABLES
t_stpo = lt_stpo
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc IS NOT INITIAL.
ev_error = abap_true.
ls_message-id = gc_message_class.
ls_message-number = ‘005’.
ls_message-type = cl_esh_adm_constants=>gc_msgty_e.
ls_message-message_v1 = ls_bom-material.
zcl_dataload_message_buffer=>add_from_bapiret2( is_return = ls_message ).
ENDIF.
ENDIF.
lv_old_material = ls_bom-material.
ENDIF.
ENDLOOP.
ls_message-number = ‘010’.
ls_message-id = gc_message_class.
ls_message-type = cl_esh_adm_constants=>gc_msgty_s.
ls_message-message_v1 = gc_bom.
zcl_dataload_message_buffer=>add_from_bapiret2( is_return = ls_message ).
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’
IMPORTING
return = ls_db_return. ” Return Messages
IF ls_db_return IS NOT INITIAL.
zcl_dataload_message_buffer=>add_from_bapiret2( is_return = ls_db_return ).
ENDIF.
ENDIF.
ENDMETHOD.
本文链接 : http://www.hot583.com/2022/04/02/bapi_mat_bom_existence_check/
本文为作者原创,如果这篇文章对您有用,欢迎转载,
转载请务必注明来源《今日热点在看》http://www.hot583.com/
我的微信公众号【 hot583 】 或者在这个链接联系我
微信原文《全文完》