Well, you can send custom messages as a response in OData, we will discuss the same in this blog along with the delete operation in OData.
There are two ways to send the messages in the HTTP response
- Messages inside the response header
- Messages inside the Body of HTTP response
- Create Message container Object : In both ways, the primary step is to create a message container object which holds the messages. The message container object is of type /iwbep/if_message_container
- Fill the message container Object
- Raise exception if you want send message inside the Body of HTTP Response
Let’s implement the delete operation of OData and implement the messaging logic inside that. Similar to update operation as covered in Part 8, Delete Operation has a method associated with it called DELETE_ENTITY
METHOD orderrheaderset_delete_entity. DATA:ls_entity TYPE zcl_zxyz_sample_odata_mpc_ext=>ts_orderrheader, ls_order_header_inx TYPE bapisdh1x, lt_return TYPE TABLE OF bapiret2. DATA lo_message_container TYPE REF TO /iwbep/if_message_container. "Get the Key value - with leading zeros io_tech_request_context->get_converted_keys( IMPORTING es_key_values = ls_entity ). "Delete the sales order using BAPI ls_order_header_inx-updateflag = 'D'. CALL FUNCTION 'BAPI_SALESORDER_CHANGE' EXPORTING salesdocument = ls_entity-vbeln order_header_inx = ls_order_header_inx TABLES return = lt_return. READ TABLE lt_return TRANSPORTING NO FIELDS WITH KEY type = 'E'. IF sy-subrc NE 0. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = abap_true. "Sending a success/information message - to the response header "Step 1- Create a message container object lo_message_container = me->mo_context->get_message_container( ). "Step 2- Fill the message container lo_message_container->add_message_text_only( EXPORTING iv_msg_type = 'S' iv_msg_text = 'Sales Order Deleted Successfully' iv_add_to_response_header = abap_true ). ELSE. "Send an Error message by raising Exception "Step 1- Create a message container object lo_message_container = me->mo_context->get_message_container( ). "Step 2- Fill the message container lo_message_container->add_messages_from_bapi( EXPORTING it_bapi_messages = lt_return ). "Step 3- Raise the exception RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception EXPORTING textid = /iwbep/cx_mgw_busi_exception=>business_error message_container = lo_message_container. ENDIF. ENDMETHOD.
Let’s trigger a delete call in the SAP Gateway client
As the sales order, 32346 has been deleted during the first delete call. If it is called for the second time then it will give you an error message, and as per the above-implemented logic, the message will appear in the response body.
If you notice the status code while sending response inside the HTTP header is 204 and while if you are raising the exception then the status code will be 400.
Note: Exception is of two categories one is TECH (Interface /iwbep/cx_mgw_tech_exception) and other is BUSI ( Interface /iwbep/cx_mgw_busi_exception ) as the name suggest TECH exception will be raised when the message is more related to technical information like type casting error, BUSI exception will be raised when the message is more related to business process like fails to create sales order, etc. )
Note: There are a couple of methods to add a message into a message container, you can explore the same while implementing the code
Check out our ABAP blogs: ABAP – ABAP Skill