Scripting Example 2: Use OData Actions in Analytics Designer or Optimized Story Experience

As an application designer or story developer, you can display the back end system's response to an OData action run in your analytic application or optimized story via text widgets and scripts.

Prerequisites

You've created an analytic application or optimized story with OData actions and two buttons to book and cancel flights for selected values.

Procedure

  1. Add six text widgets.
  2. For Text_6, enter MessageBox as its ID and MessageBox as the text.

  3. For the Book Flight button, rewrite with the following script:
    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(""); 
  4. For the Cancel Flight button, rewrite with the following script:
    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); 
  5. Run the application or story, and book and cancel a flight to see the error messages.