导语:BOM创建在SAP中是最常见的操作,通常用户会要求用客制化程序批量创建,示例中只创建一层组件,另外【SAP 批量修改或添加BOM组件】中介绍如何批量修改BOM组件,或为BOM添加组件。
DATA:
testrun LIKE bapiflag,
lt_bomgroup LIKE bapi1080_bgr_c OCCURS 0 WITH HEADER LINE,
lt_variants LIKE bapi1080_bom_c OCCURS 0 WITH HEADER LINE,
lt_items LIKE bapi1080_itm_c OCCURS 0 WITH HEADER LINE,
lt_subitems LIKE bapi1080_sui_c OCCURS 0 WITH HEADER LINE,
lt_materialr LIKE bapi1080_mbm_c OCCURS 0 WITH HEADER LINE,
lt_itemassig LIKE bapi1080_rel_itm_bom_c OCCURS 0 WITH HEADER LINE,
lt_subitemas LIKE bapi1080_rel_sui_itm_c OCCURS 0 WITH HEADER LINE,
texts LIKE bapi1080_txt_c OCCURS 0 WITH HEADER LINE,
lt_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
DATA:ls_head LIKE itab_header.
ls_head = itab_header.
lt_bomgroup-bom_group_identification = 'BAPI_SMP_COL1'.
lt_bomgroup-object_type = 'BGR'.
lt_bomgroup-object_id = 'SIMPLE1'.
lt_bomgroup-bom_usage = ls_head-stlan. " YOU COULD CHANGE THE BOM USAGE TO YOUR
lt_bomgroup-ltxt_lang = sy-langu.
lt_bomgroup-created_in_plant = ls_head-werks.
lt_bomgroup-technical_type = ' '.
lt_bomgroup-bom_text = ls_head-ztext.
lt_bomgroup-auth_group = ls_head-stlbe.
APPEND lt_bomgroup.
CLEAR lt_bomgroup.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = ls_head-stlal
IMPORTING
output = ls_head-stlal.
* Header Details of the different variants
CLEAR lt_variants.
lt_variants-bom_group_identification = 'BAPI_SMP_COL1'.
lt_variants-object_type = 'BOM'.
lt_variants-object_id = 'SIMPLE1'.
lt_variants-alternative_bom = ls_head-stlal. "可选BOM
* lt_variants-bom_status = '01'. "BOM状态
lt_variants-bom_status = ls_head-stlst.
lt_variants-base_qty = ls_head-bmeng. "数量
lt_variants-base_unit = ls_head-bmein. "单位
lt_variants-valid_from_date = ls_head-datuv. "有效期起始日期
lt_variants-alt_text = ls_head-stktx. "可选BOM文本
lt_variants-function = 'NEW'.
APPEND lt_variants.
* 物料分配 CREATE-/CHANGE BAPI 物料 BOM
CLEAR lt_materialr.
lt_materialr-bom_group_identification = 'BAPI_SMP_COL1'.
lt_materialr-material = ls_head-matnr. "物料编码
lt_materialr-plant = ls_head-werks. "工厂
lt_materialr-bom_usage = ls_head-stlan. "BOM用途
lt_materialr-alternative_bom = ls_head-stlal. "可选BOM
lt_materialr-lot_size_from = ls_head-losvn. "从批量
lt_materialr-lot_size_to = ls_head-losvn. "到批量
APPEND lt_materialr.
DATA:l_object_id TYPE cs_object_id.
DATA:l_tabix TYPE sy-tabix.
DATA:l_tabic TYPE char10.
DATA:lv_posnr TYPE sposn.
DATA:lv_i TYPE i.
DATA:lv_n TYPE n LENGTH 4.
CLEAR :lv_posnr,lv_i,lv_n.
lv_i = 10.
* Details of the items of the variants
LOOP AT itab_item INTO DATA(ls_item).
CLEAR:l_tabix,
l_tabic,
l_object_id.
l_tabix = sy-tabix.
MOVE l_tabix TO l_tabic.
CONCATENATE 'SIMPLE' l_tabic INTO l_object_id.
CONDENSE l_object_id NO-GAPS.
lv_n = lv_i.
lv_posnr = lv_n.
CLEAR lt_items.
lt_items-bom_group_identification = 'BAPI_SMP_COL1'.
lt_items-object_type = 'ITM'.
lt_items-object_id = l_object_id.
lt_items-item_no = lv_posnr. "行号
lt_items-item_cat = ls_item-postp. "类别
lt_items-sort_string = ls_item-sortf. "排序字符串
lt_items-component = ls_item-idnrk. "组件物料
lt_items-item_text1 = ls_item-potx1. "项目文本1
lt_items-item_text2 = ls_item-potx2. "项目文本2
lt_items-comp_qty = ls_item-menge. "组件数量 (BTCI)
lt_items-comp_unit = ls_item-meins. "组件计量单位
lt_items-ltxt_lang = sy-langu.
lt_items-valid_from_date = sy-datum. "有效起始日期
lt_items-mat_provision = ls_item-beikz. "物料供应标识
lt_items-opr_scrap = ls_item-avoau. "工序废品
lt_items-spproctype = ls_item-itsob. "特殊获取类
lt_items-iss_st_loc = ls_item-lgort. "库存地点
lt_items-alt_item_group = ls_item-alpgr. "替代组
lt_items-alt_item_strategy = ls_item-alpst. "替代策咯
lt_items-alt_item_prio = ls_item-alprf. "优先级
lt_items-usage_prob = ls_item-ewahr. "使用可能性
APPEND lt_items.
CLEAR lt_itemassig.
lt_itemassig-bom_group_identification = 'BAPI_SMP_COL1'.
lt_itemassig-sub_object_type = 'ITM'.
lt_itemassig-sub_object_id = l_object_id."'SIMPLE1'.
lt_itemassig-super_object_type = 'BOM'.
lt_itemassig-super_object_id = 'SIMPLE1'.
lt_itemassig-valid_from_date = sy-datum.
lt_itemassig-function = 'NEW'.
APPEND lt_itemassig.
CLEAR ls_item.
lv_i = lv_i + 10.
ENDLOOP.
* 调用BAPI,创建BOM。
CALL FUNCTION 'BAPI_MATERIAL_BOM_GROUP_CREATE'
EXPORTING
* TESTRUN = ' '
all_error = 'X'
TABLES
bomgroup = lt_bomgroup
variants = lt_variants
items = lt_items
materialrelations = lt_materialr
itemassignments = lt_itemassig
return = lt_return.
注意事项:
1、lt_variants-alternative_bom(可选BOM)赋值的时候一定要有前置零,否则会报错
2、ITEMS和ITEMASSIGNMENTS要一一对应,对应条件是OBJECT_ID和SUB_OBJECT_ID,同理SUBITEMS和SUBITEMASSIGNMENTS也是一样
作者:小飞猪猪猪猪猪猪猪–CSDN