In Logic of CRMD_CUMULAT_H I learned the fact that gross weight of an order is calculated based on all its line item. Recently in my project I have the requirement to modify the gross weight of an order directly via ABAP code.

I check the importing parameter of FM and don’t find the corresponding parameter for CUMULAT_H.
After research I have written the below report:
REPORT zchange_cumulat_h.

PARAMETERS: quantity TYPE int4 OBLIGATORY DEFAULT 1,
             item TYPE crmd_orderadm_i-number_int OBLIGATORY DEFAULT 20,
             srvo_id TYPE crmd_orderadm_h-object_id OBLIGATORY DEFAULT '5700000242'.
CONSTANTS: cv_sales_item TYPE crmt_subobject_category_db VALUE 'BUS2000131'.
DATA: lv_srvo_guid       TYPE crmd_orderadm_h-guid,
      lt_to_save         TYPE crmt_object_guid_tab,
      lt_saved           TYPE crmt_return_objects,
      lv_schedule_guid   TYPE crmt_object_guid,
      lt_schedule_line   TYPE crmt_schedlin_i_comt,
      lt_schedule_detail TYPE crmt_schedlin_extdt,
      ls_schedule_detail LIKE LINE OF lt_schedule_detail,
      ls_schedule_line   LIKE LINE OF lt_schedule_line,
      lt_changed_fields  TYPE crmt_input_field_tab,
      ls_changed_fields  LIKE LINE OF lt_changed_fields,
      lt_orderadm_i      TYPE TABLE OF crmd_orderadm_i.

START-OF-SELECTION.

SELECT SINGLE * INTO @DATA(ls_header) FROM crmd_orderadm_h WHERE object_id = @srvo_id.

CHECK sy-subrc = 0.

SELECT SINGLE * INTO @DATA(ls_cum_h) FROM crmd_cumulat_h WHERE guid = @ls_header-guid.

CHECK sy-subrc = 0.
lv_srvo_guid = ls_header-guid.

WRITE:/ 'Old gross weight', ls_cum_h-gross_weight COLOR COL_GROUP.
PERFORM print_gross_weight_detail.

SELECT * INTO TABLE lt_orderadm_i FROM crmd_orderadm_i WHERE header = lv_srvo_guid AND object_type = cv_sales_item.
CHECK sy-subrc = 0.

READ TABLE lt_orderadm_i ASSIGNING FIELD-SYMBOL(<sales_item>) with key number_int = item.
IF sy-subrc <> 0.
   WRITE:/ 'Cannot find line item for item id:', item.
   RETURN.
ENDIF.

SELECT SINGLE guid INTO lv_schedule_guid FROM crmd_schedlin WHERE item_guid = <sales_item>-guid.
CHECK lv_schedule_guid IS NOT INITIAL.

ls_schedule_line-ref_guid = <sales_item>-guid.
ls_schedule_detail-guid = ls_schedule_detail-logical_key = lv_schedule_guid.
ls_schedule_detail-item_guid = <sales_item>-guid.
ls_schedule_detail-mode = 'B'.
ls_schedule_detail-quantity = quantity.
APPEND ls_schedule_detail TO ls_schedule_line-schedlines.
APPEND ls_schedule_line TO lt_schedule_line.

ls_changed_fields-ref_guid = <sales_item>-guid.
ls_changed_fields-ref_kind = 'B'.
ls_changed_fields-objectname = 'SCHEDLIN'.
ls_changed_fields-logical_key = lv_schedule_guid.
APPEND 'QUANTITY' TO ls_changed_fields-field_names.
APPEND ls_changed_fields TO lt_changed_fields.

CALL FUNCTION 'CRM_ORDER_MAINTAIN'
  EXPORTING
    it_schedlin_i     = lt_schedule_line
  CHANGING
    ct_input_fields   = lt_changed_fields
  EXCEPTIONS
    error_occurred    = 1
    document_locked   = 2
    no_change_allowed = 3
    no_authority      = 4.

IF sy-subrc <> 0.
  WRITE: / 'error during quantity change'.
  RETURN.
ENDIF.

APPEND lv_srvo_guid TO lt_to_save.

CALL FUNCTION 'CRM_ORDER_SAVE'
  EXPORTING
    it_objects_to_save   = lt_to_save
    iv_update_task_local = abap_true
  IMPORTING
    et_saved_objects     = lt_saved
  EXCEPTIONS
    document_not_saved   = 1.

IF sy-subrc <> 0.
  WRITE:/ 'Save failed'.
  RETURN.
ENDIF.

COMMIT WORK AND WAIT.

SELECT SINGLE * INTO ls_cum_h FROM crmd_cumulat_h WHERE guid = ls_header-guid.

CHECK sy-subrc = 0.
WRITE: / 'New Gross weight after change:' COLOR COL_NEGATIVE, ls_cum_h-gross_weight.
PERFORM print_gross_weight_detail.

FORM print_gross_weight_detail.
  DATA: lt_product TYPE TABLE OF crmd_product_i,
        lt_item TYPE TABLE OF crmd_orderadm_i.

  SELECT * INTO TABLE lt_item FROM crmd_orderadm_i where header = lv_srvo_guid.
  CHECK lt_item IS NOT INITIAL.
  SELECT * INTO TABLE lt_product FROM crmd_product_i FOR ALL ENTRIES IN lt_item
     WHERE guid = lt_item-guid.

  LOOP AT lt_item ASSIGNING FIELD-SYMBOL(<item>).
     WRITE: / 'Item id:' COLOR COL_POSITIVE, <item>-number_int COLOR COL_HEADING.
     READ TABLE lt_product ASSIGNING FIELD-SYMBOL(<prod>) WITH KEY guid = <item>-guid.
     CHECK sy-subrc = 0.
     WRITE: / 'Item Gross Weight:' COLOR COL_POSITIVE, <prod>-gross_weight COLOR COL_GROUP.
  ENDLOOP.
ENDFORM.

How to use this report

Once executed, it will print out the detail of header gross weight before and after change.
Why the header gross weight update is triggered by the change of SCHEDLIN?
1. As could be found from callstack below, the update of gross weight is done in event callback CRM_CUMULAT_H_UPD_BUFFER_EC.
Check event registration table CRMC_EVENT_CALL, and we can know this FM is registered against event TRIGGER_FUNCTION with attribute CUMULAT_H_CALC.
It is CRM_CUMULAT_H_UPD_COLLECT_EC which raises this event, see below screenshot:
So which function will trigger the call of CRM_CUMULAT_H_UPD_COLLECT_EC?
2. Check CRMC_EVENT_CALL again, there are 11 possibilities.
Double check in the runtime, it is FM CRM_PRODUCT_I_PUBLISH_OW where the event AFTER_CHANGE on object PRODUCT_I is raised.
3. Check further via callstack, CRM_PRODUCT_I_A_CHANGE_QUAN_EC will call CRM_PRODUCT_I_PUBLISH_OW.
Query CRM_PRODUCT_I_A_CHANGE_QUAN_EC against CRMC_EVENT_CALL, and we get to the answer finally:
The event AFTER_CHANGE_ORDER_QTY on object SCHEDLIN_I will in the end trigger the update on CUMULAT_H.
To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply