Example 2: Using OData Actions in an Application
You can display the back-end system's response to an action in an application by
writing the response as a message in a text field.
Prerequisites
You have created an application and used a simple and a more complex OData action
within this application for two buttons to book and cancel flights for selected
values.
Procedure
-
Insert six Text widgets on the canvas and rename the
last one to MessageBox.
-
Rewrite your script from the Book Flight button as
follows:
var ret = ODataService_1.executeAction("Flight/Book", {Flight:{Airline:"UA",Flightconnection:"0941", Flightdate: "2019-01-05"},NumberOfSeats: 1});
var succ = "";
if (ret.ok === true) {
succ = "SUCCESS";
} else {
succ = "ERROR";
}
MessageBox.applyText(succ);
Text_2.applyText(succ + " message :" + ret.error.message);
Text_3.applyText(succ + " code :" + ret.error.code);
Text_4.applyText("target :" + ret.error.target);
Text_5.applyText("");
-
Rewrite your script from the Cancel Flight button as
follows:
var ret = ODataService_1.executeAction("CancelMyFlights", {DateFrom: "2019-01-01", DateTo: "2019-12-31"});
console.log(ret);
var succ = "";
if (ret.ok === true) {
succ = "SUCCESS";
} else {
succ = "ERROR";
}
MessageBox.applyText(succ);
Text_2.applyText(succ + " message :" + ret.error.message);
Text_3.applyText(succ + " code :" + ret.error.code);
Text_4.applyText("target :" + ret.error.target);
var info = "";
//if (false) {
if (ret.ok === true) {
var numberofoccupiedseats = ConvertUtils.integerToString(ret.value[0].Numberofoccupiedseats);
var flightprice = ConvertUtils.numberToString(ret.value[0].Flightprice);
var totalnumberofseats = ConvertUtils.integerToString(ret.value[0].Totalnumberofseats);
var currency = ret.value[0].Currency;
info = "Your flight price was " + flightprice + " " + currency + ". " + "There are " + numberofoccupiedseats + " occupied from " + totalnumberofseats + " seats in total.";
}
Text_5.applyText("" + info);
-
Run the application and book and cancel a flight to see the error messages.