Product change on item of CRM document

Problem: you want to change product on one of the document items in SAP CRM. You want to do it from a custom report.

Solution: when you first think about it, it doesn’t look like a hard task. Especially if you have used CRM_ORDER_MAINTAIN FM before. Unfortunately lack of any error messages returned from CRM_ORDER_MAINTAIN makes it somewhat frustrating task.

First of all you need to prepare tables for the change of the product, on the document item. Apparently, change is done by deletion of old item and creation of new one, so instead of just sending new product information you also need to provide basic information about the item itself. The easiest way to do it, is to simply copy the data from the old item.

Second part is the Schedule of the item. Apparently you also need to change this part – although you do not need to change anything on the Schedule lines (you just need to send it to the system). So here, also, read old Schedule lines and copy them to the „new” ones.

Having done both of these things system will delete old and create new item with the new product given by user. All that is left now is to save the document.

  CONSTANTS: lc_object_kind_item TYPE crmt_object_kind VALUE 'B',
             lc_update_mode_update TYPE crmt_mode VALUE 'B'.
 
  DATA: lt_items TYPE crmt_orderadm_i_comt,
        ls_item TYPE crmt_orderadm_i_com,
        ls_old_item TYPE crmt_orderadm_i_wrk,
        lv_old_item_guid TYPE crmt_object_guid,
        lv_new_product_id TYPE comt_product_id,
        lt_old_items type crmt_orderadm_i_wrkt,
        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.
 
  READ TABLE lt_old_items INTO ls_old_item WITH TABLE KEY guid = lv_old_item_guid.
  CHECK sy-subrc = 0 AND ls_old_item-ordered_prod <> lv_new_product_id.
 
  MOVE-CORRESPONDING ls_old_item TO ls_item.
  ls_item-ordered_prod = lv_new_product_id.
  ls_item-mode = lc_update_mode_update.
  INSERT ls_item INTO TABLE lt_items.
 
  CLEAR ls_input_fields.
  ls_field_name-fieldname = 'MODE'.
  INSERT ls_field_name INTO TABLE ls_input_fields-field_names.
  ls_field_name-fieldname = 'NUMBER_INT'.
  INSERT ls_field_name INTO TABLE ls_input_fields-field_names.
  ls_field_name-fieldname = 'ORDERED_PROD'.
  INSERT ls_field_name INTO TABLE ls_input_fields-field_names.
  ls_field_name-fieldname = 'ITM_TYPE'.
  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.
 
  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.
 
  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 sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  CONSTANTS: lc_object_kind_item TYPE crmt_object_kind VALUE 'B',
             lc_update_mode_update TYPE crmt_mode VALUE 'B'.

  DATA: lt_items TYPE crmt_orderadm_i_comt,
        ls_item TYPE crmt_orderadm_i_com,
        ls_old_item TYPE crmt_orderadm_i_wrk,
        lv_old_item_guid TYPE crmt_object_guid,
        lv_new_product_id TYPE comt_product_id,
        lt_old_items type crmt_orderadm_i_wrkt,
        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.

  READ TABLE lt_old_items INTO ls_old_item WITH TABLE KEY guid = lv_old_item_guid.
  CHECK sy-subrc = 0 AND ls_old_item-ordered_prod <> lv_new_product_id.

  MOVE-CORRESPONDING ls_old_item TO ls_item.
  ls_item-ordered_prod = lv_new_product_id.
  ls_item-mode = lc_update_mode_update.
  INSERT ls_item INTO TABLE lt_items.

  CLEAR ls_input_fields.
  ls_field_name-fieldname = 'MODE'.
  INSERT ls_field_name INTO TABLE ls_input_fields-field_names.
  ls_field_name-fieldname = 'NUMBER_INT'.
  INSERT ls_field_name INTO TABLE ls_input_fields-field_names.
  ls_field_name-fieldname = 'ORDERED_PROD'.
  INSERT ls_field_name INTO TABLE ls_input_fields-field_names.
  ls_field_name-fieldname = 'ITM_TYPE'.
  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.

  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.

  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 sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.



Globally Unique Identifier

Komentowanie jest zakończone.