Thursday 13 September 2012

BOL Find


*Get the BOL Core instance
DATA: lr_core1 TYPE REF TO cl_crm_bol_core.
*Load the component set Or Component
lr_core1 = cl_crm_bol_core=>get_instance( ).
lr_core1->start_up( 'BP_APPL' ).

*Determine Dynamic Query Services Available
DATA:lv_obj_model  TYPE REF TO if_genil_obj_model.
lv_obj_model = cl_crm_genil_model_service=>get_runtime_model( ).

DATA:lt_query_names TYPE crmt_ext_obj_name_tab.
CALL METHOD lv_obj_model->get_object_list
  EXPORTING
    iv_object_kind     = if_genil_obj_model=>dquery_object
*    iv_ws_enabled_only = ABAP_FALSE
  IMPORTING
    et_object_list     = lt_query_names.


**Select Particular Query
DATA:lv_query_name TYPE crmt_ext_obj_name.
READ TABLE lt_query_names INTO lv_query_name INDEX 6. "'BuilContactAdvancedSearch '

*Create Query Service by passing the dynamic search object name
DATA:lv_query TYPE REF TO cl_crm_bol_dquery_service.
lv_query = cl_crm_bol_dquery_service=>get_instance( lv_query_name ).


*Set General Query parameters for maximum number of hits
DATA lt_parms TYPE crmt_name_value_pair_tab.
DATA ls_parms TYPE crmt_name_value_pair.

ls_parms-name = 'MAX_HITS'.
ls_parms-value = '5'.
APPEND ls_parms TO lt_parms.
lv_query->set_query_parameters( it_parameters = lt_parms ).

*Add Selection Criteria where BP number > 30
CALL METHOD lv_query->add_selection_param
  EXPORTING
    iv_attr_name = 'BP_NUMBER'
    iv_sign      = 'I'
    iv_option    = 'GT'
    iv_low       = '0000311966'                             "0000311966
    iv_high      = ' '.


*Execute Query and Receive Result
DATA:lv_result_tab TYPE REF TO if_bol_entity_col.  "To Capture Multiple Records
lv_result_tab = lv_query->get_query_result( ).

lv_result_tab->sort( iv_attr_name = 'BP_NUMBER'
                     iv_sort_order = if_bol_bo_col=>sort_descending ).

*Use Iterator to access entities in query result
DATA:lv_iterator TYPE REF TO if_bol_entity_col_iterator. "or IF_BOL_ENTITY_COL
lv_iterator = lv_result_tab->get_iterator( ).


DATA:lv_bp TYPE REF TO if_bol_bo_property_access. "CL_CRM_BOL_ENTITY
lv_bp = lv_iterator->find_by_property( iv_attr_name = 'BP_NUMBER'
                                       iv_value = '0000406648' ).


DATA:lv_bp_find TYPE REF TO cl_crm_bol_entity.
lv_bp_find ?= lv_bp.
lv_bp_find = lv_result_tab->find( iv_entity = lv_bp_find ).

*Get First Entity in the result list.
*Entity is contact person here.
DATA:lv_entity TYPE REF TO cl_crm_bol_entity.
lv_entity  = lv_iterator->get_first( ).

*Loop through Further Entities....
WHILE lv_entity IS BOUND. "If lv_entity is not initial..

*Access Attributes of the Business Object (Entity) Selected
  DATA:lv_bpnumber TYPE string,
          lv_contactbp  TYPE string.
 
  lv_bpnumber = lv_entity->get_property_as_string( 'BP_NUMBER' ).
  lv_contactbp  = lv_entity->get_property_as_string( 'CONP_NUMBER' ).

  WRITE: / lv_bpnumber.

  lv_entity = lv_iterator->get_next( ).

ENDWHILE.

BOL Delete


DATA: lr_core TYPE REF TO cl_crm_bol_core.
*Get the BOL Core instance
lr_core = cl_crm_bol_core=>get_instance( ).
*Load the component set
lr_core->load_component_set( 'BP_APPL' ).
*------------------------------------------------------------
*Raise the query instance by passing the search object name
DATA:lr_query TYPE REF TO cl_crm_bol_query_service.
lr_query = cl_crm_bol_query_service=>get_instance(
                  iv_query_name = 'BuilContactPersonSearch' ).

DATA it_parms TYPE crmt_name_value_pair_tab.
DATA wa_parms TYPE crmt_name_value_pair.


wa_parms-name = 'CONP_NUMBER'.
wa_parms-value = '400788'.
APPEND wa_parms TO it_parms.

*Add the selection parameters
CALL METHOD lr_query->set_query_parameters
  EXPORTING
    it_parameters = it_parms.

*Get the result list
DATA:lr_result TYPE REF TO if_bol_entity_col.
lr_result = lr_query->get_query_result( ).

*Get the first entity in the result list
DATA:lr_entity TYPE REF TO cl_crm_bol_entity.
lr_entity ?= lr_result->get_first( ).

DATA lv_string TYPE string.
*-------------------------------------------------------
*Lock and modify the property
IF  lr_entity->lock( ) = if_genil_boolean=>true.
  lr_entity->delete( ).
*RV_STATUS = LR_CORE->ROOT_DELETE( iv_entity = LR_ENTITY ).
  lr_core->modify( ).

  DATA lv_transaction TYPE REF TO if_bol_transaction_context.
  lv_transaction = lr_core->get_transaction( ).
  lv_transaction->save( ).
  lv_transaction->commit( ).
  lr_core->modify( ).
ENDIF.

Tuesday 21 August 2012

BOL Change

DATA: lr_core TYPE REF TO cl_crm_bol_core.
lr_core = cl_crm_bol_core=>get_instance( ).
lr_core->start_up( 'BT' ).
*Get the BOL Core instance
lr_core = cl_crm_bol_core=>get_instance( ).

DATA:lv_query TYPE REF TO cl_crm_bol_query_service.
lv_query = cl_crm_bol_query_service=>get_instance( 'BTQuery1O' ).
** Set a search criterion
lv_query->set_property( iv_attr_name = 'OBJECT_ID' iv_value = '8000002731' ).

*Get Search Criteria
DATA:lv_objid TYPE REF TO data.
lv_objid = lv_query->get_property( iv_attr_name = 'OBJECT_ID' ).

*Get Corresponding Text for a given value.
*Text will be fetched from Domain If available or else,
*fetch from GENIL Component
DATA:lv_sex TYPE string.
lv_sex = lv_query->get_property_text( 'SEX' ).

* Execute the query and receive result
DATA:lv_result TYPE REF TO if_bol_entity_col.
lv_result = lv_query->get_query_result( ).
IF lv_result IS NOT INITIAL.
  DATA:lr_orderheader TYPE REF TO cl_crm_bol_entity,
       lr_order TYPE REF TO cl_crm_bol_entity.
  lr_order ?= lv_result->get_current( ).
  TRY.
      CALL METHOD lr_order->get_related_entity
        EXPORTING
          iv_relation_name = 'BTOrderHeader'
        RECEIVING
          rv_result        = lr_orderheader.
    CATCH cx_crm_genil_model_error .
  ENDTRY.

  lr_orderheader->switch_to_change_mode( ).
  IF lr_orderheader->lock( ) = 'X'.
    IF lr_orderheader->is_changeable( ) = 'X'.

*      lr_orderheader->set_property( iv_attr_name = 'DESCRIPTION'
*                                                     iv_value     = 'Description Changed' ). or

      lr_orderheader->if_bol_bo_property_access~set_property( iv_attr_name = 'DESCRIPTION'
                                                                                                iv_value     = 'Description Changed' ).

*Send All Changes to BO Layer
      lr_core->modify( ).

*6.  Save and Commit the Changes Using Global Tranaction Context
      DATA:lv_transaction TYPE REF TO if_bol_transaction_context .
      lv_transaction = lr_core->get_transaction( ).
      lv_transaction->save( ).
      lv_transaction->commit( ).
    ENDIF.
  ENDIF.
ENDIF.

BOL Create

DATA: lv_bol_core TYPE REF TO cl_crm_bol_core.
*Get the BOL Core instance
lv_bol_core = cl_crm_bol_core=>get_instance( ).
*Load the component set
lv_bol_core->load_component_
set( 'BT' ).

*1.  Build Parameters to create an Entity
*    Here a Order with Technical Name 'BTOrder'
DATA lt_parms TYPE crmt_name_value_pair_tab.
DATA ls_parms TYPE crmt_name_value_pair.

ls_parms-name = 'PROCESS_TYPE'.
ls_parms-value = 'BPAT'.
APPEND ls_parms TO lt_parms.

*2.  Get Factory for Business Object
DATA:lv_order_factory TYPE REF TO cl_crm_bol_entity_factory.
lv_order_factory = lv_bol_core->get_entity_
factory( 'BTOrder' ).

*3.  Create Root Entity
DATA:lv_order TYPE REF TO cl_crm_bol_entity.
lv_order = lv_order_factory->create( lt_parms ).

*4.  Create Child Objects
DATA:lv_order_header TYPE REF TO cl_crm_bol_entity,
     lv_actity_header TYPE REF TO cl_crm_bol_entity.

lv_order_header = lv_order->create_related_
entity( 'BTOrderHeader' ).
lv_actity_header = lv_order->create_related_
entity( 'BTHeaderActivityExt' ).

*5.  Submit Root Objects and Child Objects Which are created
lv_bol_core->modify( ).

*6.  Save and Commit Changes Using Global Tranaction Context
DATA:lv_transaction TYPE REF TO if_bol_transaction_context .
lv_transaction = lv_bol_core->get_transaction( ).
lv_transaction->save( ).
lv_transaction->commit( ).