Skip to main content

SAP CRM: Remove error messages at application level

Submitted by Stefan Barsuhn on

If you want to remove messages from a CRM order and you want the message to be removed at the database level (not only at the UI level), for instance, to be able to remove errors that prevent you from creating a follow-up document (follow-up documents can only be created from error-free documents), you may find the following code snippet useful:

Note that the following is using function modules because the BOL layer does not provide the appropriate method calls:

  DO 1 TIMES.
    DATA(lr_core) = cl_crm_bol_core=>get_instance( ).
    CHECK lr_core IS BOUND.

    lr_core->load_component_set( 'BT' ).
    DATA(lr_entity) = lr_core->get_root_entity( iv_object_name = 'BTOrder' iv_object_guid = iv_guid ).
    CHECK lr_entity IS BOUND.

    " check it's a hold transaction
    DATA lr_type TYPE REF TO crmt_process_type_db.
    lr_type ?= lr_entity->get_properties_by_bpath( `BTOrderHeader/@PROCESS_TYPE` ).

    CHECK lr_type IS BOUND AND lr_type->* EQ 'ZHLD'.

    " we're doing this using FMs because the BOL approach does not give the proper ITEM GUID
    " and does not allow proper delete

    DATA lt_msg_handle TYPE bal_t_msgh.
    CLEAR lt_msg_handle.
    CALL FUNCTION 'CRM_MESSAGES_DISPLAY'
      EXPORTING
        iv_document_number = iv_guid
        iv_surpress_output = abap_true
        iv_in_transaction  = abap_true
      IMPORTING
        et_msg_handle      = lt_msg_handle
      EXCEPTIONS
        OTHERS             = 0.


    " replace the following two messages by warnings
    " CA_TTE  523 Item 10: Define incoming business partner on item level
    " CA_TTE  425 Item 10: TTE processing terminated
    DATA: ls_msg  TYPE bal_s_msg,
          ls_info TYPE crmt_msg_info.

    LOOP AT lt_msg_handle ASSIGNING FIELD-SYMBOL(<fs_message>).
      CLEAR: ls_msg, ls_info.

      " read the message detail
      CALL FUNCTION 'CRM_MESSAGES_GET_MSG_INFO'
        EXPORTING
          is_msg_handle           = <fs_message>
          iv_get_caller_name      = abap_true
        IMPORTING
          es_info                 = ls_info
          es_msg                  = ls_msg
*         EV_REF_OBJECT           =
        EXCEPTIONS
          not_found               = 1
          wrong_context_structure = 2
          data_error              = 3
          OTHERS                  = 4.

      " make sure message was found and it's one of the pricing messages
      CHECK sy-subrc EQ 0 AND
            ls_msg-msgid EQ 'CA_TTE' AND
            ( ls_msg-msgno EQ '523' OR ls_msg-msgno EQ '425' ).

      " 1) delete the message
      " build range tables
      DATA(lt_msgno) = VALUE bal_r_idno( ( sign = 'I' option = 'EQ' low-msgid = ls_msg-msgid low-msgno = ls_msg-msgno ) ).
      CALL FUNCTION 'CRM_MESSAGES_DELETE'
        EXPORTING
          iv_log_handle  = ls_info-log_handle
          it_r_msgidno   = lt_msgno
        EXCEPTIONS
          appl_log_error = 1
          OTHERS         = 2.

      " save message
      DATA(lt_logh) = VALUE bal_t_logh( ( ls_info-log_handle ) ).

      " save message
      CALL FUNCTION 'CRM_MESSAGES_SAVE'
        EXPORTING
          it_log_handle  = lt_logh
*         IT_DOCNUMBER   =
        EXCEPTIONS
          save_error     = 1
          appl_log_error = 2
          OTHERS         = 3.

    ENDLOOP.
  ENDDO.

The important thing here is that you need to call CRM_MESSAGES_SAVE at the end, otherwise the changes will not be permanent.

 

Tags