How to copy BOM using CEWB Function Modules

I've seen on SCN plenty of question about copying BOM from one plant to other, or from one material to other. Most of them were closed by using LSMW or direct input methods, but there is a really nice way to do it using CEWB function modules. I've been playing with CEWB modules for a long time so I'm not afraid to debug CEWB in case I cannot make it work by myself but when you look on the logic of the sample program to copy BOM then you'll see it's reasonable. 
 
  1. At the beginning let's clear Function Group memory using FM 'CP_CC_S_REFRESH_DATA'. Just for the safety reasons :-)
  2. Then let's load BOM header data to FG memory. If you want to do mass transaction then you would probably want to fill selection table i_cpsc_bom_sel with more than one BOM number, it will be then faster than calling FM 'CP_CC_S_LOAD_COMPLEX_BY_BOM' separately many times.
  3. But header data is not enough, we should do same thing for BOM items using FM 'CP_CC_S_LOAD_COMPLEX_BY_ITM', as Items belongs to different work area in CEWB.
  4. Once we loaded data to memory we can start copying, firstly we need to call 'CP_CC_S_BOM_PROVIDE_BY_MBM' to get source header data and then use its export parameters in FM 'CS_CL_S_MAT_BOM_CREATE_BY_COPY', which is doing copy of a header data.
  5. Once it's done we have to get source items from memory using 'CS_CL_P_ITM_PROVIDE', and then use it for copying items to our target BOM in 'CS_CL_S_ITM_CREATE_BY_COPY'.
  6. At the end, when everything is fine, use FM 'CP_CC_S_SAVEto save your changes and commit your work.

Work done!
Bellow and in the NUGG attachment you can find sample program which is using this technique for single BOM copy. It does not have error handling or built-in change number usage but you can do it yourself in your programs if this will be necessary.
 

report zabcopybom.
datac_bom_class_data      like bom_class_data,
      e_ecm_data_error_type type cpcc_message_type.


selection-screen begin of block b01 with frame title text-b01.
parametersp_matnr type mara-matnr obligatory,
            p_werks type marc-werks obligatory,
            p_stlal type mast-stlal obligatory,
            p_stlan type mast-stlan obligatory.
selection-screen end of block b01.
selection-screen begin of block b02 with frame title text-b02.
parametersp_matto type mara-matnr obligatory,
            p_werto type marc-werks obligatory,
            p_stlto type mast-stlal obligatory,
            p_stnto type mast-stlan.
selection-screen end of block b02.



start-of-selection.
  perform copy_bom.


form copy_bom.
  "! refresh FG data to be sure we get correct data only
  call function 'CP_CC_S_REFRESH_DATA'
    exceptions
      workarea_not_set 1
      others           2.
  if sy-subrc <> 0.
    "! no problem if there was error as this means nothing was to refresh
  endif.

  data i_cpsc_bom_sel type cpsc_bom_sel_type.
  datasource_stlnr type stko-stlnr.
  select single stlnr into @source_stlnr from mast
  where matnr eq @p_matnr
  and   werks eq @p_werks
  and   stlal eq @p_stlal.
  check sy-subrc eq 0.

  i_cpsc_bom_sel-stlnr value #low source_stlnr sign 'I' option 'EQ'  ).
  i_cpsc_bom_sel-stlty value #low 'M' sign 'I' option 'EQ'  ).
  i_cpsc_bom_sel-stlal value #low p_stlal sign 'I' option 'EQ'  ).
  i_cpsc_bom_sel-stlan value #low p_stlan sign 'I' option 'EQ'  ).
    "! Load BOM Header to memory
  call function 'CP_CC_S_LOAD_COMPLEX_BY_BOM'
    exporting
      i_class                        'P'
      i_work_area                    'SAP_BOM'
      i_classes_in_workarea          value classes_in_workareabom_inarea abap_true 
                                                                  itm_inarea abap_true 
                                                                  sui_inarea abap_true )
      i_cpsc_bom_sel                 i_cpsc_bom_sel
      i_date_from                    sy-datum
      i_date_to                      sy-datum
    exceptions
      workarea_not_found             1
      class_wrong_type               2
      workarea_wrong_type            3
      class_in_workarea_inconsistent 4
      workarea_not_specified         5
      bom_not_found                  6
      no_selection_criteria          7
      invalid_selection_period       8
      key_date_required_for_ecm      9
      others                         10.
  check sy-subrc eq 0.
    "! Load BOM Items to memory
  call function 'CP_CC_S_LOAD_COMPLEX_BY_ITM'
    exporting
      i_class                        'P'
      i_work_area                    'SAP_ITEM'
      i_classes_in_workarea          value classes_in_workareabom_inarea abap_true 
                                                                  itm_inarea abap_true 
                                                                  sui_inarea abap_true )
      i_cpsc_bom_sel                 i_cpsc_bom_sel
      i_date_from                    sy-datum
      i_date_to                      sy-datum
    exceptions
      workarea_not_found             1
      class_wrong_type               2
      workarea_wrong_type            3
      class_in_workarea_inconsistent 4
      workarea_not_specified         5
      itm_not_found                  6
      no_selection_criteria          7
      invalid_selection_period       8
      key_date_required_for_ecm      9
      others                         10.
  check sy-subrc eq 0.

  data e_bom_class_data type standard table of bom_class_data .
    "! Get BOM Header data
  call function 'CP_CC_S_BOM_PROVIDE_BY_MBM'
    exporting
      i_date_from      sy-datum
      i_date_to        sy-datum
      i_material       p_matnr
      i_plant          p_werks
      i_stlan          p_stlan
      i_stlnr          source_stlnr
      i_stlal          p_stlal
    tables
      e_bom_class_data e_bom_class_data
    exceptions
      wrong_key        1
      others           2.
  check sy-subrc eq and line_existse_bom_class_data[ ).

  c_bom_class_data-stlal p_stlto.
  c_bom_class_data-stlan p_stnto.
    "! Create copy of BOM header
  call function 'CS_CL_S_MAT_BOM_CREATE_BY_COPY'
    exporting
