on 2023 May 11 8:09 AM
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!"); } }); },
Request clarification before answering.
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".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
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.
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?
cloudsf As you can see, there are many errors logged in the console.
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
User | Count |
---|---|
85 | |
11 | |
8 | |
8 | |
6 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.