Creating, changing and reading business transaction data using class cl_ags_crm_1o_api

To create, change or read business transaction data in SAP CRM or SAP Solution Manager, you can use low level API function modules CRM_ORDER_READ, CRM_ORDER_MAINTAIN, CRM_*_OW or high level layer BOL/GenIL.

However, sometimes you want to use a simple API which is optimized for single transaction processing. In that case, you should use classes cl_ags_crm_1o_api (and cl_crm_bsp_maintain_1o).

*"----------------------------------------------------------------------
*"Methoden Signatur:
*"  Importing
*"    IV_HEADER_GUID TYPE CRMT_OBJECT_GUID
*"    IV_PROCESS_TYPE TYPE CRMT_PROCESS_TYPE
*"  Exporting
*"    EV_HEADER_GUID TYPE CRMT_OBJECT_GUID
*"----------------------------------------------------------------------
METHOD create_transaction.

*We have to use class cl_crm_bsp_maintain_1o instead of cl_ags_crm_1o_api
*because class cl_ags_crm_1o_api don't support creation of follow-ups yet.
*After creation we need to save or we need to handover buffer from cl_crm_bsp_maintain_1o to cl_ags_crm_1o_api.
*Both ways are not possible. Therefore we free our instance of cl_crm_bsp_maintain_1o and hope the best.
"However this a dirty solution because the instance is still in buffer and marked as "to be saved".
*Therefore we might have side effects when someone uses class cl_crm_bsp_maintain_1o afterwards in same session.
*At the moment that isn't the fact.

  DATA:
    lr_maintain_1o TYPE REF TO cl_crm_bsp_maintain_1o.

  CLEAR ev_header_guid.


*Get Instance of 1O Manager
  CALL METHOD cl_crm_bsp_maintain_1o=>get_instance
    RECEIVING
      rr_maintain_1o = lr_maintain_1o.

*Create Follow Up.
  CALL METHOD lr_maintain_1o->copy_order
    EXPORTING
      iv_process_type    = iv_process_type
      iv_old_header_guid = iv_header_guid
      iv_vona_kind       = 'A' "Use Copy Control
*     iv_template_type   =
    IMPORTING
      ev_new_header_guid = ev_header_guid
    EXCEPTIONS
      error_occurred     = 1
      OTHERS             = 2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'X' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  FREE lr_maintain_1o.

ENDMETHOD.

*"----------------------------------------------------------------------
*"Methoden Signatur:
*"  Importing
*"    IV_HEADER_GUID TYPE CRMT_OBJECT_GUID
*"    IV_DESCRIPTION TYPE CRMT_PROCESS_DESCRIPTION
*"    IV_TEXTTYPE TYPE TDID
*"    IV_TEXTID TYPE DOK_ID
*"    IV_TEXTOBJ TYPE DOKU_OBJ
*"    IS_TRACK TYPE /TMWFLOW/TRACK
*"    IS_CONTEXT TYPE ZCIEH_CONTEXT_S
*"----------------------------------------------------------------------
METHOD adjust_short_text.

  DATA:
    lr_instance   TYPE REF TO cl_ags_crm_1o_api,
*   ls_orderadm_h TYPE crmt_orderadm_h_wrk,
    lv_on_database_flag TYPE abap_bool,
    lv_short_text LIKE iv_description.

*Get Instance of Change Transaction
  CALL METHOD cl_ags_crm_1o_api=>get_instance
    EXPORTING
*     iv_language                   = SY-LANGU
      iv_header_guid                = iv_header_guid
*     iv_process_type               =
      iv_process_mode               = 'C' "We want to Change the Change Transaction. But Exception can not tell that document is just locked.
    IMPORTING
*     et_msg                        =
      eo_instance                   = lr_instance
    EXCEPTIONS
      invalid_parameter_combination = 1
      error_occurred                = 2
      OTHERS                        = 3.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'X' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

*Check if Transaction already exists on Database.
*Getting MODE of ORDERADM_H don't work correct.
  CALL FUNCTION 'CRM_ORDERADM_H_ON_DATABASE_OW'
    EXPORTING
      iv_orderadm_h_guid  = iv_header_guid
    IMPORTING
      ev_on_database_flag = lv_on_database_flag.