*     I_ECN_S                = ' '
      i_key_date_s           sy-datum
      i_stlty                e_bom_class_data[ ]-stlty
      i_stlnr                e_bom_class_data[ ]-stlnr
      i_stlal                e_bom_class_data[ ]-stlal
      i_stkoz                e_bom_class_data[ ]-stkoz
      i_matnr_new            p_matto
      i_werks_new            p_werto
      i_stlan_new            c_bom_class_data-stlan
      i_stlnr_new            c_bom_class_data-stlnr
      i_stlal_new            c_bom_class_data-stlal
      i_flg_bom_check        abap_false
    changing
      c_bom_class_data       c_bom_class_data
    exceptions
      mbm_not_consistent     1
      bom_not_consistent     2
      no_authority           3
      bom_not_locked         4
      no_valid_material      5
      no_valid_plant         6
      no_valid_usage         7
      alternative_overflow   8
      ident_already_exists   9
      multiple_bom           10
      variant_bom            11
      bom_in_plant_not_found 12
      ecm_data_not_suitable  13
      input_incomplete       14
      no_valid_bom           15
      others                 16.
  if sy-subrc 0.
    "! GET source items
    datae_itm_class_data type standard table of itm_class_data.
    call function 'CS_CL_P_ITM_PROVIDE'
      exporting
        i_date_from      sy-datum
        i_date_to        sy-datum
        i_stlty          e_bom_class_data[ ]-stlty
        i_stlnr          e_bom_class_data[ ]-stlnr
        i_stlal          e_bom_class_data[ ]-stlal
        i_werks          p_werks
      tables
        e_itm_class_data e_itm_class_data
      exceptions
        wrong_key        1
        others           2.
    if sy-subrc eq 0.
      datai_itm_class_data_new type itm_class_data.
      datai_alternatives_new type cscl_alt_tb_type.
      loop at e_itm_class_data assigning field-symbol(<itm>).
    "! Copy Item        
        call function 'CS_CL_S_ITM_CREATE_BY_COPY'
          exporting
*           i_ecn_s                  = i_ecn_s
            i_key_date_s             sy-datum
            i_stlty                  e_bom_class_data[ ]-stlty
            i_stlnr                  e_bom_class_data[ ]-stlnr
            i_stlal                  e_bom_class_data[ ]-stlal
            i_stvkn                  <itm>-stvkn
            i_stlkn                  <itm>-stlkn
            i_stpoz                  <itm>-stpoz
            i_itm_class_data_new     i_itm_class_data_new
            i_stlty_new              c_bom_class_data-stlty
            i_stlnr_new              c_bom_class_data-stlnr
            i_alternatives_new       value cscl_alt_tb_typestlal p_stlto )
            i_flg_itm_check          abap_false
          exceptions
            new_item_not_consistent  1
            no_authority             2
            no_valid_item            3
            new_item_not_locked      4
            new_ident_already_exists 5
            wrong_new_ident_type     6
            no_valid_new_bom         7
            input_incomplete         8
            ecm_data_not_suitable    9
            others                   10.
        if sy-subrc ne 0.
          return.
        endif.
      endloop.
          "! Save data
      call function 'CP_CC_S_SAVE'
        exceptions
          error_at_save 1
          others        2.
      if sy-subrc 0.
        commit work.
      else.
        rollback work.
      endif.
    endif.
  endif.

endform.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值