Skip to Content
0
Jun 06, 2020 at 11:09 PM

Adding to data to Northwind odata service

277 Views

Hi,

I want to know: Is it possible to add data to the odata Service? And if yes does anyone know how to do that. I want to add products to the ProductSet table. So I have written the following (non working) code but always get an error message that says that one or more of the fields are not filled correctly...even if I fill them correctly...Maybe one of you have an idea:

createProduct: function(oEvent) {
var oProduct = oEvent.getSource().getBindingContext().getObject();
var oModel = this.getModel();

var oBasketModel = this.getModel("product");
var oProductItemEntry = oModel.createEntry("ProductSet");
var oSalesOrderLineItem = oProductItemEntry.getObject();

// Add data
oSalesOrderLineItem.ProductID = "01009000";
oSalesOrderLineItem.TypeCode = "99";
oSalesOrderLineItem.Category = "Category 2";
oSalesOrderLineItem.Name = "Testprodukt";
oSalesOrderLineItem.NameLanguage = "DE";
oSalesOrderLineItem.Description = "Test-Beschreibung";
oSalesOrderLineItem.DescriptionLanguage = "DE";
//oSalesOrderLineItem.SupplierID = "01008888";
//oSalesOrderLineItem.SupplierName = "Lieferant";
oSalesOrderLineItem.TaxTarifCode = 2;
//oSalesOrderLineItem.MeasureUnit = "CM";
//oSalesOrderLineItem.WeightMeasure = 0.180;
//oSalesOrderLineItem.WeightUnit = "KG";
oSalesOrderLineItem.CurrencyCode = oModel.getProperty("/currencyCode");
//oSalesOrderLineItem.Price = 98.000;
//oSalesOrderLineItem.Width = parseFloat(98.00);
//oSalesOrderLineItem.Depth = 98.00;
//oSalesOrderLineItem.Height = 98.00;
//oSalesOrderLineItem.DimUnit = "CM";
oSalesOrderLineItem.CreatedAt = new Date();
oSalesOrderLineItem.ChangedAt = new Date();

//var that = this;
oModel.create("/ProductSet", oSalesOrderLineItem, {
success: function(oData) {
oBasketModel.setProperty("/productId", oData.ProductID);

this.createProduct(oEvent);
}
});
}

Does anyone have an idea to solve my Problem? It seems that the System does not accept the wirtten data. Even if I reduce the amount of items. Most of the fields have to fill in.

As an addition, I have the following working code example for adding orders. If anyone would like to help me - maybe the following code might help...

thx in advance for any hint or solution...

createSalesOrderLineItem: function(oProduct) {
var oModel = this.getModel();
var oBasketModel = this.getModel("basket");

var oSalesOrderLineItemEntry = oModel.createEntry("SalesOrderLineItemSet");
var oSalesOrderLineItem = oSalesOrderLineItemEntry.getObject();

// Add data to an order
oSalesOrderLineItem.SalesOrderID = oBasketModel.getProperty("/salesOrderId");
oSalesOrderLineItem.ProductID = oProduct.ProductID;
oSalesOrderLineItem.Note = oProduct.Name;
oSalesOrderLineItem.Quantity = "1";
oSalesOrderLineItem.CurrencyCode = oBasketModel.getProperty("/currencyCode");
oSalesOrderLineItem.DeliveryDate = new Date();
oModel.create("/SalesOrderLineItemSet", oSalesOrderLineItem, {
success: function(oData) {...}