5 1 1 1 1 1 Rating 100% (5 Votes)
This topic was raised many times, I always found it's not possible to (or at least to hard to try) to update components allocation for routing operations. Fortunately I had to do it somehow and I manage to do it in two ways : 
1) with BDC (yeah I know, creepy but possible)
2) using EWB FM's basing on the SAP note 488765 - "Do it yourself EWB programming".
Today I will present program which is using EWB, in future I will post an example of doing this by BDC.
 
 
This program reads data from Excel file with proper structure (you can find Excel template in attachment). After reading data it collects all routings which needs to be updated to be sure that each routing is updated only once. Then the most important part is done in following way:
1) We need to load each routing and connected BOM to memory with CP_CC_S_LOAD_COMPLEX_BY_TSK and CP_CC_S_LOAD_COMPLEX_BY_BOM,
2) Next step is to get the BOM into internal table using CP_CC_S_PROVIDE_ITM_BY_AS_PATH.  BOM table contains also a explosion of sub-assemblies which are not phantoms which at the beginning seem to be useless, but after a while you'll notice that we need all levels of multilevel BOM in order to calculate Path (PLMZ-KANTE) and order level (PLMZ-STLST).  If you have to do components allocation for single level BOM then reworking the table is not needed, but in my example I did it as I needed to allocate components from multi-level BOM. You'll find here a form ITEM_CHECK which do the manipulation of the table. 
3) After it's done we need load operation data (CP_CC_S_PROVIDE_COM_BY_OPR) and current allocation of components (CP_CL_P_OPR_ALLOCATION_PROVIDE). We need to do it in order to delete current allocation.
4) Deletion is done with CM_CL_P_COM_DELETE and saved with CP_CC_S_SAVE. Deletion is committed as in other case there is no possibility to create new allocation.
5) Allocation is deleted, so we can now recreate it with new values. Firstly we need to reload again operation data to memory with CP_CC_S_OPR_PROVIDE_BY_MTK and then create allocation with  CP_CC_S_CREATE_COM. Again we need to save allocation with CP_CC_S_SAVE.
 
Bellow you'll find the code of the program. You can download also NUGG file for it!.
 
Program works in two steps, firstly it displays reading log, if it is ok you have to press first button on the toolbar  to create allocation.

report  zab_comp_allocation.

type-poolscpsc,cmclslis.
tablesmapl.

typesbegin of ty_assignment,
        matnr like mapl-matnr,
        werks  type werks_d,
        plnnr  type plnnr,
        plnfl  type plnfolge,
        plnal  type plnal,
        plnty  type plnty,
        vornr  type vornr,
        stlnr type stko-stlnr,
        stlal  type stlal,
        stlan  type stlan,
        posnr  type sposn,
        idnrk type stpo-idnrk,
        stlst type plmz-stlst,
      end of ty_assignment.

typesbegin of ty_plan,
        matnr like marc-matnr,
        werks like marc-werks,
        stlal like mast-stlal,
        stlan like mast-stlan,
        plnty like plko-plnty,
        plnnr like plko-plnnr,
        plnal like plko-plnal,
        plnfl type plnfolge,
      end of ty_plan.

constantsbegin of c_log,
            read   type balognr value 'READ',
            delete type balognr value 'DELETE',
            create type balognr value 'CREATE',
          end of c_log.

datagt_plan type standard table of ty_plan.
datagt_assignment type standard table of ty_assignment.

datagt_return type bapiret2_t.
datags_return type bapiret2.

datag_error type c.
field-symbols<assign> like line of gt_assignment.
field-symbols<plan> like line of gt_plan.


selection-screen begin of block exc with frame title text-exc.
parametersp_path type localfile lower case modif id exc.
parametersp_rows type i modif id exc.
selection-screen end of block exc.

selection-screen begin of block oth with frame title text-oth.
**  parameters: p_delexi as checkbox. "delete exisiting assigment
parametersp_test as checkbox.
select-optionss_matnr for mapl-matnr.
select-optionss_werks for mapl-werks.
select-optionss_plnnr for mapl-plnnr.
selection-screen end of block oth.

at selection-screen on value-request for p_path.
  perform open_file_dialog changing p_path.



initialization.

start-of-selection.
  refreshgt_plan[]gt_assignment[]gt_return[].
  perform check_entries.
  check g_error is initial.
  perform load_excel.

end-of-selection.
  check g_error is initial.
  perform display_log.

*&---------------------------------------------------------------------*
*&      Form  open_file_dialog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_PATH     text
*----------------------------------------------------------------------*
form open_file_dialog changing p_path.
  dataft_filetable type filetable with header line.
  dataf_rc type sy-subrc.

  cl_gui_frontend_services=>file_open_dialog(
  exporting
*      window_title            = window_title
    default_extension       'XLS'
*      default_filename        = default_filename
*      file_filter             = file_filter
*      with_encoding           = with_encoding
*      initial_directory       = initial_directory
    multiselection          space
  
changing
    file_table              ft_filetable[]
    rc                      
f_rc
*      user_action             = user_action
*      file_encoding           = file_encoding
  exceptions
    file_open_dialog_failed 1
    cntl_error              2
    error_no_gui            3
    not_supported_by_gui    4
    others                  5
    ).
  if sy-subrc eq 0.
    read table ft_filetable index 1.
    if sy-subrc eq 0.
      p_path ft_filetable.
    endif.

  endif.



