How To Delete Items of CRM Order with CRM_ORDER_MAINTAIN

We start with defining the variables we are going to use for the CRM_ORDER_MAINTAIN function

  CONSTANTS: lc_mode_delete      TYPE crmt_mode          VALUE 'D',
             lc_object_kind_item TYPE crmt_object_kind   VALUE 'B'.

  DATA: lt_header_guid   TYPE crmt_object_guid_tab,
        lt_saved_objects TYPE crmt_return_objects,
        lt_exception     TYPE crmt_exception_t,
        lv_log_handle    TYPE balloghndl,
        lt_orderadm_i    TYPE crmt_orderadm_i_wrkt,
        ls_orderadm_i    TYPE crmt_orderadm_i_wrk,
        lt_items         TYPE crmt_orderadm_i_comt,
        ls_item          TYPE crmt_orderadm_i_com,
        lt_schedules     TYPE crmt_schedlin_wrkt,
        lt_schedlin_i    TYPE crmt_schedlin_i_comt,
        ls_schedlin_i    TYPE crmt_schedlin_i_com,
        ls_schedline     TYPE crmt_schedlin_extd,
        ls_schedule      TYPE crmt_schedlin_wrk,
        lt_input_fields  TYPE crmt_input_field_tab,
        ls_input_fields  TYPE crmt_input_field,
        ls_field_name    TYPE crmt_input_field_names.

Next we get items of the order with CRM_ORDER_READ

  INSERT iv_guid INTO lt_header_guid INDEX 1.

  CALL FUNCTION 'CRM_ORDER_READ'
    EXPORTING
      it_header_guid       = lt_header_guid
    IMPORTING
      et_orderadm_i        = lt_orderadm_i
      et_schedlin          = lt_schedules
    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.

Then we fill the necessarry tables for CRM_ORDER_MAINTAIN

  LOOP AT lt_orderadm_i INTO ls_orderadm_i.
    MOVE-CORRESPONDING ls_orderadm_i TO ls_item.
    ls_item-mode = lc_mode_delete.
    INSERT ls_item INTO TABLE lt_items.

    LOOP AT lt_schedules INTO ls_schedule WHERE item_guid = ls_item-guid.
      MOVE-CORRESPONDING ls_schedule TO ls_schedline.
      CLEAR ls_schedline-item_guid.
      ls_schedline-logical_key = ls_schedule-guid.
      INSERT ls_schedline INTO TABLE ls_schedlin_i-schedlines.

      ls_schedlin_i-ref_guid = ls_item-guid.
      INSERT ls_schedlin_i INTO TABLE lt_schedlin_i.

      CLEAR ls_input_fields.
      ls_field_name-fieldname = 'LOGICAL_KEY'.
      INSERT ls_field_name INTO TABLE ls_input_fields-field_names.
      ls_field_name-fieldname = 'QUANTITY'.
      INSERT ls_field_name INTO TABLE ls_input_fields-field_names.

      ls_input_fields-ref_guid = ls_item-guid.
      ls_input_fields-ref_kind = lc_object_kind_item.
      ls_input_fields-objectname = 'SCHEDLIN'.
      ls_input_fields-logical_key = ls_schedule-logical_key.
      INSERT ls_input_fields INTO TABLE lt_input_fields.
    ENDLOOP.
  ENDLOOP.

We provide the field names that we want to make changes on

  CLEAR ls_input_fields.
  ls_field_name-fieldname = 'MODE'.
  INSERT ls_field_name INTO TABLE ls_input_fields-field_names.

  ls_input_fields-ref_guid = ls_item-guid.
  ls_input_fields-objectname = 'ORDERADM_I'.
  INSERT ls_input_fields INTO TABLE lt_input_fields.

Now we are ready to call CRM_ORDER_MAINTAIN

  CALL FUNCTION 'CRM_ORDER_MAINTAIN'
    EXPORTING
      it_schedlin_i     = lt_schedlin_i
    CHANGING
      ct_orderadm_i     = lt_items
      ct_input_fields   = lt_input_fields
    EXCEPTIONS
      error_occurred    = 1
      document_locked   = 2
      no_change_allowed = 3
      no_authority      = 4
      OTHERS            = 5.

If maintain bapi call is successful, we save the order and commit our work

  CALL FUNCTION 'CRM_ORDER_SAVE'
    EXPORTING
      it_objects_to_save = lt_header_guid
    IMPORTING
      et_saved_objects   = lt_saved_objects
      et_exception       = lt_exception
    CHANGING
      cv_log_handle      = lv_log_handle
    EXCEPTIONS
      document_not_saved = 1
      OTHERS             = 2.

  IF lt_exception IS INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
  ENDIF.

 

ABOUT Furkan Sönmez

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

RELATED POSTS

COMMENTS

comments powered by Disqus