How To Create Counter Reading Using BOL Programming

Here is a sample code I used to create counter reading using BOL programming

We start by declaring the variables to be used

  DATA: lr_core             TYPE REF TO cl_crm_bol_core,
        lr_btorder          TYPE REF TO cl_crm_bol_entity,
        lr_btadmin_h        TYPE REF TO cl_crm_bol_entity,
        lr_counter_h        TYPE REF TO cl_crm_bol_entity,
        lr_reading_h        TYPE REF TO cl_crm_bol_entity,
        lr_transaction      TYPE REF TO if_bol_transaction_context.

  DATA: ls_reading          TYPE crmt_reading_mpil,
        ls_counter_h        TYPE crmst_counter_h_btil,
        lt_interval         TYPE STANDARD TABLE OF nriv,
        ls_interval         TYPE nriv,
        lv_reading_guid     TYPE crmt_object_guid,
        lv_reading_id       TYPE crmt_mpk_object_id_reading,
        lv_created_at       TYPE crmt_mpk_created_at,
        lv_changed_at       TYPE crmt_mpk_changed_at,
        lv_timestamp_from   TYPE crmt_mpk_timestamp_from,
        lv_timestamp_to     TYPE crmt_mpk_timestamp_to.

 

Next we start our transaction and create new reading entity using BOL programming

  lr_core = cl_crm_bol_core=>get_instance( ).

  lr_core->start_up( 'ONEORDER' ).

  lr_btorder ?= lr_core->get_root_entity( iv_object_name = 'BTOrder'
                                          iv_object_guid = iv_guid ).

  CHECK lr_btorder IS BOUND.

  lr_btadmin_h = lr_btorder->get_related_entity( iv_relation_name = 'BTOrderHeader' ).

  CHECK lr_btadmin_h IS BOUND.

*  lock entity if not already locked to be able to make changes
  IF lr_btadmin_h->is_locked( ) EQ abap_false.
    lr_btadmin_h->lock( ).
  ENDIF.

  lr_counter_h = lr_btadmin_h->get_related_entity( iv_relation_name = 'BTHeaderCounters' ).

  CHECK lr_counter_h IS BOUND.

  lr_counter_h->get_properties( IMPORTING es_attributes = ls_counter_h ).

  lr_reading_h = lr_counter_h->create_related_entity( iv_relation_name = 'BTHeaderReadings' ).

  CHECK lr_reading_h IS BOUND.

 

Then we set properties of the new reading

*  create guid for the new reading
  CALL FUNCTION 'CRM_GUID_CREATE'
    IMPORTING
      ev_guid = lv_reading_guid.

  lr_reading_h->set_property( iv_attr_name = 'GUID'
                              iv_value     = lv_reading_guid ).

*  create new id for the new reading using reading number object CRM_MPK_RD
  SELECT *
    INTO TABLE lt_interval
    FROM nriv
   WHERE object EQ 'CRM_MPK_RD'
     AND toyear EQ '0000'.

  CHECK sy-subrc EQ 0.

  READ TABLE lt_interval INTO ls_interval INDEX 1.

  CALL FUNCTION 'NUMBER_GET_NEXT'
    EXPORTING
      nr_range_nr             = ls_interval-nrrangenr
      object                  = 'CRM_MPK_RD'
    IMPORTING
      number                  = lv_reading_id
    EXCEPTIONS
      interval_not_found      = 1
      number_range_not_intern = 2
      object_not_found        = 3
      quantity_is_0           = 4
      quantity_is_not_1       = 5
      interval_overflow       = 6
      buffer_overflow         = 7
      OTHERS                  = 8.

  lr_reading_h->set_property( iv_attr_name = 'OBJECT_ID'
                              iv_value     = lv_reading_id ).

*  set necessarry appointment fields
  GET TIME STAMP FIELD lv_created_at.
  GET TIME STAMP FIELD lv_changed_at.
  GET TIME STAMP FIELD lv_timestamp_from.
  GET TIME STAMP FIELD lv_timestamp_to.

  lr_reading_h->set_property( iv_attr_name = 'CREATED_AT'
                              iv_value     = lv_created_at ).

  lr_reading_h->set_property( iv_attr_name = 'CHANGED_AT'
                              iv_value     = lv_changed_at ).

  lr_reading_h->set_property( iv_attr_name = 'TIMESTAMP_FROM'
                              iv_value     = lv_timestamp_from ).

  lr_reading_h->set_property( iv_attr_name = 'TIMESTAMP_TO'
                              iv_value     = lv_timestamp_to ).

 

Lastly, we send the changes we made to BO layer and save

*  save changes
  lr_core->modify( ).

  lr_transaction = lr_core->get_transaction( ).

  IF lr_transaction->check_save_possible( ) EQ abap_true.
    IF lr_transaction->save( ) EQ abap_true.
      lr_transaction->commit( ).
    ENDIF.
  ENDIF.

 

ABOUT Furkan Sönmez

www.linkedin.com/profile/view?id=122521475

RELATED POSTS

COMMENTS

comments powered by Disqus