ABAP code to raise exception with text message in sap odata method

 

While working with odata, in many cases, we need to trigger message to the front end system.

Consider a scenario , we have implement get_entity method for a entity set of odata, and in case record not found based on the key field which is pass, then we need to trigger a message to the front end system.




 




code

  method SOHEADERSET_GET_ENTITY.

  data ls_key_tab type /IWBEP/S_MGW_NAME_VALUE_PAIR.

  data lv_vbeln type vbeln.

"read the key fields

    read TABLE it_key_tab into ls_key_tab with key name 'Vbeln'.
    if sy-subrc 0.

    lv_vbeln ls_key_tab-value.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input         lv_vbeln
     IMPORTING
       OUTPUT        lv_vbeln.


    select SINGLE from zovbak into CORRESPONDING FIELDS OF er_entity WHERE vbeln lv_vbeln.


    endif.

"if no record found trigger the busi exception
   if er_entity-vbeln is INITIAL.

 data lo_message_container TYPE REF TO /iwbep/if_message_container,
        lv_msg_text type BAPI_MSG.


        lo_message_container mo_context->get_message_container).
*        lo_message_container = mo_context->get_message_container( ).


  lv_msg_text 'Sales Order not found.'.

  call METHOD lo_message_container->add_message_text_only
    EXPORTING
      iv_msg_type               'E'                " Message Type - defined by GCS_MESSAGE_TYPE
      iv_msg_text               lv_msg_text                 " Message Text
*      iv_error_category         =                  " Error category - defined by GCS_ERROR_CATEGORY
*      iv_is_leading_message     = abap_true        " Flags this message as the leading error message
*      iv_entity_type            =                  " Entity type/name
*      it_key_tab                =                  " Entity key as name-value pair
*      iv_add_to_response_header = abap_false       " Flag for adding or not the message to the response header
*      iv_message_target         =                  " Target (reference) (e.g. Property ID) of a message
    .

    raise EXCEPTION type /iwbep/cx_mgw_busi_exception
      EXPORTING
*        textid                 =
*        previous               =
        message_container      =  lo_message_container
*        http_status_code       = gcs_http_status_codes-bad_request
*        http_header_parameters =
*        sap_note_id            =
*        msg_code               =
*        entity_type            =
*        message                =
*        message_unlimited      =
*        filter_param           =
*        operation_no           =
    .

   endif.

  endmethod.

Post a Comment

0 Comments

Total Pageviews