Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

This was developed on CRM 2007 and may work on CRM 4.0

A quite common problem in the last projects was the lack of standard functionality to copy trees of BOL nodes dynamically.

This is for example necessary when editing sales area data of business partners. Sales Area data often contains many fields or tables which have to be maintained for each Sales Area. The most of the fields are however similar for more than one sales area and it might be interesting to copy the values from one Sales Area to another.

This example shall explain how to copy the data from one object to another.

Declares the local data:

TYPES:
  BEGIN OF ltype_child_object,
    parent TYPE crmt_ext_obj_name,
    relation TYPE crmt_relation_name,
    objects TYPE REF TO if_bol_entity_col,
  END OF ltype_child_object.
DATA:
    lr_entity       TYPE REF TO cl_crm_bol_entity,
    lr_obj_to_copy     TYPE REF TO cl_crm_bol_entity,
    lr_orig_rel     TYPE REF TO if_bol_entity_col,
    lr_child_ent    TYPE REF TO cl_crm_bol_entity,
    lr_header       TYPE REF TO cl_crm_bol_entity,
    lv_index        TYPE int4,
    ls_sales_area   TYPE crmt_bus_sales_area,
    lt_return       TYPE bapiret2_t,
    ls_return       TYPE bapiret2,
    lr_object_model  TYPE REF TO if_genil_obj_model,
    lr_object_model_   TYPE REF TO cl_crm_genil_obj_model,
    lr_relations    TYPE REF TO crmt_relation_name_tab,
    lv_rel_name     TYPE crmt_relation_name,
    lt_relations    TYPE crmt_relation_name_tab,
    lt_components_loaded TYPE genil_component_tab,
    ls_child             TYPE ltype_child_object,
    lt_children          TYPE TABLE OF ltype_child_object,
    lr_rel_ent        TYPE REF TO cl_crm_bol_entity,
    lv_att_struc_name TYPE strukname,
    ref_rowtype       TYPE REF TO cl_abap_structdescr,
    lv_relname        TYPE crmt_relation_name,
    lv_is_11          TYPE crmt_boolean,
    ref_wa            TYPE REF TO data.

  FIELD-SYMBOLS: <attr> TYPE ANY,
                 <value> TYPE ANY TABLE.

Gets the components loaded and fetches the sub relations and assignes it to relations table

lr_object_model = cl_crm_genil_model_service=>get_runtime_model( ).
      lr_object_model_ ?= lr_object_model.
      lt_components_loaded = lr_object_model_->get_components_loaded( ).

      lr_relations = lr_object_model->get_modeled_relations( iv_object_name = 'BuilSalesArrangement' ).
      ASSIGN lr_relations->* TO <value>.
      lt_relations = <value>.

fetch all child objects and append to table

LOOP AT lt_relations INTO lv_rel_name.
        CLEAR: ls_child.

        ls_child-parent = lr_obj_to_copy->get_name( ).
        lr_orig_rel = lr_obj_to_copy->get_related_entities(
                    iv_mode = cl_crm_bol_entity=>bypassing_buffer
                    iv_relation_name = lv_rel_name ).

        ls_child-relation = lv_rel_name.
        ls_child-objects ?= lr_orig_rel.
        APPEND ls_child TO lt_children.
      ENDLOOP.

Create a new BuilSalesArrangement object

TRY.
          lr_entity = lr_header->create_related_entity(
           iv_relation_name = 'BuilSalesArrangementRel' ).
        CATCH cx_crm_genil_model_error cx_crm_genil_duplicate_rel.
*               should never happen
      ENDTRY.

Bind the newly created object to BP

IF lr_entity IS BOUND.
        typed_context->builsalesarrangement->collection_wrapper->add( iv_entity    = lr_entity
                                                                      iv_set_focus = abap_true ).

Now we have to fill the newly created object and create child objects containing the copied values from the original object. In order to do this two loops can be used: First loop through all possible sub objects and loop in a inner loop throught the entities in the entity collection

LOOP AT lt_children INTO ls_child.
          lr_rel_ent ?= ls_child-objects->if_bol_entity_col~get_first( ).
          WHILE lr_rel_ent IS BOUND.

*          get relation name
            lr_rel_ent->get_parent_relation(
                  IMPORTING
                    ev_relation_name = lv_relname
                    ev_relation_is_11 = lv_is_11
                    ).

*          get the name of the structure of the BOL object and create a new
*          field symbol with this type
            lv_att_struc_name = lr_rel_ent->get_attr_struct_name( ).
            ref_rowtype ?= cl_abap_typedescr=>describe_by_name( lv_att_struc_name ).
            CREATE DATA ref_wa TYPE HANDLE ref_rowtype.
            ASSIGN ref_wa->* TO <attr>.

*          get properties of subobject into field symbol
            lr_rel_ent->if_bol_bo_property_access~get_properties(
                  IMPORTING es_attributes = <attr> ).

*          create new subobject for copied sales organization with the
*          original values
            lr_child_ent = lr_entity->create_related_entity(
                  iv_relation_name = lv_relname ).
            lr_child_ent->set_properties( <attr> ).

            lr_rel_ent = ls_child-objects->if_bol_entity_col~get_next( ).
          ENDWHILE.

          lv_index = typed_context->builsalesarrangement->collection_wrapper->size( ).

        ENDLOOP.

      ENDIF.

6 Comments

  1. Guest

    Hi,
    I found your wiki for copying BOL node trees in the sdn and wanted to do something similar.

    Unfortunately I don't have the type ltype_child_object, available.Is that program supposed to run under CRM 2007 or is it already CRM 7.0?

    Thank's for any hints ...

    Regards Gerhard

  2. Guest

    Hi Gerhard,

    I have used the code in CRM 2007. ltype_child_object is a local type of the following components and types: PARENT (CRMT_EXT_OBJ_NAME), RELATION (crmt_relation_name), OBJECTS (TYPE REF TO if_bol_entity_col).

    /Florian

  3. Guest

    Hi,

    thanks for your help! 

    Adding the type definition solved the problem.

    Best regards Gerhard

  4. Former Member

    really thank you..

  5. Former Member

    Hi,

    thanks nice articel on BOL programming.

  6. Guest

    Hi Florian,

    Wiki is really interesting and helping me a lot.

    How do you get an instance of lr_obj_to_copy in your example?

    Thanks

    Deepak