cancel
Showing results for 
Search instead for 
Did you mean: 

How to ouput data to the console from the api service SAP s4/HANA Cloud with ODATA V4

cloudsf
Explorer
0 Kudos
749

Hello,

I want to get access to the data, with using th SAP S4/HANA Cloud API Service OData V4.

Try Out | Warehouse - Read (A2X) | SAP Business Accelerator Hub

Thats my Code example how i tried to get acces.

I Used a proxy to solve the cors problem.

sap.ui.define([ 
"sap/ui/model/json/JSONModel", 
"sap/ui/model/odata/v4/ODataModel",
"sap/m/MessageBox" 
], 
function (JSONModel, ODataModel, MessageBox) { 
"use strict"; return {
 var service_Url = "https://cors-anywhere.herokuapp.com/https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata4/sap/api_warehouse_2/srvd_a2x/sap/warehouse/0001/";
var oData_Model = new sap.ui.model.odata.v4.ODataModel(service_Url, true); 

oData_Model.read("/Warehouse')", { 
success: function (oData) { 
console.log(oData); 
var JSON_oModel = new sap.ui.model.json.JSONModel();
 JSON_oModel.setData(oData); 
sap.ui.getCore().setModel(JSON_oModel, "NW_oModel"); 
}, 
error: function () { alert("Konnte ODATA-Service nicht laden!"); } }); },
  

Accepted Solutions (1)

Accepted Solutions (1)

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

You can't simply switch the service URL to an OData V4 service while keeping the APIs from the v2.ODataModel. Please review the OData V4 model documentation "Changes Compared to OData V2 Model".

cloudsf
Explorer
0 Kudos

Hi Boghyon, thanks for your answer and the reference to the documentation, I now understand that the READ command no longer exists in OData V4...

I found this guide where I implemented step one.

Implementation:

sap.ui.define([
    "sap/ui/model/json/JSONModel",
    "sap/ui/model/odata/v4/ODataModel",
    "sap/m/MessageBox"
],
    function (JSONModel, ODataModel, MessageBox) {
        "use strict";
        return {
            create_CL_oModel: function () {
                
                var oModel = new sap.ui.model.odata.v4.ODataModel({
                    serviceUrl: "https://cors-anywhere.herokuapp.com/https://[intern-sap-url]/sap/opu/odata4/sap/api_warehouse_2/srvd_a2x/sap/warehouse/0001/",
                    synchronizationMode: "None",
                });

                console.log(oModel);

            }
        }
    })<br>

Console:

But there is no data in the Odata section, is that correct?

And how could I output the data that the object should deliver to me on the console?

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi cloudsf,

Generally, never rely on what you see in the console when developing applications and logging UI5 objects. Instead, refer to public APIs and documentation topics only.

From the code snippet, I can see that you're logging the model instance (oModel) right after creating it. Depending on the model configuration, the corresponding V4 OData service $metadata document is not fetched right away (unless "earlyRequests" is enabled).

The entities are fetched on-demand based on bindings.

cloudsf
Explorer
0 Kudos

Hi Boghyon Hoffmann,

I'm currently trying to fix the bindings.

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "umlager/evaluation/sfba/umldiaeva/model/models"
],
    function (Controller, models) {
        "use strict";
        return Controller.extend("umlager.evaluation.sfba.umldiaeva.controller.1010_Haupt_Ansicht", {
            onInit: function () {
                //Fiori Theme ::sap_fiori_3::sap_belize::sap_fiori_3_dark
                sap.ui.getCore().applyTheme("sap_fiori_3_dark");

                this.getView().setModel(sap.ui.getCore().getModel("CL_oModel"));

                var clModel = sap.ui.getCore().getModel("CL_oModel");

                console.log("clModel-OUTPUT:");
                console.log(clModel);

                var oNote = clModel.bindProperty("/Warehouse('{1050}')");

                oNote.requestValue().then(function (sValue) {
                    console.log(sValue);


                });

            },

Modell:

sap.ui.define([
    "sap/ui/model/json/JSONModel",
    "sap/ui/model/odata/v4/ODataModel",
    "sap/m/MessageBox"
],
    function (JSONModel, ODataModel, MessageBox) {
        "use strict";
        return {
            create_CL_oModel: function () {

                var oModel = new sap.ui.model.odata.v4.ODataModel({
                    serviceUrl: "https://cors-anywhere.herokuapp.com/https://[intern-url]/sap/opu/odata4/sap/api_warehouse_2/srvd_a2x/sap/warehouse/0001/",
                    earlyRequests: true,
                    synchronizationMode: "None",
                });

                sap.ui.getCore().setModel(oModel, "CL_oModel");

            }
        }
    })

Normalerweise sollten doch jetzt bei console.log(sValue); Daten ankommen oder ?

Console:

Normally, data should arrive at "console.log(sValue);", right?

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

cloudsf As you can see, there are many errors logged in the console.

  • The OData V4 service itself needs to comply with the OData V4 specification e.g. regarding the $metadata response and the response header "OData-Version" having the expected value "4.0". It could be also possible that the service is responding correctly but the client receiving unexpected values due to the proxy server cors-anywhere-herokuapp.com
  • Ensure that the binding path you pass as an argument e.g. to bindProperty is syntactically correct in terms of the OData specification. "/Warehouse('{1050}')" is not correct.

Due to the above errors addressed above, console.log(sValue) cannot work yet.

Also, there is no need to set the model to sap.ui.getCore(). Consider setting the "global" models to your component instead. Refer to https://stackoverflow.com/a/42251431/5846045

Answers (2)

Answers (2)

Giulio_Grandine
Product and Topic Expert
Product and Topic Expert
0 Kudos

Why do you want to change to ODATA V2? ODATA V4 is a completely different architecture than V2 and if the API was developed on ODATA V4 it only makes sense that it might not work properly on V2. While SAP will continue to support applications written on ODATA V2 for the time being, I don't think you can expect compatibility between the two.

cloudsf
Explorer
0 Kudos

Hello Giulio, I would like to switch to OData V4 and tried the V2 version because it worked for me before. In V2 there is this read command with which you can access the data. But this does not work in the V4 version. So I don't know how to get the data in V4.

I think if I had a code example or something similar that can output data to the console via V4, it would have helped me.

For example, I would like to use the "Warehouse - Read (A2X)" from the Business Accelerator Hub.

Warehouse - Read (A2X) - API - OData V4

leonikussmaul
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Ferdinand,

Can you provide some more information? What exactly is logged to the console and loaded to the JSON Model, if the call is successful?

cloudsf
Explorer
0 Kudos

Hi Leoni,

Thank you for your quick response.

thats my Code i ran again.

sap.ui.define([
    "sap/ui/model/json/JSONModel",
    "sap/ui/model/odata/v4/ODataModel",
    "sap/m/MessageBox"
],
    function (JSONModel, ODataModel, MessageBox) {
        "use strict";
        return {            
	//create CL-oModel
            create_CL_oModel: function () {

                //______   https://cors-anywhere.herokuapp.com/corsdemo/   ______ Demo-Server vorherausführen da sonst kein Zugriff!

                var service_Url = "https://cors-anywhere.herokuapp.com/https://sandbox.api.sap.com//sap/opu/odata4/sap/api_warehouse_2/srvd_a2x/sap/warehouse/0001/"
                var oData_Model = new sap.ui.model.odata.v4.ODataModel(service_Url, true);

                console.log("oData_Model_CL:");
                console.log(oData_Model);

                oData_Model.read("/Warehouse')", {
                    success: function (oData) {
                        console.log("oData_IN_CL");
                        console.log(oData);

                        var JSON_oModel = new sap.ui.model.json.JSONModel();

                        JSON_oModel.setData(oData);
                        console.log("JSON_oModel");
                        console.log(JSON_oModel);

                        sap.ui.getCore().setModel(JSON_oModel, "NW_oModel");

                    },
                    error: function () {
                        alert("Konnte ODATA-Service nicht laden!");
                    }
                });
            },

And here is the console output