Search code examples
odatasapui5

getProperty of entity in Odata model in SAPUI5 not working


I have a oData model with entities : Order, OrderInformation. There is 1 : 1 an association between Order and OrderInformation.

Now in the view, based on a value in OrderInformation, I should hide / display a button.

In the controller, following logic to get the value of OrderInformation->appUrl does not work but I can read the property of entity 'Order'.

   Init: function(){
   // Not working
    var prop = this.getView().getModel().getProperty("/OrderInformations('"+ this._orderId + "')/appUrl");
   // Working
    var prop = this.getView().getModel().getProperty("/Orders('"+ this._orderId + "')/orderType");
    }

In transaction /IWFND/GW_CLIENT, following query gives me correct value

/sap/opu/odata/sap/<<ServiceURL>>/OrderInformations('132123')/appUrl  

I also tried with the attachRequestCompleted but still no success.

Init:function(){
    var oModel = this.getView().getModel();
    oModel.attachRequestCompleted(function(oEvent){
        var myval = model.getProperty("/OrderInformations('"+ this._orderId + "')/appUrl");
        });
}

Can someone provide any idea what can be going wrong ?

BR Nilesh


Solution

  • You can use the oModel.read function to trigger a request to the backend, within the success handler you read the result of the response and process the received data

    var test = oModel.read("OrderInformations('" + this._orderId + "')", {
        success: function(oData, response) {
            var appUrl = oData.result.appUrl; //response.data.appUrl also works
            // do something
        },
        error: function (oError) {
            // Error handling on failed response
        }
    });
    

    API reference: https://openui5beta.hana.ondemand.com/#docs/api/symbols/sap.ui.model.odata.ODataModel.html#read

    I don't understand this line you wrote:

    In the controller, following logic to get the value of OrderInformation->appUrl does not work but I can read the property of entity 'Order'.

    Order is another Entity with a property and the addressing for this works like described above? Did you init your model like this: /sap/opu/odata/sap/<<ServiceURL>>/Order? Is OrderInformation a related entity of Order? If yes extend the read with the Navigation property of the odata service which defines the relationship between the two Entities

    I hope this answers you question, if anything left, let me know

    Best regards