ABAP FM Create Material Variant Configuration with Reference (CUCB_COPY_CONFIGURATION)

In this blog post, I will share a function module to copy Variant Configuration of an existing material and paste it to another one.

We will use CUCB_GET_CONFIGURATION, CUCB_COPY_CONFIGURATION and CUCB_SET_CONFIGURATION standard function modules.

It takes reference material number and the material number itself as parameter. (Plant is optional. I used 2000 as plant number.) And it returns Variant Configuration Instance Number (MARA-CUOBF) which you may need for further processing.

Here is the source code:

ZMA_F_CREATE_VARIANT_CONFIG_WR

Import Parameters:

P_MATCFG
TYPE
MARA-MATNR
Reference Material

F_MATNR
TYPE
MARA-MATNR
The Material to Create Variant Configuration

P_WERKS
TYPE
WERKS-WERKS
Plant

Export Parameter:

E_NEW_INSTANCE
TYPE
MARA-CUOBF
Internal object number

function zma_f_create_variant_config_wr. *“———————————————————————- *”*“Local Interface: *” IMPORTING *“ REFERENCE(P_MATCFG) TYPE MARA-MATNR *” REFERENCE(F_MATNR) TYPE MARA-MATNR *“ REFERENCE(P_WERKS) TYPE WERKS-WERKS OPTIONAL *” EXPORTING *“ REFERENCE(E_NEW_INSTANCE) TYPE MARA-CUOBF *”———————————————————————- type-pools: ibco2. data: ref_cuobf type mara-cuobf. select single cuobf from mara into ref_cuobf where matnr = p_matcfg. if sy-dbcnt > 0. data: lv_satnr type mara-satnr. select single satnr from mara into lv_satnr where matnr = p_matcfg. data: instance type cuib_cuobj, ibase type ibco2_ibase_rec, configuration type ibco2_instance_tab2, wa_configuration like line of configuration, eo_cbase_ref type ref to if_cbase_e. data: iv_instance type cuib_cuobj, new_instance type cuib_cuobj, lv_root_inst type cuib_cuobj, lt_cfg type ibco2_instance_tab2, ls_cfg like line of lt_cfg, lt_match type cuib_match_instance_tab, ibase_copy type ibco2_ibase_rec, es_instance_rec type ibco2_instance_rec2, configuration_copy type ibco2_instance_tab2, wa_config_copy like line of configuration, eo_cbase_ref_copy type ref to if_cbase_e. if p_werks = “. move ref_cuobf to instance. call function ‘CUCB_GET_CONFIGURATION’ exporting instance = instance importing ibase = ibase configuration = configuration eo_cbase_ref = eo_cbase_ref exceptions invalid_input = 1 invalid_instance = 2 instance_is_a_classification = 3 others = 4. refresh: lt_cfg, lt_match. call function ‘CUCB_COPY_CONFIGURATION’ exporting source_instance = instance importing configuration = lt_cfg et_match_instance = lt_match es_instance_rec = es_instance_rec exceptions invalid_instance = 1 instance_is_a_classification = 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. data: owner type ibxx_business_object. data: lv_ibase type p. loop at lt_cfg into ls_cfg. lv_root_inst = ls_cfg-instance. owner-object_type = ‘MARAT’. owner-object_key = f_matnr. ls_cfg-owner = owner. modify lt_cfg from ls_cfg. endloop. call function ‘CUCB_SET_CONFIGURATION’ exporting root_instance = lv_root_inst changing configuration = lt_cfg exceptions invalid_input = 1 invalid_instance = 2 instance_is_a_classification = 3 others = 4. call function ‘BAPI_TRANSACTION_COMMIT’ exporting "wait = ‘X’ wait = ‘X’. call function ‘CUCB_CONFIGURATION_TO_DB’ exporting root_instance = lv_root_inst root_object = ls_cfg-owner force_new_instance = ‘X’ * IV_WITHOUT_COMMIT_UPDATE = ’ ’ iv_material = f_matnr * IV_LOCATION = ‘2000’ importing new_instance = new_instance * TABLES * EXP_NEW_NESTED_CUOBJS = exceptions invalid_instance = 1 invalid_root_instance = 2 no_changes = 3 already_registered_for_update = 4 instance_is_a_classification = 5 others = 6. if sy-subrc 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. else. data: ls_bapiret3 type bapiret2. call function ‘BAPI_TRANSACTION_COMMIT’ exporting "wait = ‘X’ wait = ‘X’ importing return = ls_bapiret3. data: wa_mara type mara. select * from mara into wa_mara where matnr = f_matnr. wa_mara-satnr = lv_satnr. wa_mara-cuobf = new_instance. update mara from wa_mara. endselect. e_new_instance = new_instance. endif. elseif p_werks = ‘2000’. ” For Werks 2000 clear: instance. data: lv_objkey type ibinown-objkey. concatenate p_matcfg p_werks into lv_objkey. select single instance from ibinown into instance where objkey = lv_objkey. call function ‘CUCB_GET_CONFIGURATION’ exporting instance = instance importing ibase = ibase configuration = configuration eo_cbase_ref = eo_cbase_ref exceptions invalid_input = 1 invalid_instance = 2 instance_is_a_classification = 3 others = 4. refresh: lt_cfg, lt_match. “ Take MARC instance call function ‘CUCB_COPY_CONFIGURATION’ exporting source_instance = instance importing configuration = lt_cfg et_match_instance = lt_match exceptions invalid_instance = 1 instance_is_a_classification = 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. clear: owner. data: type_of type ibxx_business_object. loop at lt_cfg into ls_cfg. lv_root_inst = ls_cfg-instance. owner-object_type = ‘MARC’. owner-object_key = f_matnr. concatenate owner-object_key p_werks into owner-object_key. ls_cfg-owner = owner. modify lt_cfg from ls_cfg. endloop. call function ‘CUCB_SET_CONFIGURATION’ exporting root_instance = lv_root_inst changing configuration = lt_cfg exceptions invalid_input = 1 invalid_instance = 2 instance_is_a_classification = 3 others = 4. call function ‘BAPI_TRANSACTION_COMMIT’ exporting "wait = ‘X’ wait = ‘X’. call function ‘CUCB_CONFIGURATION_TO_DB’ exporting root_instance = lv_root_inst root_object = ls_cfg-owner force_new_instance = ‘X’ * IV_WITHOUT_COMMIT_UPDATE = ’ ’ iv_material = f_matnr iv_location = ‘2000’ importing new_instance = new_instance * TABLES * EXP_NEW_NESTED_CUOBJS = exceptions invalid_instance = 1 invalid_root_instance = 2 no_changes = 3 already_registered_for_update = 4 instance_is_a_classification = 5 others = 6. if sy-subrc 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. else. clear: ls_bapiret3. call function ‘BAPI_TRANSACTION_COMMIT’ exporting "wait = ‘X’ wait = ‘X’ importing return = ls_bapiret3. data: wa_marc type marc. select * from marc into wa_marc where matnr = f_matnr. wa_marc-stdpd = lv_satnr. wa_marc-cuobj = new_instance. update marc from wa_marc. endselect. e_new_instance = new_instance. endif. endif. endif. endfunction. http://bit.ly/2zA0NSa #SAP #SAPCloud #AI