Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Problem parsing oData v2 response

Hello Experts,

I'm using a custom oData service to get employeeNumber(s) from userID (mapped to GET_ENTITYSET).

Entity structure is very simple (Employee):

UserID

EmployeeNumber (key)

A UserID is sent to the service and Pernr(s) are sent from the service.

SAPUI5 code:

        var oModel = new sap.ui.model.odata.v2.ODataModel(sRoot + "/sap/opu/odata/sap/ZOMRI_HR1_SRV");

      

        oModel.attachRequestCompleted(function() {

            var k = Object.keys(this.oData)[0]; // Works but I don't think this is proper code

            var employeeNumber= this.oData[k].EmployeeNumber;

            console.log("employeeNumber-->"+employeeNumber);

        });

        var aFilters = [];

        var nameFilter = new sap.ui.model.Filter("UserID", sap.ui.model.FilterOperator.EQ, "OMRICOH");

        aFilters.push(nameFilter);

     

        var mParameters = {

                  filters : aFilters //,

                 //urlParameters:{"$format" : "json"} // Not needed (default in v2)

        };

        oModel.read("/Employees", mParameters);

    }

In the above example input parameter name is 'UserID' and value is 'OMRICOH'

Output looks like this (in SAP Gateway Client)

{

  "d" : {

    "results" : [

      {

        "__metadata" : {

          "id" : "http://<server:port>/sap/opu/odata/sap/ZOMRI_HR1_SRV/Employees('01000659')",

          "uri" : "http://<server:port>/sap/opu/odata/sap/ZOMRI_HR1_SRV/Employees('01000659')",

          "type" : "ZOMRI_HR1_SRV.Employee"

        },

        "UserID" : "",

        "EmployeeNumber" : "01000659"

      }

    ]

  }

}

I need to get to the 'EmployeeNumber' value.

The problem is that when I try to get the output in a simple way:

1) I tried o.getParameter("response").responseText in 'attachRequestCompleted' function:

"{"d":{"results":[{"__metadata":{"id":"<server:port>/sap/opu/odata/sap/ZOMRI_HR1_SRV/Employees('01000659')","uri":"<server:port>/sap/opu/odata/sap/ZOMRI_HR1_SRV/Employees('01000659')","type":"ZOMRI_HR1_SRV.Employee"},"UserID":"","EmployeeNumber":"01000659"}]}}"

'responseText' is a String and not an object...

2) The following code works:

var k = Object.keys(this.oData)[0];

var employeeNumber= this.oData[k].EmployeeNumber;

However the main problem is that the response object contains the required value in the object key, this.oData returns (from 'attachRequestCompleted' function):

Object {Employees('01000659'): Object}

--Employees('01000659'): Object

----EmployeeNumber: "01000659"

----UserID: ""

So what am I missing?

What do I need to change the SAPUI5 code in order to parse the response correctly or does the problem come from the GW side?

SAPUI5 version is 1.28.7

Regards,

Omri

Former Member replied

Hi,

I have two suggestions that you can try, but I don't know if they will work since I can't see the data that you are receiving.

Option 1:

Once you get the response text, use

var responseObject = JSON.parsere(responseText);

Then access your data through the responseObject.

Option 2:

I don't know if using attachRequestCompleted is a requirement for you, but if it's not then you can remove it and rather have the callBack function defined directly in your oModel.read() request. For example:

var mParameters = {

   filters: aFilters,

  success: function(oData, oResponse){

debugger;

}

};

oModel.read("/Employees", mParameters);

the oData parameter should then contain the returned data, but you can also try and access it in oResponse. As I can't inspect the data myself, I can't say for sure though. Take a look at JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.odata.v2.ODataModel for more information on it.

Hope this is helpful

Regards

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question