Skip to Content
0
Sep 24, 2018 at 09:46 AM

How to add a new record by using public OData (Read-Write) service?

193 Views

Dear SAP experts

I have created a simple UI5 web app using SAP WEB IDE Full-Stack tool. I took the data from https://services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/Products and the data is displayed properly.

Now I am expecting that I can add a new record as well. I have created the form, and a button to submit the data. I also have created the controller as below :

handleSave : function () {
var newData = {
"odata.type": "ODataDemo.Product",
"ID": this.getView().byId("InputID").getValue(),
"Name": this.getView().byId("InputName").getValue(),
"Description": this.getView().byId("InputDescription").getValue(),
"ReleaseDate": this.getView().byId("InputReleaseDate").getValue(),
"DiscontinuedDate": this.getView().byId("InputDiscontinuedDate").getValue(),
"Rating": this.getView().byId("InputRating").getValue(),
"Price": this.getView().byId("InputPrice").getValue()
};

var oModel = new sap.ui.model.odata.v2.ODataModel("https://services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/");

oModel.create(
"/Products", 
newData,
{
async : true,
success : function(oSuccess) {
alert("success..");
},
error : function(oError) {
alert("fail..");
}
}
);
} 

But it is not working. When I count the data by running the following url, the number of data is remaining same. https://services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/Products/$count

I have tried the following function as well, but it is also not working.

handleSave : function () {
var newData = {
"odata.type": "ODataDemo.Product",
"ID": this.getView().byId("InputID").getValue(),
"Name": this.getView().byId("InputName").getValue(),
"Description": this.getView().byId("InputDescription").getValue(),
"ReleaseDate": this.getView().byId("InputReleaseDate").getValue(),
"DiscontinuedDate": this.getView().byId("InputDiscontinuedDate").getValue(),
"Rating": this.getView().byId("InputRating").getValue(),
"Price": this.getView().byId("InputPrice").getValue()
};

var requestObj = {
requestUri: '',
method: '',
headers: {
"X-Requested-With" : "XMLHttpRequest",
"Content-Type" : "application/javascript",
"DataServiceVersion" : "2.0",
"MaxDataServiceVersion" : "2.0",
"Accept" : "application/javascript"
}
};

var url = "proxy/http/services.odata.org/V2/(S(zgiajht3rj0txzdxqds0itlv))/OData/OData.svc/";
var method = "POST";
requestObj.requestUri = url;
requestObj.method = method;
requestObj.data = newData;
OData.request(requestObj, function() {
this.getView().byId("idAppControl").getModel().refresh();
this.getView().byId("SimpleForm").setVisible(false);
});
} 

Please kindly help me to check why the two functions above are not working.

Is there any other way to add a new record? Please advise.