二维码

[教程] BASICS OF BOL PROGRAMMING

Twilight发表于 2015-12-05 00:58Twilight 最后回复于 2015-12-05 00:58 [复制链接] 4861 0

***************************************************************************************************************
*BOL PROGRAMMING
*SAP CRM WEBCLIENT
*GIVES INTRODUCTION TO * basic activities like search, create, update in the bol
*programming in order to give introduction to the main classes and methods of business object layer.
***************************************************************************************************************
  1. *used data
  2. DATA: lr_core TYPE REF TO cl_crm_bol_core.

  3. *in order to use any service of BOL, we need to start the MODEL (component set) using
  4. * CORE class.
  5. lr_core = cl_crm_bol_core=>get_instance( ).

  6. *once we have the instance, then load the component set.
  7. *here we will be using ONEORDER component set.

  8. *when we are working on web ui, this will be handled by framework. here we are doing it in the report programming so we need to handle it
  9. TRY.
  10.     CALL METHOD lr_core->load_component_set
  11.       EXPORTING
  12.         iv_component_set_name = 'ONEORDER'.
  13.   CATCH cx_crm_genil_general_error .
  14. ENDTRY.

  15. *component is loaded, so we can use the BOL services. Let us search for a particular
  16. *contract. for that we need to get an instance of dquery service class.
  17. DATA: lr_query TYPE REF TO cl_crm_bol_dquery_service,
  18.       lr_result TYPE REF TO cl_crm_bol_bo_col.

  19. *-->SEARCH OPERATION
  20. "get the instance of dynamic search object,which will be used to search service contracts.
  21. lr_query ?= cl_crm_bol_dquery_service=>get_instance( iv_query_name = 'BTQSrvCon' ).

  22. "Every dynamic search object will have its own result type object. here BTQSrvcon has BTQRSrvcon as result object.
  23. "so in the result list all objects are type of BTQRSrvcon.

  24. "we got the instance, so we need to set the selection criteria.

  25. "fetching all contracts whose description is 'testing'.
  26. "you can call this method multiple times to add different selection parameters.
  27. lr_query->add_selection_param( iv_attr_name = 'DESCRIPTION'
  28.                                                 iv_sign      = 'I'
  29.                                                 iv_option    = 'EQ'
  30.                                                 iv_low       = 'testing' ).

  31. "get the list contracts that match the selection criteria in the form of collection.
  32. "collection is nothing but a container that holds records. we can compare it like a internal table
  33. "that holds records.
  34. lr_result ?= lr_query->get_query_result( ).

  35. "Let us print the contract id and description of all records in teh collection.

  36. "cl_crm_bol_entity is used to represent the record in the BUSINESS OBJECT LAYER.
  37. DATA: lr_entity TYPE REF TO cl_crm_bol_entity,
  38.       lv_objectid TYPE string,
  39.       lv_descr TYPE char40.

  40. "read the first record in the collection.
  41. lr_entity ?= lr_result->if_bol_bo_col~get_first( ).

  42. "following while loop mechanism is used to visit each record in the collection.
  43. WHILE lr_entity IS BOUND.
  44.   "read the contract id.
  45.   "get property as string will return attribute values in the form of string. so we declared lv_objectid as string.
  46.   lr_entity->get_property_as_string( EXPORTING iv_attr_name = 'OBJECT_ID'
  47. RECEIVING rv_result = lv_objectid  ).

  48.   "get property as value will return the value as it as.so we declared DESCRIPTION with data element.
  49.   lr_entity->get_property_as_value( EXPORTING iv_attr_name = 'DESCRIPTION' IMPORTING ev_result = lv_descr ).
  50.   WRITE : lv_objectid, lv_descr.
  51.   WRITE /:.

  52.   "read the next record in the collection.
  53.   lr_entity ?= lr_result->if_bol_bo_col~get_next( ).
  54. ENDWHILE.
复制代码

