Monday, 9 July 2012

BOL Programming - Dquery Objects

*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'
    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( ).

*Use Iterator to acces 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( ).

*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( ).


*..........Begin of Messages
*Acces Messages of Business Objects
*Use the Message Container Manager
DATA:lv_mcm TYPE REF TO cl_crm_genil_mess_cont_manager.
lv_mcm = lr_core1->get_message_cont_manager( ).

*To Obtain the Message Container for Relavent Object
DATA:lv_object_name TYPE crmt_ext_obj_name,
     lv_object_id TYPE crmt_genil_object_id.

lv_object_name = lv_entity->get_name( ).
lv_object_id   = lv_entity->get_key( ).

DATA:lv_mc TYPE REF TO if_genil_message_container.
lv_mc = lv_mcm->get_message_cont( iv_object_name = lv_object_name
                                  iv_object_id = lv_object_id ).
*Get Relavent messages..
DATA:lv_number_of_errors TYPE int4,
     lt_messages TYPE crmt_genil_message_tab.

*Get Number of Messages Count
lv_number_of_errors = lv_mc->get_number_of_messages( lv_mc->mt_error ).
IF lv_number_of_errors <> 0.
*Get List of Error messages
  lv_mc->get_messages( EXPORTING iv_message_type = lv_mc->mt_error
                       IMPORTING et_messages = lt_messages ).
ENDIF.
*..........End of Messages


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

*Access Attributes of the Business Objects 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' ).

*Get 1:1 Related Entities of BP of BuilSalesEmployeeRel Child Cardinality 1
  DATA:lv_employee_responsible TYPE REF TO cl_crm_bol_entity. "Only one record
  lv_employee_responsible = lv_entity->get_related_entity( 'BuilSalesEmployeeRel' ).

  DATA:lv_empbp TYPE string.
  lv_empbp = lv_employee_responsible->get_property_as_string( 'SALESEMPLOYEE' ).

*Get 0:n Related Entities of BP of BuilContactPersonAddressRel Child Cardinality 0..n
  DATA:lv_address TYPE REF TO if_bol_entity_col.
  lv_address = lv_entity->get_related_entities( iv_relation_name = 'BuilContactPersonAddressRel' ).

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

*Get First Entity in the result list.
*Entity is address of BP here.
  DATA:lv_entity_add TYPE REF TO cl_crm_bol_entity.
  lv_entity_add  = lv_iterator_add->get_first( ).
*Loop through all address of BP....
  WHILE lv_entity_add IS BOUND. "Means lv_entity is not initial..
    DATA:lv_stdardaddress  TYPE string.
    lv_stdardaddress = lv_entity_add->get_property_as_string( 'STANDARDADDRESS' ).

    WRITE:/ lv_bpnumber,        "Business Partner
          / lv_contactbp,       "Contact Person
          / lv_empbp,           "Sales Employee Responsible
          / lv_stdardaddress.   "Standard Address

    lv_entity_add = lv_iterator_add->get_next( ).
  ENDWHILE.
  lv_entity = lv_iterator->get_next( ).
ENDWHILE.

No comments:

Post a Comment