*We want to change the description.
  IF iv_description IS NOT INITIAL.

*Get Short Text
    CALL METHOD lr_instance->get_short_text
      IMPORTING
        ev_short_text        = lv_short_text
      EXCEPTIONS
        document_not_found   = 1
        error_occurred       = 2
        document_locked      = 3
        no_change_authority  = 4
        no_display_authority = 5
        no_change_allowed    = 6
        OTHERS               = 7.

*Expected & unexpected error...
    CASE sy-subrc.
      WHEN 0.
      WHEN 3. RAISE document_locked.
      WHEN 4. RAISE no_authority.
      WHEN 5. RAISE no_authority.
      WHEN 6. RAISE no_change_allowed.
      WHEN OTHERS. MESSAGE ID sy-msgid TYPE 'X' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDCASE.

*Set Short Text if it is empty yet or if Transaction will be created.
    IF lv_short_text IS INITIAL OR lv_on_database_flag = abap_false. "ls_orderadm_h-mode = 'A'.

      CALL METHOD lr_instance->set_short_text
        EXPORTING
          iv_short_text     = iv_description
        EXCEPTIONS
          error_occurred    = 1
          document_locked   = 2
          no_change_allowed = 3
          no_authority      = 4
          OTHERS            = 5.

*Expected & unexpected error...
      CASE sy-subrc.
        WHEN 0.
        WHEN 2. RAISE document_locked.
        WHEN 3. RAISE no_change_allowed.
        WHEN 4. RAISE no_authority.
        WHEN OTHERS. MESSAGE ID sy-msgid TYPE 'X' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDCASE.

    ENDIF.

  ENDIF.

*This is a little bit strange but necessary.
*Status changes and long text changes bypass the One Order Framework.
*Therefore when we save the Change Transaction via CRM_ORDER_SAVE it might detect nothing to do.
*But this is wrong, we still want a commit work.

*Therefore we simulate a change on ORDERADM_H.
  CALL FUNCTION 'CRM_EVENT_PUBLISH_OW'
    EXPORTING
      iv_obj_name = 'ORDERADM_H'
      iv_guid_hi  = iv_header_guid
      iv_kind_hi  = 'A'
      iv_event    = 'SAVE'.

*Free Instance.
  FREE: lr_instance.

ENDMETHOD.

*"----------------------------------------------------------------------
*"Methoden Signatur:
*"  Importing
*"    IV_HEADER_GUID TYPE CRMT_OBJECT_GUID
*"----------------------------------------------------------------------
METHOD save_transaction.

  DATA:
    lr_instance      TYPE REF TO cl_ags_crm_1o_api,
    lt_exception     TYPE crmt_exception_t,
    lt_msg           TYPE /tmwflow/mo_tt_msg,
    lt_saved_objects TYPE crmt_return_objects,
    lv_log_handle TYPE balloghndl.

*Get Instance of Change Transaction
  CALL METHOD cl_ags_crm_1o_api=>get_instance
    EXPORTING
*     iv_language                   = SY-LANGU
      iv_header_guid                = iv_header_guid
*     iv_process_type               =
      iv_process_mode               = 'B'
    IMPORTING
*     et_msg                        =
      eo_instance                   = lr_instance
    EXCEPTIONS
      invalid_parameter_combination = 1
      error_occurred                = 2
      OTHERS                        = 3.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'X' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

*Save Transaction changes to database.
  CALL METHOD lr_instance->save
    EXPORTING
      iv_unlock                  = abap_true
*     iv_save_only_external_data = SPACE
      iv_init                    = abap_true
    IMPORTING
      et_exception               = lt_exception[]
      et_msg                     = lt_msg[]
      et_saved_objects           = lt_saved_objects[]
    CHANGING
      cv_log_handle              = lv_log_handle
    EXCEPTIONS
      error_occurred             = 1
      OTHERS                     = 2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'X' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSEIF lt_saved_objects[] IS INITIAL.
    MESSAGE x151(00).
  ENDIF.

  FREE: lr_instance.

ENDMETHOD.