**************UPDATING OPERATION***************************
  1. "Let us change the description of one service contract.

  2. "get the curren entity from the result list.

  3. "Every collection holds one pointer which always points to the current record we visited.We generally call it is a focus
  4. "in the BOL language. so get current returns the object which has focus. so what ever the last object we visited in the
  5. "above loop, will be fetched in the below statement.
  6. lr_entity ?= lr_result->if_bol_bo_col~get_current( ).

  7. IF lr_entity IS BOUND.  "we never work on object references which are initial. this is check is very important in BOL.
  8.   "can we use the set property as string directly on this lr_entity?.

  9.   "No, it is dynamic query result object, generally all properties of these type objects are read only.

  10.   "DESCRIPTION is property of BTAdminH object. we need to get it using the relations. We need to take help from MODEl to see
  11.   "what are the relations that we need to go through to reach the target object. Transaction GENIL_MODEL_BROWSER

  12.   "in our scenario, we have BTQRSrvcon as a soruce in the lr_entity variable. This object has a relation 'Association BTADVSSrvCon Child Cardinality 1'
  13.   "which will give the object BTOrder.

  14.   "again BTOrder is having a relation 'Composition BTOrderHeader Child Cardinality 1', which will fetch the target object BTAdminH. this object has
  15.   "an attribute 'DESCRIPTION'.

  16.   "while looking at the relations, look at the cardinality of relation. if cardinality is 0...1, then relation always fetch one single entity. if is
  17.   "0...N, then relation will fetch multiple entities.

  18.   " use the GET RELATED ENTITY when cardinality is 0...1.
  19.   " use the GET_RELATED_ENTITIES when cardinality is 0...N.
  20.   DATA: lr_order TYPE REF TO cl_crm_bol_entity.

  21.   "read the BTORDER first.
  22.   " the parameter iv_mode will take another value 'b', which stands for bypassing the buffer.
  23.   "which means, framework will ignore the values that exist in the bol buffer and read the target
  24.   "entity values from the database by triggering required APIs. it can decrease the performance. so use only in necessary
  25.   "situations like when you are reading that particular entity first time..
  26.   TRY.
  27.       CALL METHOD lr_entity->get_related_entity
  28.         EXPORTING
  29.           iv_relation_name = 'BTADVSSrvCon'
  30.           iv_mode          = cl_crm_bol_entity=>bypassing_buffer
  31.         RECEIVING
  32.           rv_result        = lr_order.
  33.     CATCH cx_crm_genil_model_error .
  34.   ENDTRY.

  35.   "we got the order, then read the BTAdminH.
  36.   DATA:  lr_header TYPE REF TO cl_crm_bol_entity.
  37.   IF lr_order IS BOUND.
  38.     TRY.
  39.         CALL METHOD lr_order->get_related_entity
  40.           EXPORTING
  41.             iv_relation_name = 'BTOrderHeader'
  42.             iv_mode          = cl_crm_bol_entity=>bypassing_buffer
  43.           RECEIVING
  44.             rv_result        = lr_header.
  45.       CATCH cx_crm_genil_model_error .
  46.     ENDTRY.
  47.   ENDIF.

  48.   "we got the BTadminH. change its DESCRIPTION property.
  49.   IF lr_header IS BOUND.
  50.     "before changing any property of BOL object, we need to lock it.
  51.     "this will be accomplished by lock method.
  52.     IF  lr_header->lock( ) = abap_true.  "if we get the lock, then only continue
  53.       "we use set_* methods to change the properties of BOL objects.
  54.       lr_header->set_property_as_string( EXPORTING iv_attr_name = 'DESCRIPTION'
  55.       iv_value = 'changed'  ).
  56.     ENDIF.

  57.     "once change is done, we need to tell about this change to the framework.
  58.     "we will use core class for this.
  59.     lr_core->modify( ).

  60.     "then get the transaction from the entity and check whether this save is possible or not.
  61.     DATA: lr_transaction TYPE REF TO if_bol_transaction_context.
  62.     lr_transaction ?= lr_header->get_transaction( ).
  63.     IF lr_transaction IS BOUND.
  64.       IF lr_transaction->check_save_possible( ) EQ abap_true.
  65.         "if we can save , then save the transaction and commit the changes.
  66.         IF lr_transaction->save( ) EQ abap_true.
  67.           lr_transaction->commit( ).
  68.         ENDIF.
  69.         "we changed the object description. read it again to get the new value.
  70.         lr_header->get_property_as_value( EXPORTING iv_attr_name = 'DESCRIPTION'
  71.                  IMPORTING ev_result = lv_descr ).
  72.       ENDIF.
  73.     ENDIF.
  74.   ENDIF.
  75. ENDIF.
复制代码

***************CREATE operation*****************************
  1. *Let us create one service contract and fill some of its properties.

  2. *"first step , we need to create the root entity.
  3. DATA:  lr_factory       TYPE REF TO cl_crm_bol_entity_factory,
  4.        lr_order_new     TYPE REF TO cl_crm_bol_entity,
  5.        ls_params        TYPE crmt_name_value_pair,
  6.        lt_params        TYPE crmt_name_value_pair_tab.

  7. ls_params-name  = 'PROCESS_TYPE'.
  8. ls_params-value = 'ZSZ'.
  9. APPEND ls_params TO lt_params.

  10. "create root entity will be handled by the factory class. we use CORE class
  11. "to get the instance of required factory class by sending the root object to its
  12. "method get entity factory. then we use CREATE method of factory class by sending
  13. "the necessary parameters.

  14. "here we are creating contracts, so we are sending the process type as input.
  15. lr_factory   = lr_core->get_entity_factory( 'BTOrder' ).    "#EC NOTEXT
  16. lr_order_new = lr_factory->create( lt_params ).
  17. IF lr_order_new IS BOUND.
  18.   "BTOrder is having only GUID, we need to get BTAdminH entity to fill up some of its properties.
  19.   TRY.
  20.       CALL METHOD lr_order_new->get_related_entity
  21.         EXPORTING
  22.           iv_relation_name = 'BTOrderHeader'
  23.           iv_mode          = cl_crm_bol_entity=>bypassing_buffer
  24.         RECEIVING
  25.           rv_result        = lr_header.
  26.     CATCH cx_crm_genil_model_error .
  27.   ENDTRY.

  28.   "fill the properties using the setter method
  29.   IF lr_header IS BOUND.
  30.     "we use set_* methods to change the properties of BOL objects.
  31.     lr_header->set_property_as_string( EXPORTING iv_attr_name = 'DESCRIPTION'
  32.     iv_value = 'New Contract'  ).
  33.     lr_core->modify( ).
  34.     lr_transaction ?= lr_header->get_transaction( ).
  35.     IF lr_transaction IS BOUND.
  36.       IF lr_transaction->check_save_possible( ) EQ abap_true.
  37.         "if we can save , then save the transaction and commit the changes.
  38.         IF lr_transaction->save( ) EQ abap_true.
  39.           lr_transaction->commit( ).
  40.         ENDIF.

  41.         "we changed the object description. read it again to get the new value.
  42.         lr_header->get_property_as_value( EXPORTING iv_attr_name = 'DESCRIPTION'
  43.                   IMPORTING ev_result = lv_descr ).

  44.       ENDIF.
  45.     ENDIF.
  46.   ENDIF.
  47. ENDIF.
复制代码
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表