UI5 – Updating a record

Home / Blog / UI5 – Updating a record

I’m feeling pretty excited right now.  I’ve been struggling for over a week to get a stupid POST or PUT statement to work in my UI5 application.  Let’s just say, there have been a lot of “color metaphors” said after the kids went to bed.  I’ve scoured google, tried so many things I can’t even remember them all.  Finally, today I was able to pull it all together.

First, the issue I was running into was that my GET services worked fine everywhere, but my PUT & POST would only work in a rest client.  For some reason, the X-CSRF-TOKEN was always undefined, so the service could never work.

component.js

I’m using an oDataModel, and I’m including this so you know what headers I’m setting.  It turns out that the Content-Type was my undoing.  I had been using application/atom+xml, as soon as I changed it to json, everything started working.

oModel.oHeaders = {
“DataServiceVersion”: “2.0”,
“MaxDataServiceVersion”: “2.0”,
“X-Requested-With”: “XMLHttpRequest”,
“Content-Type”: “application/json”,
}

detail.controller.js

I made a method to execute this upon save.  My next step is make it more dynamic, taking values from the screen, but step one was hardcoding.  /Dispatch is my service that does the PUT.

handleSaveButtonPress : function (evt) {
var oEntry = {};
oEntry.IOrderNumber = “1000001”;
oEntry.IPriority = “1”;
oEntry.ISequence = “25”;
var lServ = “/Dispatch(‘” + “1000001” + “‘)”;

var oModel = this.getView().getModel(“orders”);

jQuery.sap.require(“sap.ui.commons.MessageBox”);
oModel.update(lServ, oEntry, null, function(){
oModel.refresh();
sap.ui.commons.MessageBox.alert(“Success!”)
},function(){
sap.ui.commons.MessageBox.alert(“Error!”);
})

}

It all looks so simple now… but believe me, what a headache.  I hope this can help someone else in the future.

Thanks for reading,

As always, thanks for reading and don't forget to check out our SAP Service Management Products at my other company JaveLLin Solutions,
Mike

Leave a Reply

Your email address will not be published. Required fields are marked *