Header Ads

CRM - change document appointment date

To set up the appointment date we have to add a code similar to below code in 
CRM_APPOINTMENT_BADI  - Method IF_EX_CRM_APPOINTMENT_BADI~CRM_APPOINTMENT_MERGE
*----------------------------------------------
* DATA DECLARATION
*----------------------------------------------
INCLUDE crm_direct.
*- Document
DATA lv_guid              TYPE crmt_object_guid.
DATA lv_item_guid         TYPE crmt_object_guid.
DATA lt_req_objects       TYPE crmt_object_name_tab.
DATA lt_header_guid       TYPE crmt_object_guid_tab.
DATA lt_appointment       TYPE crmt_appointment_wrkt.
DATA ls_appointment       TYPE crmt_appointment_wrk.
DATA lt_appointment_com   TYPE crmt_appointment_comt.
DATA ls_appointment_com   TYPE crmt_appointment_com.
*- Input fiels and others
DATA ls_logical_date_key  TYPE crmt_date_logical_date_key.
DATA lt_input_fields      TYPE crmt_input_field_tab.
DATA ls_input_fields      TYPE crmt_input_field.
DATA ls_input_field_names TYPE crmt_input_field_names.
DATA lt_input_field_names TYPE crmt_input_field_names_tab.
*- Aux
DATA lv_timestamp_from    TYPE timestamp.
DATA lv_timestamp_to      TYPE timestamp.
DATA lt_saved_objects     TYPE crmt_return_objects.
*----------------------------------------------
* SELECTION SCREEN * This part is added for you to test the code as an independent report program
*----------------------------------------------
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-b01.
PARAMETERS p_id       TYPE crmt_object_id. "Document ID
PARAMETERS p_kind     TYPE crmt_object_kind. "A-Header, B-Item
PARAMETERS p_item     TYPE crmd_orderadm_i-number_int. "Item number. Example: 10
SELECTION-SCREEN: END OF BLOCK b1.
SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME TITLE text-b02.
PARAMETERS p_type     TYPE crmt_appointment_wrk-appt_type. "Appointment Type
PARAMETERS p_datef    TYPE sy-datlo. "Date from
PARAMETERS p_timef    TYPE sy-timlo. "Time from
PARAMETERS p_datet    TYPE sy-datlo. "Date to
PARAMETERS p_timet    TYPE sy-timlo. "Time to
SELECTION-SCREEN: END OF BLOCK b2.
*----------------------------------------------
* READY, SET... GO!
*----------------------------------------------
START-OF-SELECTION.
*- Check Fields
  IF p_id IS INITIAL OR p_kind IS INITIAL OR p_type IS INITIAL OR p_datef IS INITIAL OR p_timef IS INITIAL.
    MESSAGE 'Please fill all input fields, except date_to and time_to' TYPE 'E'.
  ENDIF.
  IF p_kind = 'B' AND p_item IS INITIAL.
    MESSAGE 'Please fill item number to update' TYPE 'E'.
  ENDIF.
*- Get document GUID
  SELECT SINGLE guid FROM crmd_orderadm_h INTO lv_guid WHERE object_id = p_id.
  IF SY-SUBRC NE 0.
    MESSAGE 'Document not found' TYPE 'E'.
  ENDIF.
*- Get item GUID?
  IF p_kind = gc_object_kind-orderadm_i.
    SELECT SINGLE guid FROM crmd_orderadm_i INTO lv_item_guid WHERE header = lv_guid AND number_int = p_item.
    IF SY-SUBRC NE 0.
      MESSAGE 'Item not found' TYPE 'E'.
    ENDIF.
  ENDIF.
*- Read Document Info
  INSERT lv_guid INTO TABLE lt_header_guid.
  INSERT gc_object_name-appointment INTO TABLE lt_req_objects.
  CALL FUNCTION 'CRM_ORDER_READ'
    EXPORTING
      it_header_guid       = lt_header_guid
      iv_mode              = gc_mode-display
      it_requested_objects = lt_req_objects
    IMPORTING
      et_appointment       = lt_appointment
    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.
  IF sy-subrc NE 0.
    MESSAGE 'Error reading document' TYPE 'E'.
  ENDIF.
*- Calculate timestamp for new date/time
  CONVERT DATE p_datef TIME p_timef INTO TIME STAMP lv_timestamp_from TIME ZONE sy-zonlo.
  CONVERT DATE p_datet TIME p_timet INTO TIME STAMP lv_timestamp_to TIME ZONE sy-zonlo.
*- Get current appointment data
  IF p_kind NE gc_object_kind-orderadm_i.
    READ TABLE lt_appointment INTO ls_appointment WITH KEY ref_kind = p_kind appt_type = p_type.
  ELSE.
    READ TABLE lt_appointment INTO ls_appointment WITH KEY ref_kind = p_kind ref_guid = lv_item_guid appt_type = p_type.
  ENDIF.
  IF sy-subrc NE 0.
    MESSAGE 'Appointment type not found in document' TYPE 'E'.
  ENDIF.
*- Format the appointment data
  MOVE-CORRESPONDING ls_appointment TO ls_appointment_com.
  ls_appointment_com-timestamp_from = lv_timestamp_from.
  ls_appointment_com-timestamp_to = lv_timestamp_to.
*- Set logical key and build input fields
  MOVE-CORRESPONDING ls_appointment_com TO ls_logical_date_key.
  CALL FUNCTION 'CRM_APPT_BUILD_INPUT_FIELDS'
    EXPORTING
      iv_ref_handle   = '0'
      iv_ref_guid     = ls_appointment_com-ref_guid
      iv_ref_kind     = ls_appointment_com-ref_kind
      is_logical_key  = ls_logical_date_key
    IMPORTING
      es_input_fields = ls_input_fields.
  IF sy-subrc <> 0.
    MESSAGE 'Error generating input fields' TYPE 'E'.
  ENDIF.
  INSERT ls_input_fields INTO TABLE lt_input_fields.
  INSERT ls_appointment_com INTO TABLE lt_appointment_com.
*- Clear the reference of the timecontext for recalculation of the dates
  CALL FUNCTION 'CRM_DATES_TIMECONTEXT_DELETE'.
*- Change appointment in memory
  CALL FUNCTION 'CRM_APPT_MAINTAIN_MULTI_OW'
    EXPORTING
      iv_ref_guid            = ls_appointment_com-ref_guid
      iv_ref_kind            = ls_appointment_com-ref_kind
      it_appointment_com     = lt_appointment_com
    CHANGING
      ct_input_fields        = lt_input_fields
    EXCEPTIONS
      precondition_violation = 1
      invariant_violation    = 2
      OTHERS                 = 3.
  IF sy-subrc <> 0.
    MESSAGE 'Error changing appointment' TYPE 'E'.
  ENDIF.
*- Save document
  CALL FUNCTION 'CRM_ORDER_SAVE'
    EXPORTING
      IT_OBJECTS_TO_SAVE   = lt_header_guid
      IV_UPDATE_TASK_LOCAL = 'X'
    IMPORTING
      ET_SAVED_OBJECTS     = lt_saved_objects
    EXCEPTIONS
      DOCUMENT_NOT_SAVED   = 1
      OTHERS               = 2.
  IF sy-subrc <> 0 OR lt_saved_objects IS INITIAL.
    MESSAGE 'Error saving document' TYPE 'E'.
  ELSE.
    MESSAGE 'Date updated and saved!' TYPE 'S'.
  ENDIF.
  COMMIT WORK.
Powered by Blogger.