endform.                    "open_file_dialog

*&---------------------------------------------------------------------*
*&      Form  check_entries
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form check_entries.
  clear g_error.
  if  p_path  is initial .
    message s001(00with 'Fill path!' display like 'E'.
    g_error 'X'.
  elseif p_rows lt 2.
    message s001(00with 'Last row number must be greater than 1!' display like 'E'.
    g_error 'X'.
  endif.
endform.                    "check_entries

*&---------------------------------------------------------------------*
*&      Form  load_task_list2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->I_PLNTY    text
*      -->I_PLNNR    text
*      -->I_PLNAL    text
*      -->I_STTAG    text
*      -->FS_ALV     text
*----------------------------------------------------------------------*
form load_task_list  using        i_matnr like marc-matnr
                                  i_werks like marc-werks
                                  i_plnty like plko-plnty
                                  i_plnnr like plko-plnnr
                                  i_plnal like plko-plnal
                                  i_sttag like plko-datuv.

  datafs_classes_in_workarea like classes_in_workarea,
        fs_tsk_plnty_selection type cpsc_plnty_type,
        ft_tsk_plnty_selection type cpsc_plnty_type occurs 0,
        fs_tsk_plnnr_selection type cpsc_plnnr_type,
        ft_tsk_plnnr_selection type cpsc_plnnr_type occurs 0,
        fs_tsk_plnal_selection type cpsc_plnal_type,
        ft_tsk_plnal_selection type cpsc_plnal_type occurs 0,
        fs_tsk_selection type cpsc_tsk_sel_type,
        fs_mbm_sel_type type cpsc_mbm_sel_type,
        fs_mbm_matnr      type cpsc_matnr_type ,
        fs_mbm_werks      type cpsc_werks_type ,
        fs_mbm_vbeln      type cpsc_vbeln_type ,
        fs_mbm_vbpos      type cpsc_posnr_type .


  concatenate 'I' 'EQ' i_matnr into fs_mbm_matnr.
  append fs_mbm_matnr to fs_mbm_sel_type-matnr.

  concatenate 'I' 'EQ' i_werks into fs_mbm_werks.
  append fs_mbm_werks to fs_mbm_sel_type-werks.


* objects for loading
  fs_classes_in_workarea-mtk_inarea 'X'.
  fs_classes_in_workarea-tsk_inarea 'X'.
  fs_classes_in_workarea-seq_inarea 'X'.
  fs_classes_in_workarea-opr_inarea 'X'.
  fs_classes_in_workarea-suo_inarea 'X'.
  fs_classes_in_workarea-prt_inarea 'X'.
  fs_classes_in_workarea-com_inarea 'X'.
  fs_classes_in_workarea-itm_inarea 'X'.
  fs_classes_in_workarea-bom_inarea 'X'.

* fill selection conditions - PLNTY
  concatenate 'I' 'EQ' i_plnty into fs_tsk_plnty_selection.
  append fs_tsk_plnty_selection to ft_tsk_plnty_selection.
  fs_tsk_selection-plnty ft_tsk_plnty_selection.

  concatenate 'I' 'EQ' i_plnnr into fs_tsk_plnnr_selection.
  append fs_tsk_plnnr_selection to ft_tsk_plnnr_selection.
  fs_tsk_selection-plnnr ft_tsk_plnnr_selection.

  concatenate 'I' 'EQ' i_plnal into fs_tsk_plnal_selection.
  append fs_tsk_plnal_selection to ft_tsk_plnal_selection.
  fs_tsk_selection-plnal ft_tsk_plnal_selection.

* load objects into EWB
  call function 'CP_CC_S_LOAD_COMPLEX_BY_TSK'
    exporting
      i_class                        'P'
      i_classes_in_workarea          fs_classes_in_workarea
      i_cpsc_tsk_sel                 
fs_tsk_selection
      i_date_from                    
i_sttag
      i_date_to                      
i_sttag
    
exceptions
      workarea_not_found             1
      workarea_wrong_type            2
      class_in_workarea_inconsistent 3
      workarea_not_specified         4
      opr_not_found                  5
      no_selection_criteria          6
      invalid_selection_period       7
      key_date_required_for_ecm      8
      others                         9.

  if sy-subrc ne 0.

  endif.

  call function 'CP_CC_S_LOAD_COMPLEX_BY_BOM'
  exporting
    i_class                              'P'
*     I_PROFILE                            = I_PROFILE
*     I_WORK_AREA                          = I_WORK_AREA
    i_classes_in_workarea                fs_classes_in_workarea
*     I_CPSC_MTK_SEL                       = I_CPSC_MTK_SEL
*     I_CPSC_COM_SEL                       = I_CPSC_COM_SEL
*     I_CPSC_TSK_SEL                       = I_CPSC_TSK_SEL
*     I_CPSC_OPR_SEL                       = I_CPSC_OPR_SEL
*     I_CPSC_SUO_SEL                       = I_CPSC_SUO_SEL
    i_cpsc_mbm_sel                       fs_mbm_sel_type
*     I_CPSC_BOM_SEL                       = I_CPSC_BOM_SEL
*     I_CPSC_ITM_SEL                       = I_CPSC_ITM_SEL
*     I_CPSC_SUI_SEL                       = I_CPSC_SUI_SEL
*     I_CPSC_PRT_SEL                       = I_CPSC_PRT_SEL
*     I_CPSC_PAC_SEL                       = I_CPSC_PAC_SEL
*     I_CPSC_CHA_SEL                       = I_CPSC_CHA_SEL
*     I_CPSC_MST_SEL                       = I_CPSC_MST_SEL
*     I_CPSC_ODP_SEL                       = I_CPSC_ODP_SEL
*     I_CPSC_CLA_SEL                       = I_CPSC_CLA_SEL
*     I_CPSC_ECN_SEL                       = I_CPSC_ECN_SEL
*     I_SEL_OPR_IDENT                      = I_SEL_OPR_IDENT
    i_date_from                          i_sttag
    i_date_to                            
i_sttag
*     I_MESSAGE_HANDLER                    = 'X'
*     I_SAVE_PROTOCOLL                     = ' '
*   IMPORTING
*     E_BOM_IDENT                          = E_BOM_IDENT
*     E_BOM_LOCK                           = E_BOM_LOCK
*     E_ITM_LOCK                           = E_ITM_LOCK
*     E_TSK_LOCK                           = E_TSK_LOCK
*     E_SEQ_LOCK                           = E_SEQ_LOCK
*     E_OPR_LOCK                           = E_OPR_LOCK
*     E_WORKAREA                           = E_WORKAREA
*     E_PROFILE                            = E_PROFILE
*     E_DATE_FROM                          = E_DATE_FROM
*     E_DATE_TO                            = E_DATE_TO
*     E_FLG_ECM_EVALUATION                 = E_FLG_ECM_EVALUATION
  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
    .
  if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  endif.


endform.                    " LOAD_TASK_LIST



*&---------------------------------------------------------------------*
*&      Form  componentallocation_ewb
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->FS_ALV         text
*      -->F_PLNNR        text
*      -->F_PLNAL        text
*      -->FT_COMALL      text
*      -->F_ONLY_OPTION  text
*----------------------------------------------------------------------*
form componentallocation_ewb  using i_matnr like marc-matnr
                                    i_werks like marc-werks
                                    i_stlan like mast-stlan
                                    i_stlal like mast-stlal
                                    i_plnnr type plnnr
                                    i_plnal 
type plnal
                                    i_plnty 
like plpo-plnty
                                    i_plnfl type plnfolge.

  dataft_opr_class_data type standard table of opr_class_data.
  dataft_itm_tree_data type standard table of itm_tree_class_data.
  dataft_com_class_data type standard table of com_class_data.
  dataft_opr_all_data_del type cpcl_opr_tab_type.
  datafs_com_ident_delete type cmcl_com_ident_type.
  datafs_itm_ident type  cscl_itm_id_xt_type.
  datafs_opr_ident_new type  cpcl_opr_type.
  dataft_opr_ident type cpcl_opr_tab_type.
  dataft_opr_ref type  cpcl_opr_ref_set_tab_type.
  dataft_ident_delete_tab type cmcl_com_tab_type.
  datafs_com_data type com_class_data.
  dataf_error type c.
  field-symbols<opr_all_dell> like line of ft_opr_all_data_del.
  field-symbols<com_class_data> like line of ft_com_class_data.
  field-symbols<oprdata> like line of ft_opr_class_data.
  field-symbols<itmdata> like line of ft_itm_tree_data.

  "read BOM and ROUTING data to memory
  perform load_task_list
  
using i_matnr
        i_werks
        i_plnty
        i_plnnr
        i_plnal
        sy
-datum.


  "get items tree
  call function 'CP_CC_S_PROVIDE_ITM_BY_AS_PATH'
  exporting
    i_key_date_s                   sy-datum
    i_application                  'PP01'
    i_material_root                i_matnr
    i_plant_root                   
i_werks
    i_usage_root                   
i_stlan
    i_alternative_root             
i_stlal
*   I_SALES_ORDER_ROOT              = I_SALES_ORDER_ROOT
*   I_SALES_ORDER_ITEM_ROOT         = I_SALES_ORDER_ITEM_ROOT
*   I_REQUIRED_QUANTITY_ROOT        = 1
*   I_PATH                          = ' '
*    I_FLG_PATH_PROVIDE_ONLY         = ' '
    i_flg_path_expand_totally       'X'
*   I_FILTER_EXPRESSION             = I_FILTER_EXPRESSION
*   I_VERID                         = I_VERID
* IMPORTING
*   E_WORKAREA                      = E_WORKAREA
*   E_DATE_FROM                     = E_DATE_FROM
*   E_DATE_TO                       = E_DATE_TO
*   E_FLG_ECM_EVALUATION            = E_FLG_ECM_EVALUATION
  tables
    c_itm_tree_class_data           ft_itm_tree_data
  
exceptions
    key_date_required_for_ecm       1
    others                          2
    .
  if sy-subrc eq 0.
    "after getting BOM we need to check it ant rework a little
    perform item_check  tables  ft_itm_tree_data.

    "get operations data
    call function 'CP_CC_S_PROVIDE_COM_BY_OPR'
      exporting
        i_date_from                    sy-datum
        i_date_to                      sy-datum
        i_application                  'PP01'
        i_material_root                i_matnr
        i_plant_root                   
i_werks
        i_usage_root                   
i_stlan
        i_alternative_root             
i_stlal
*       I_SALES_ORDER_ROOT             = I_SALES_ORDER_ROOT
*       I_SALES_ORDER_ITEM_ROOT        = I_SALES_ORDER_ITEM_ROOT
*       I_REQUIRED_QUANTITY_ROOT       = I_REQUIRED_QUANTITY_ROOT
       i_plnty                        i_plnty
       i_plnnr                        
i_plnnr
       i_plnal                        
i_plnal
       i_plnfl                        
i_plnfl
*       I_PLNKN                        = I_PLNKN
*       I_IDENT_OPR                    = I_IDENT_OPR
*       I_IDENT_REF_OPR                = I_IDENT_REF_OPR
*       I_VORNR_OPR                    = I_VORNR_OPR
*       I_IDENT_ITM                    = I_IDENT_ITM
*       I_FILTER_EXPRESSION            = I_FILTER_EXPRESSION
*       I_VERID                        = I_VERID
      tables
        e_com_class_data               ft_com_class_data
     
exceptions
       no_authority                   1
       wrong_key                      2
       bom_not_found                  3
       no_valid_bom                   4
       bom_not_active                 5
       others                         6
              .
    if sy-subrc eq 0.

     "delete current assignment of components by setting LOEKZ on PLMZ (save directly after)
      clear f_error .
      loop at ft_com_class_data assigning <com_class_data>.
        call function 'CP_CL_P_OPR_ALLOCATION_PROVIDE'
          exporting
            i_date_from sy-datum
            i_plnty     <com_class_data>-plnty
            i_plnnr     
<com_class_data>-plnnr
            i_plnkn     
<com_class_data>-plnkn
          
importing
            e_opr_ident ft_opr_all_data_del
          
exceptions
            wrong_key   1.
        if sy-subrc eq 0.
          move-corresponding <com_class_data> to fs_com_ident_delete.
*       delete current component allocation
          call function 'CM_CL_P_COM_DELETE'
            exporting            "i_ecn_s = i_ecn_s
              i_key_date_s     sy-datum
              i_com_ident      fs_com_ident_delete
              i_opr_alloc_data 
ft_opr_all_data_del[]
            
exceptions
              no_valid_com     1
              path_incomplete  2.
          if sy-subrc ne 0.
            f_error 'X'.
            perform add_message
                        
using
                            'W'
                            '001'
                            '00'
                            'Could not delete allocation.'
                            space
                            space
                            space
                            i_matnr
                            i_werks
                            <com_class_data>
-plnnr
                            <com_class_data>-plnal
                            <com_class_data>
-idnrk
                            c_log-delete.

          endif.
        endif.
      endloop.

      "save data (deletion of current assignment)
      call function 'CP_CC_S_SAVE'.
      if p_test is initial .
        commit work.
      else.
        rollback work.
      endif.

    endif.

  else.
    perform add_message
    
using
          'E'
          '001'
          '00'
          'Could not find BOM for'
          i_matnr
          i_werks
          space
          i_matnr
          i_werks
          i_plnnr
          i_plnal
          space
          c_log
-create.

  endif.

"read operation data
  call function 'CP_CC_S_OPR_PROVIDE_BY_MTK'
   exporting
     i_date_from                        sy-datum
     i_date_to                          sy-datum
     i_material                         i_matnr
     i_plant                            
i_werks
*   I_SALES_ORDER                      = I_SALES_ORDER
*   I_SALES_ORDER_ITEM                 = I_SALES_ORDER_ITEM
     i_plnty                            i_plnty
     i_plnnr                            
i_plnnr
     i_plnal                            
i_plnal
     i_plnfl                            
i_plnfl
*   I_PLNKN                            = I_PLNKN
*   I_IDENT                            = I_IDENT
*   I_VORNR                            = I_VORNR
*   I_FLG_SUB_OPR_PROVIDE              = ' '
*   I_FLG_TSK_GROUP_TYPE_PROVIDE       = ' '
*   I_FLG_PROVIDE_INV                  = ' '
*   I_FILTER_EXPRESSION                = I_FILTER_EXPRESSION
* IMPORTING
*   E_OPR_INV                          = E_OPR_INV
    tables
      e_opr_class_data                   ft_opr_class_data
   
exceptions
     wrong_key                          1
     others                             2
            .
  if sy-subrc eq 0.
    sort ft_opr_class_data by plnal plnfl plnty plnnr vornr.
  endif.

"create components allocation
  loop at ft_itm_tree_data assigning <itmdata>.
    clear fs_com_data.
    if <itmdata>-sortf eq 'SKIP'"skip components of sub-assemblies which are not phantoms
      continue.
    endif.
    move-corresponding <itmdata> to fs_com_data.
    move <itmdata>-stufe_k to fs_com_data-stlst"order level

    if <itmdata>-dumps ne 'X' and fs_com_data-stlst is not initial and
       <itmdata>-stlnr_w eq <itmdata>-stlnr.
      "we have to calculate proper order level manually....
      subtract from fs_com_data-stlst.
    endif.

    "read if we have assignment to operation in out internal table
    "if yes the we create assignment
    read table gt_assignment assigning <assign> with key matnr i_matnr
                                                         werks 
i_werks
                                                         plnnr 
i_plnnr
                                                         plnfl 
i_plnfl
                                                         plnal 
i_plnal
                                                         plnty 
i_plnty
                                                         stlnr 
fs_com_data-stlnr
                                                         stlal fs_com_data-stlal
                                                         stlan i_stlan
                                                         posnr 
fs_com_data-posnr
                                                         idnrk fs_com_data-idnrk
                                                         stlst fs_com_data-stlst
                                                         binary search.
    if sy-subrc eq 0.
      "read operation data
      read table ft_opr_class_data assigning <oprdata> with key plnal <assign>-plnal
                                                                plnfl 
<assign>-plnfl
                                                                plnty 
<assign>-plnty
                                                                plnnr 
<assign>-plnnr
                                                                vornr 
<assign>-vornr
                                                                
binary search.
      if sy-subrc eq 0.
        "move proper data from operation and item data to component allocation structure
        move-corresponding <oprdata>  to   fs_com_data.
        move<oprdata>-ident         to fs_com_data-ident.
        move<itmdata>-ident         to fs_com_data-ident_itm.
        move<itmdata>-kante_v       to fs_com_data-kante.
        move<itmdata>-kante_v       to fs_com_data-kante_v.
        move<itmdata>-stlty         to fs_com_data-stlty.
        move<itmdata>-stlnr         to fs_com_data-stlnr.
        move<itmdata>-stlal         to fs_com_data-stlal.
        move<itmdata>-stvkn         to fs_com_data-stlkn.
        movei_matnr                 to fs_com_data-bomat.
        movei_werks                 to fs_com_data-werk_stl.

        if fs_com_data-vornr is not initial.

          "create allocation
          call function 'CP_CC_S_CREATE_COM'
          exporting
*         I_ECN_S                         = ' '
            i_key_date_s                    sy-datum
            i_com_class_data                
fs_com_data
*       IMPORTING
*         E_ITM_LOCK                      = E_ITM_LOCK
*         E_OPR_LOCK                      = E_OPR_LOCK
*         E_OPR_IDENT_ALLOC_INV           = E_OPR_IDENT_ALLOC_INV
*         E_ZUONR                         = E_ZUONR
*         E_COM_HANDLE_DEL_TAB            = E_COM_HANDLE_DEL_TAB
*         E_ECM_DATA_ERROR_TYPE           = E_ECM_DATA_ERROR_TYPE
          exceptions
            no_authority                    1
            no_valid_operation              2
            no_valid_item                   3
            operation_already_locked        4
            item_already_locked             5
            operation_not_locked            6
            item_not_locked                 7
            item_not_specified              8
            operation_not_specified         9
            ecm_data_not_suitable           10
            allocation_not_valid            11
            link_to_ref_set_of_opr          12
            link_to_wc_ref_set_of_opr       13
            others                          14
            .
          if sy-subrc <> 0.
            f_error 'X'.
            perform add_message
            
using
                  'E'
                  '001'
                  '00'
                  'Could not create assignment'
                  i_matnr
                  i_werks
                  i_plnnr
                  i_matnr
                  i_werks
                  i_plnnr
                  i_plnal
                  fs_com_data
-idnrk
                  c_log
-create.

          endif.
        endif.
      else.
        perform add_message
        
using
              'E'
              '001'
              '00'
              'Could not find operation'
              <assign>-vornr
              i_plnnr
              i_plnal
              i_matnr
              i_werks
              i_plnnr
              i_plnal
              fs_com_data
-idnrk
              c_log
-create.
      endif.
    endif.
  endloop.


  call function 'CP_CC_S_SAVE'.
  if p_test is initial and f_error is initial.
    "save only in case of no error appeared
    commit work.
  else.
    rollback work.
  endif.


endform.                    " COMPONENTALLOCATION_EWB

*&---------------------------------------------------------------------*
*&      Form  load_excel
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form load_excel.
  "ole copy of excel data,
  dataft_excel type standard table of alsmex_tabline.
  field-symbols<excel> like line of ft_excel.
  dataf_rows type i.

" Excel columns in proper order.
"MATNR  WERKS PLNNR PLNFL PLNAL PLNTY VORNR STLNR STLAL STLAN POSNR IDNRK STLST

  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    exporting
      filename                p_path
      i_begin_col             
1
      i_begin_row             2
      i_end_col               13
      i_end_row               p_rows
    
tables
      intern                  ft_excel
    
exceptions
      inconsistent_parameters 1
      upload_ole              2
      others                  3.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
            
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

  datafs_assign type ty_assignment.
  datafs_plan type ty_plan.
  loop at ft_excel assigning <excel>.
    case <excel>-col.
      when 1.
        clearfs_assignfs_plan.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-matnr.

        fs_plan-matnr   fs_assign-matnr.
      when 2.
        fs_assign-werks <excel>-value.
        fs_plan-werks   <excel>-value.
      when 3.
        fs_assign-plnnr <excel>-value.
        fs_plan-plnnr   <excel>-value.
      when 4.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-plnfl.
        fs_plan-plnfl   fs_assign-plnfl.
      when 5.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-plnal.
        fs_plan-plnal   fs_assign-plnal.
      when 6.
        fs_assign-plnty <excel>-value.
        fs_plan-plnty   <excel>-value.
      when 7.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-vornr.
      when 8.
        fs_assign-stlnr <excel>-value.
      when 9.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-stlal.
        fs_plan-stlal fs_assign-stlal.
      when 10.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-stlan.
        fs_plan-stlan fs_assign-stlan.
      when 11.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-posnr.
      when 12.

        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-idnrk.

        if fs_plan-matnr in s_matnr and
           fs_plan-werks in s_werks and
           fs_plan-plnnr in s_plnnr .

        endif.
      when 13.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  <excel>-value
          importing
            output fs_assign-stlst.

        "collect data it's the last cell so we append row to table
        collect fs_plan into gt_plan.
        append fs_assign to gt_assignment.
    endcase.
    delete table ft_excel from <excel>.
  endloop.

  delete gt_assignment where matnr is initial.

  "checks of data.
  loop at gt_assignment assigning <assign>.

    "check if material exists in plant
    select single matnr into <assign>-matnr
      
from marc
      
where matnr eq <assign>-matnr
      
and werks   eq <assign>-werks.
    if sy-subrc ne 0"material not opened in plant
      "error message here
      "stop processing  "continue to next routing
      perform add_message
      
using
            'E'
            '001'
            '00'
            'Material: '
            <assign>-matnr
            
'not opened on plant: '
            <assign>-werks
            <assign>
-matnr
            <assign>
-werks
            <assign>
-plnnr
            <assign>
-plnal
            space
            c_log
-read.
      delete gt_assignment where matnr <assign>-matnr
                            
and werks <assign>-werks
                            
and plnty <assign>-plnty
                            
and plnnr <assign>-plnnr
                            
and plnal <assign>-plnal
                            
and plnfl <assign>-plnfl.
      continue.
    endif.


    "check if material exists in plant
    select single matnr into <assign>-idnrk
    
from marc
    
where matnr eq <assign>-idnrk
    
and werks   eq <assign>-werks.
    if sy-subrc ne 0"material not opened in plant
      "error message here
      "stop processing  "continue to next routing
      perform add_message
      
using
            'E'
            '001'
            '00'
            'Component: '
            <assign>-idnrk
            
'not opened on plant: '
            <assign>-werks
            <assign>
-matnr
            <assign>
-werks
            <assign>
-plnnr
            <assign>
-plnal
            space
            c_log
-read.
      delete gt_assignment where matnr <assign>-matnr
                              
and werks <assign>-werks
                              
and plnty <assign>-plnty
                              
and plnnr <assign>-plnnr
                              
and plnal <assign>-plnal
                              
and plnfl <assign>-plnfl.
      continue.
    endif.



    "check if routing exist in plant
    select single matnr into <assign>-matnr
      
from mapl
      
where matnr eq <assign>-matnr
      
and werks eq <assign>-werks
      
and plnty eq <assign>-plnty
      
and plnnr eq <assign>-plnnr
      
and plnal eq <assign>-plnal.
    if sy-subrc ne 0.
      "error message here
      "stop processing  "continue to next routing
      perform add_message
      
using
            'E'
            '001'
            '00'
            'Routing not found: '
            <assign>-plnty
            <assign>
-plnnr
            <assign>
-plnal
            <assign>
-matnr
            <assign>
-werks
            <assign>
-plnnr
            <assign>
-plnal
            space
            c_log
-read.
      delete gt_assignment where matnr <assign>-matnr
                            
and werks <assign>-werks
                            
and plnty <assign>-plnty
                            
and plnnr <assign>-plnnr
                            
and plnal <assign>-plnal
                            
and plnfl <assign>-plnfl.
      continue.
    endif.

    if <assign>-stlnr is initial.
      select single stlnr
        
into <assign>-stlnr
        
from mast
        
where matnr eq <assign>-matnr
        
and werks eq <assign>-werks
        
and stlan eq <assign>-stlan
        
and stlal eq <assign>-stlal.
      if sy-subrc ne 0.
        perform add_message
        
using
              'E'
              '001'
              '00'
              'Could not find BOM for'
              <assign>-matnr
              <assign>
-werks
              space
              <assign>
-matnr
              <assign>
-werks
              <assign>
-plnnr
              <assign>
-plnal
              space
              c_log
-read.

        delete gt_assignment where matnr <assign>-matnr
        
and werks <assign>-werks
        
and plnty <assign>-plnty
        
and plnnr <assign>-plnnr
        
and plnal <assign>-plnal
        
and plnfl <assign>-plnfl.

        continue.
      endif.
    else.
      select single stlnr
      
into <assign>-stlnr
      
from stpo
      
where idnrk eq <assign>-idnrk
      
and   stlnr  eq  <assign>-stlnr.
      if sy-subrc ne 0.
        perform add_message
        
using
              'E'
              '001'
              '00'
              'Could not find BOM for'
              <assign>-idnrk
              <assign>
-werks
              space
              <assign>
-idnrk
              <assign>
-werks
              <assign>
-plnnr
              <assign>
-plnal
              space
              c_log
-read.

        delete gt_assignment where matnr <assign>-matnr
        
and werks <assign>-werks
        
and plnty <assign>-plnty
        
and plnnr <assign>-plnnr
        
and plnal <assign>-plnal
        
and plnfl <assign>-plnfl.

        continue.
      endif.
    endif.
  endloop.

  sort gt_assignment by matnr werks plnnr plnfl plnal plnty stlnr stlal stlan posnr idnrk stlst.
  sort gt_plan by matnr werks plnty plnnr plnal plnfl.

endform.                    "load_excel

*&---------------------------------------------------------------------*
*&      Form  create_assignment
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form create_assignment.

  loop at gt_plan assigning <plan>.

    perform componentallocation_ewb
                
using
                   <plan>-matnr
                   <plan>
-werks
                   <plan>
-stlan
                   <plan>
-stlal
                   <plan>
-plnnr
                   <plan>
-plnal
                   <plan>
-plnty
                   <plan>
-plnfl.

  endloop.

endform.                    "create_assignment

*&---------------------------------------------------------------------*
*&      Form  add_message
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->I_TYPE     text
*      -->I_NUMBER   text
*      -->I_ID       text
*      -->I_V1       text
*      -->I_V2       text
*      -->I_V3       text
*      -->I_V4       text
*      -->I_MATNR    text
*      -->I_WERKS    text
*      -->I_PLNNR    text
*      -->I_PLNAL    text
*      -->I_IDNRK    text
*----------------------------------------------------------------------*
form add_message using value(i_type)
                       value(i_number)
                       value(i_id)
                       value(i_v1)
                       value(i_v2)
                       value(i_v3)
                       value(i_v4)
                       value(i_matnr)
                       value(i_werks)
                       value(i_plnnr)
                       value(i_plnal)
                       value(i_idnrk)
                       value(i_log_no).
  clear gs_return.


  gs_return-type                    i_type.
  gs_return-id                      i_id.
  gs_return-number                  i_number.
  gs_return-message_v1              i_v1.
  gs_return-message_v2              i_v2.
  gs_return-message_v3              i_v3.
  gs_return-message_v4              i_v4.
  gs_return-log_no                  i_log_no.
  message id i_id type i_type number i_number with i_v1 i_v2 i_v3 i_v4 into gs_return-message.

  concatenate i_matnr i_werks i_plnnr into gs_return-parameter separated by space.
  concatenate i_plnal i_idnrk into gs_return-field separated by space.
  "gs_return-row
  "gs_return-system

  append gs_return to gt_return.
  clear gs_return.

endform.                    "add_message

*&---------------------------------------------------------------------*
*&      Form  display_log
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form display_log.
  datafs_layout type slis_layout_alv.

  fs_layout-colwidth_optimize 'X'.

  if gt_return[] is initial.
    perform add_message
                
using
                    'S'
                    '001'
                    '00'
                    'No errors appeared'
                    space
                    space
                    space
                    space
                    space
                    space
                    space
                    space
                    c_log
-read.

  endif.

  call function 'REUSE_ALV_GRID_DISPLAY'
   exporting
*     I_INTERFACE_CHECK                 = ' '
*     I_BYPASSING_BUFFER                = ' '
*     I_BUFFER_ACTIVE                   = ' '
      i_callback_program                sy-repid
      i_callback_pf_status_set          
'SET_PF_STATUS'
      i_callback_user_command           'USER_COMMAND'
*     I_CALLBACK_TOP_OF_PAGE            = ' '
*     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*     I_CALLBACK_HTML_END_OF_LIST       = ' '
     i_structure_name                  'BAPIRET2'
*     I_BACKGROUND_ID                   = ' '
*     I_GRID_TITLE                      = I_GRID_TITLE
*     I_GRID_SETTINGS                   = I_GRID_SETTINGS
      is_layout                         fs_layout
*     IT_FIELDCAT                       = IT_FIELDCAT
*     IT_EXCLUDING                      = IT_EXCLUDING
*     IT_SPECIAL_GROUPS                 = IT_SPECIAL_GROUPS
*     IT_SORT                           = IT_SORT
*     IT_FILTER                         = IT_FILTER
*     IS_SEL_HIDE                       = IS_SEL_HIDE
*     I_DEFAULT                         = 'X'
*     I_SAVE                            = ' '
*     IS_VARIANT                        = IS_VARIANT
*     IT_EVENTS                         = IT_EVENTS
*     IT_EVENT_EXIT                     = IT_EVENT_EXIT
*     IS_PRINT                          = IS_PRINT
*     IS_REPREP_ID                      = IS_REPREP_ID
*     I_SCREEN_START_COLUMN             = 0
*     I_SCREEN_START_LINE               = 0
*     I_SCREEN_END_COLUMN               = 0
*     I_SCREEN_END_LINE                 = 0
*     I_HTML_HEIGHT_TOP                 = 0
*     I_HTML_HEIGHT_END                 = 0
*     IT_ALV_GRAPHICS                   = IT_ALV_GRAPHICS
*     IT_HYPERLINK                      = IT_HYPERLINK
*     IT_ADD_FIELDCAT                   = IT_ADD_FIELDCAT
*     IT_EXCEPT_QINFO                   = IT_EXCEPT_QINFO
*     IR_SALV_FULLSCREEN_ADAPTER        = IR_SALV_FULLSCREEN_ADAPTER
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER           = E_EXIT_CAUSED_BY_CALLER
*     ES_EXIT_CAUSED_BY_USER            = ES_EXIT_CAUSED_BY_USER
    tables
      t_outtab                          gt_return[]
   
exceptions
     program_error                     1
     others                            2
            .
  if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  endif.


endform.                    "display_log

*&---------------------------------------------------------------------*
*&      Form  set_pf_status
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->TT_EXTAB   text
*----------------------------------------------------------------------*
form set_pf_status using tt_extab type slis_t_extab.

  set pf-status 'STANDARD' of program sy-repid immediately.

endform.                    "set_pf_status

*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->F_UCOMM      text
*      -->FS_SELFIELD  text
*----------------------------------------------------------------------*
form user_command  using f_ucomm like sy-ucomm
                         fs_selfield 
type slis_selfield.

  dataft_return type bapiret2_t.
  case f_ucomm.
    when 'CREATE_ASS'.
      perform create_assignment.
      ft_return[] gt_return[].
      sort ft_return by log_no.
      read table ft_return with key log_no c_log-delete transporting no fields binary search.
      if sy-subrc ne 0.
        perform add_message
        
using
              'S'
              '001'
              '00'
              'No errors appeared'
              space
              space
              space
              space
              space
              space
              space
              space
              c_log
-delete.
      endif.
      read table ft_return with key log_no c_log-create transporting no fields binary search.
      if sy-subrc ne 0.
        perform add_message
        
using
              'S'
              '001'
              '00'
              'No errors appeared'
              space
              space
              space
              space
              space
              space
              space
              space
              c_log
-create.
      endif.

      fs_selfield-refresh 'X'.
  endcase.


endform.                    "USER_COMMAND


*&---------------------------------------------------------------------*
*&      Form  item_check
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->FT_ITEM_CLASS_DATA  text
*----------------------------------------------------------------------*
form item_check tables   ft_item_class_data structure itm_tree_class_data.
  field-symbols<itmdata> type itm_tree_class_data.
  dataf_kante type plmz-kante.
  dataf_kante_global type plmz-kante.
  field-symbols<prev_dummy_dumps> type itm_tree_class_data.


  read table ft_item_class_data index assigning <prev_dummy_dumps>.

  loop at ft_item_class_data assigning <itmdata>.
    clear <itmdata>-handle.
    clear <itmdata>-kante_v.
    clear <itmdata>-sortf.

    if <prev_dummy_dumps>-stlnr ne <itmdata>-stlnr and <prev_dummy_dumps>-dumps ne 'X'.
      "If we have sub-assembly which is not phantom it still has to be taken into accout
      "when calculating kante_v but it must be skipped during creation of assignments
      <itmdata>-sortf 'SKIP'.
      "I mark in sanfe field that this is sub-assembly without phantom
      <prev_dummy_dumps>-sanfe 'S'.
      continue.
    endif.


    if <itmdata>-dumps eq 'X'.
      read table ft_item_class_data index sy-tabix assigning <prev_dummy_dumps>.
    endif.
  endloop.


  loop at ft_item_class_data assigning <itmdata>.
    if <itmdata>-handle is not initial.
      continue.
    endif.
    <itmdata>-handle sy-tabix.
    if <itmdata>-dumps eq 'X' .
      add to f_kante_global.
      <itmdata>-kante_v f_kante_global.
      perform recursive_item_check tables ft_item_class_data
                                   
using <itmdata>
                                   
changing <itmdata>-kante_v
                                            f_kante_global
.


    elseif <itmdata>-sanfe eq 'S'"dummy code for not needed entry
      dataf_dummy_kante type plmz-kante.
      add to f_kante_global.
      perform recursive_item_check tables ft_item_class_data
                                  
using <itmdata>
                                  
changing f_dummy_kante
                                    f_kante_global
.

    elseif <itmdata>-itsob eq '52'.
      add to f_kante_global.
      <itmdata>-kante_v f_kante_global.
    else.
      clear <itmdata>-kante_v.
    endif.


  endloop.

endform.                    "item_check

*&---------------------------------------------------------------------*
*&      Form  recursive_item_check
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->FT_ITEM_CLASS_DATA  text
*      -->FS_ITEM             text
*      -->F_KANTE             text
*      -->F_KANTE_GLOBAL      text
*----------------------------------------------------------------------*
form recursive_item_check  tables   ft_item_class_data structure itm_tree_class_data
                            
using    fs_item type itm_tree_class_data
                        
changing  f_kante type plmz-kante
                                  f_kante_global 
type plmz-kante.


  field-symbols<itmdata> type itm_tree_class_data.
  datals_items type itm_tree_class_data.
  dataf_tabix type sy-tabix.
  dataf_stlnr type stko-stlnr.

  f_tabix fs_item-handle + .

  read table ft_item_class_data assigning <itmdata> index f_tabix.
  if sy-subrc eq 0.
    f_stlnr <itmdata>-stlnr.
  endif.
  loop at ft_item_class_data assigning <itmdata> from f_tabix .
    if <itmdata>-stlnr ne f_stlnr.
      exit.
    endif.
    <itmdata>-handle sy-tabix.

    if <itmdata>-dumps eq 'X' .
      add to f_kante_global.
      <itmdata>-kante_v f_kante_global.

      perform recursive_item_check tables ft_item_class_data
                                
using
            <itmdata>
      
changing
        <itmdata>-kante_v
        f_kante_global 
.
    elseif <itmdata>-sanfe eq 'S'"dummy code for not needed entriy
      dataf_dummy_kante type plmz-kante.
      add to f_kante_global.
      perform recursive_item_check tables ft_item_class_data
      
using <itmdata>
      
changing f_dummy_kante
        f_kante_global
.

    elseif <itmdata>-itsob eq '52'.
      add to f_kante_global.
      <itmdata>-kante_v f_kante_global.

    else.
      <itmdata>-kante_v f_kante"path in expl
    endif.
  endloop.

endform.

Enjoy!
 
-----------
As promised here is an example of how to filling excel file for following BOM.
 
 
 
Where SLST column is taken from CA02 and level for components allocation: