How to map message to OData response header SAP-Message

1
57787

Hello everyone, in this blog post you will learn how to to map message to OData response header SAP-Message.

Message Container

Message Container in back-end OData service is used to add the success or error messages to the response of the OData service.A standard interface /IWBEP/IF_MESSAGE_CONTAINER is available to add the messages to the OData response.

OData response header parameter “sap-message”

When a request called from SAPUI5/SAP Fiori application is successful and the success, warning messages needs to be send back to the consumer, we can use the response parameter “sap-message” to send the messages.

Message container interface /IWBEP/IF_MESSAGE_CONTAINER is used to map message container to this message protocol.

ODATA Map Message 1

Lets start by creating an example to map the messages in the OData service and how SAPUI5/SAP Fiori application can retrieve these message to display on UI.

Step-by-Step Procedure

#1 Create an OData service

Create a OData service in back-end system/front system with an entity set called “AirlineSet” which retrieves the different airlines available in the table. Here I took this simple example to demonstrate the concept, you may choose a different example as per your need.

We have step-by-step guides available here to explain the process of OData creation, I will not explain the process as we are focusing on this particular concept.

Please continue further after you have an entity set ready and code exists in the Data Provider Class(DPC) extension to get the data. You code may look like below

METHOD airlineset_get_entityset.

  SELECT * FROM scarr INTO CORRESPONDING FIELDS OF TABLE et_entityset.

ENDMETHOD.

 #2 Test the OData Service

Test the OData service in the gateway client, you should see a success response like below with HTTP response code as 200.

ODATA Map Message 2

Now our next step to step is add the some success, warning message to this response. Lets say a sample message about airline discounts. So modify the above code to add the message.

#3 Adding a message to OData response header parameter “sap-message”

Instantiate the Message Container

Method GET_MESSAGE_CONTAINER is found in the DPC extension class to instantiate the message container. Sample ABAP code is available below

  METHOD airlineset_get_entityset.

    SELECT * FROM scarr INTO CORRESPONDING FIELDS OF TABLE et_entityset.

*1. Instatiate the Message Container
    DATA: lo_message_container TYPE REF TO /iwbep/if_message_container.

    CALL METHOD me->/iwbep/if_mgw_conv_srv_runtime~get_message_container
      RECEIVING
        ro_message_container = lo_message_container.

  ENDMETHOD.
 Add the message to the Message Container

To add a message to the message container, we can use any of these methods in the message container interface, in the example we use the basic method ADD_MESSAGE

  • ADD_MESSAGE_FROM_EXCEPTION
  • ADD_MESSAGES_FROM_BAPI
  • ADD_MESSAGE_FROM_BAPI
  • ADD_MESSAGE
  • ADD_MESSAGE_TEXT_ONLY

Method ADD_MESSAGE required message class, id and type of the message.

METHOD airlineset_get_entityset.

    SELECT * FROM scarr INTO CORRESPONDING FIELDS OF TABLE et_entityset.

*1. Instatiate the Message Container
    DATA: lo_message_container TYPE REF TO /iwbep/if_message_container.

    CALL METHOD me->/iwbep/if_mgw_conv_srv_runtime~get_message_container
      RECEIVING
        ro_message_container = lo_message_container.

    CALL METHOD lo_message_container->add_message
      EXPORTING
        iv_msg_type               = /iwbep/cl_cos_logger=>warning
        iv_msg_id                 = 'ZTEST'
        iv_msg_number             = '000'
        iv_add_to_response_header = abap_true. "add the message to the header

  ENDMETHOD.

The important parameter which should be noted here is IV_ADD_TO_RESPONSE_HEADER,if we set this parameter as true then the message will be added to the response header parameter “sap-message”.

Now test the service again in the gateway client to validate whether the message exists in the response header.

ODATA Map Message 3

In the above OData response, the message appears in the header parameter “sap-message”.

We have successfully map the message container to the response header parameter “sap-message”.

Display the message in SAPUI5 / SAP Fiori application

To display the message in SAPUI5 and SAP Fiori applications, we need to read the response object and read the message. Below sample JS code snippet can be used to read the header message.

Below is the code of a view controller, in this example we log the message to the console. You may have alternative way to display the messages in dialog

sap.ui.define([
  "sap/ui/core/mvc/Controller"
], function(Controller) {
  "use strict";

  return Controller.extend("com.saplearners.controller.View1", {
    onInit: function() {

      var oModel = new sap.ui.model.json.JSONModel();
      this.getView().setModel(oModel, "viewData");

      var oServiceModel = new sap.ui.model.odata.v2.ODataModel("/sap/opu/odata/sap/ZMSG_CONT_DEMO_SRV/");
      var mParameters = {
        method: "GET",
        success: jQuery.proxy(function(oData, response) {
          this.getView().getModel("viewData").setProperty("/AirlineSet", oData.results);

          // response header
          var hdrMessage = response.headers["sap-message"];
          var hdrMessageObject = JSON.parse(hdrMessage);

          // log the header message
          console.log(hdrMessageObject);
          console.log(hdrMessageObject.message);
        }, this),

        error: jQuery.proxy(function(oError) {

        }, this)
      };

      oServiceModel.read("/AirlineSet", mParameters);
    }

  });
});
 Console

ODATA Map Message 4

Congrats..! We have successfully learned how to map message to OData response header SAP-Message. Please stay tuned for more SAP OData tutorials.

Comments are closed.