sonja
sonja

Reputation: 934

Write date to Odata Model 0CALDAY as string?

I add new data to my oData Model by creating a new Entry like this:

_.forEach(insert, data => {
var entry = (modelprefix + "('" + data.COL01 + "')");
oModel.create(modelprefix, data, mParameters);
});

That code is working finde for strings and numbers, but when it comes to a date, it doesn't work.

My target structure contains a 0CALDAY column, in which the values from the "insert" JSON Model should get parsed. I don't get an error or anything, just nothing happens..

I suspect that this is because 0CALDAY expects an object to be inserted, but it gets only a String. But what can I do?

That's one object of my Insert-JSON-Model: COL04 is the date to be inserted. It already has the correct format for my SAP! (dd.mm.yy)enter image description here

Upvotes: 0

Views: 86

Answers (1)

James
James

Reputation: 22247

I think I get what you're doing, this should take your original data run it through the "types" thing and create a date if it's typed date. For demo purposes it also converts the numbers in the types array.

var data = [
  {"AGIMENDO Info Objekt 1": "00000000", "Beschreibung Kurz": "Test0", "Währung": "200.00", "__rowNum__": 1},
  {"AGIMENDO Info Objekt 1": "00000001", "Beschreibung Kurz": "Update1", "Währung": "456.00", "__rowNum__": 2},
  {"AGIMENDO Info Objekt 1": "00000002", "Beschreibung Kurz": "Test2", "Währung": "12.00", "__rowNum__": 3},
  {"AGIMENDO Info Objekt 1": "00000003", "Beschreibung Kurz": "Test3", "Währung": "549153.00", "__rowNum__": 4},
  {"AGIMENDO Info Objekt 1": "00000004", "Beschreibung Kurz": "Text", "Währung": "1.05", "__rowNum__": 5},
  {"AGIMENDO Info Objekt 1": "00000005", "Beschreibung Kurz": "13.08.11", "Währung": "465.00", "__rowNum__": 6},
  {"AGIMENDO Info Objekt 1": "00000006", "Beschreibung Kurz": "Test21", "Währung": "4594.00", "__rowNum__": 7}
];

var types = [
  {type: "number", value: "200.00"},
  {type: "number", value: "456.00"},
  {type: "number", value: "12.00"},
  {type: "number", value: "549153.00"},
  {type: "number", value: "1.05"},
  {type: "date", value: "13.08.11"},
  {type: "number", value: "465.00"}
];

var typeVals = types.map(el => el.value);

function toDate(str) {
  var dateParts = str.split(".");
  var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
  return date;
}
function toNum(str) {
  return parseFloat(str);
}


data.forEach(row => {
  var keys = Object.keys(row);
  keys.forEach(k => {
    var t = typeVals.indexOf(row[k]);
    if (t != -1) {
      switch (types[t].type) {
        case "date":
          row[k] = toDate(row[k]);
          break;
        case "number":
          row[k] = toNum(row[k]);
          break;
      }
    }
  });
});

console.log(data);

Upvotes: 1

Related Questions