title: How to delete data from the model and database on button click in SAPUI5?
body: <p><a href="https://i.stack.imgur.com/Hjz58.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hjz58.jpg" alt="Folder structure"></a></p>
  
  <p>Hi am trying to delete a single row from the database and model on button click. In attached picture you can see my file structure.</p>
  
  <p>mymodel.hdbdd:</p>
  
  <pre><code>namespace I313766_Hubert.perslist.data;
  @Schema: 'i313766_perslist'
  context mymodel {
   type SString: String(60);
   @Catalog.tableType: #COLUMN
   Entity person {
   key ID: String(10); // element modifier 'key' defines that ID is primary key
   FIRSTNAME: SString;
   LASTNAME: SString;
   };
  
    context procedures{
   type pers {
   ID: String(10);
   FIRSTNAME: SString;
   LASTNAME: SString;
   };
   type errors {
   HTTP_STATUS_CODE : Integer;
   ERROR_MESSAGE : String(100);
   DETAIL : String(100);
   };
   };
  };
  </code></pre>
  
  <p>perslist.controller.js:</p>
  
  <pre><code>sap.ui.controller("views.perslist", {
      onInit: function() {
          var sOrigin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ":" + window.location.port : "");
          var persListOdataServiceUrl = sOrigin + "/I313766_Hubert/perslist/services/pers.xsodata";
          var odataModel = new sap.ui.model.odata.ODataModel(persListOdataServiceUrl);
          this.getView().setModel(odataModel);
      },
      addNewPerson: function() {
          var firstName = this.getView().getFirstNameField().getValue();
          var lastName = this.getView().getLastNameField().getValue();
          var persons = {};
          persons.ID = "1";
          persons.FIRSTNAME = firstName;
          persons.LASTNAME = lastName;
          this.getView().getModel()
              .create("/Persons", persons, null, this.successMsg, this.errorMsg);
      },
      successMsg: function() {
          sap.ui.commons.MessageBox.alert("Person entity has been successfully created");
      },
      errorMsg: function() {
          sap.ui.commons.MessageBox.alert("Error occured when creating person entity");
      },
      onAfterRendering: function() {
          this.getView().getFirstNameField().focus();
      },
      deletePerson: function() {
               var oTable = sap.ui.getCore().byId("persTable");
               var data = oTable.getModel();
              // var removeModel = new sap.ui.model.odata.ODataModel("../services/requestCreate.xsodata/", true);
              if(data === null)
              {
                  sap.ui.commons.MessageBox.show("Model value empty!","ERROR","ERROR");
              }
               var RowId = data.getProperty("firstNameFieldId",oTable.getContextByIndex(oTable.getSelectedIndex()));
               sap.ui.commons.MessageBox.show(RowId,"ERROR","ERROR");
               var aUrl2 = '../../services/ExcelQuery.xsjs?cmd=DeleteRequest'+'&amp;idrequest='+escape(RowId)+''; 
                      // var aUrl2 = '../../../services/poWorklistQuery.xsjs?cmd=getTotalOrders'+'&amp;groupby='+escape(groupBy)+'&amp;currency=USD&amp;filterterms=';
                          jQuery.ajax({
                             url: aUrl2,
                             method: 'GET',
                             dataType: 'json',
                             success: function ()
                             {
                                 sap.ui.commons.MessageBox.show("Finally it worked","SUCCESS","SUCCESS");
                             },
                             error: function ()
                             {
                                 sap.ui.commons.MessageBox.show("NOPE NOPE NOPE","ERROR","ERROR");
                             }
                          });
  
      }
  });
  </code></pre>
  
  <p>perslist.view.js:</p>
  
  <pre><code>sap.ui.jsview("views.perslist", {
   oFirstNameField : null,
   oLastNameField : null,
   getControllerName : function() {
  return "views.perslist";
   },
   createContent : function(oController) { // Create an instance of the table control
  var oTable = new sap.ui.table.Table({ title : "Persons List", visibleRowCount : 6,
   firstVisibleRow : 3, selectionMode : sap.ui.table.SelectionMode.Single, id: "persTable" });
  // add TableToolbar
  var oTableToolbar = new sap.ui.commons.Toolbar();
  // add first name field
  var oFirstNameLabel = new sap.ui.commons.Label({ text : 'First Name' });
  this.oFirstNameField = new sap.ui.commons.TextField({ id : 'firstNameFieldId', value : '',
   width : '10em' });
   oFirstNameLabel.setLabelFor(this.oFirstNameField);
   oTableToolbar.addItem(oFirstNameLabel);
   oTableToolbar.addItem(this.oFirstNameField);
  // add last name field
  var oLastNameLabel = new sap.ui.commons.Label({ text : 'Last Name' });
  this.oLastNameField = new sap.ui.commons.TextField({ id : 'lastNameFieldId', value : '',
   width : '10em' });
   oLastNameLabel.setLabelFor(this.oLastNameField);
   oTableToolbar.addItem(oLastNameLabel);
   oTableToolbar.addItem(this.oLastNameField);
  // add button
  var oAddPersonButton = new sap.ui.commons.Button({ id : 'addPersonButtonId',
   text : "Add Person", press : function() {
   oController.addNewPerson();
   } });
  
   var oDelPersonButton = new sap.ui.commons.Button({ id : 'delPersonButtonId',
   text : "Delete person", press : function() {
  oController.deletePerson();
   } });
   oTableToolbar.addItem(oAddPersonButton);
   oTableToolbar.addItem(oDelPersonButton);
   oTable.setToolbar(oTableToolbar);
  // define the columns and the control templates to be used
   oTable.addColumn(new sap.ui.table.Column({
   label : new sap.ui.commons.Label({ text : "First Name" }),
   template : new sap.ui.commons.TextView().bindProperty("text", "FIRSTNAME"),
   sortProperty : "FIRSTNAME", filterProperty : "FIRSTNAME", width : "100px" }));
   oTable.addColumn(new sap.ui.table.Column({
   label : new sap.ui.commons.Label({ text : "Last Name" }),
   template : new sap.ui.commons.TextView().bindProperty("text", "LASTNAME"),
   sortProperty : "LASTNAME", filterProperty : "LASTNAME", width : "200px" }));
  // bind table rows to /Persons based on the model defined in the init method of the
  // controller (aggregation binding)
   oTable.bindRows("/Persons");
  return oTable;
   },
   getFirstNameField : function() {
  return this.oFirstNameField;
   },
   getLastNameField : function() {
  return this.oLastNameField;
   }
  });
  </code></pre>
  
  <p>index.html:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
  &lt;title&gt;SAP HANA Platform E2E Dev Scenario: SAP HANA native
   PersonsList application&lt;/title&gt;
  &lt;script src="/sap/ui5/1/resources/sap-ui-core.js" id="sap-ui-bootstrap"
  data-sap-ui-libs="sap.ui.commons, sap.ui.table,sap.ui.ux3"
  data-sap-ui-theme="sap_goldreflection"&gt;
  &lt;/script&gt;
  &lt;script&gt;
   sap.ui.localResources("views");
  var oAppHeader = new sap.ui.commons.ApplicationHeader("appHeader");
   oAppHeader.setLogoSrc("http://www.sap.com/global/images/SAPLogo.gif");
   oAppHeader.setLogoText("PersonsList application in SAP HANA XS");
   oAppHeader.setDisplayWelcome(false);
   oAppHeader.setDisplayLogoff(false);
   oAppHeader.placeAt("header");
  var view = sap.ui.view({ id : "perslist-id", viewName : "views.perslist",
   type : sap.ui.core.mvc.ViewType.JS });
   view.placeAt("content");
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body class="sapUiBody" role="application"&gt;
   &lt;div id="header"&gt;&lt;/div&gt;
   &lt;div id="content"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>ExcelQuery.xsjs:</p>
  
  <pre><code>function DeleteRequest() {
      var body = '';
  
      try {
  
          var IDRequest = $.request.parameters.get('idrequest');
  
          var query = 'Delete from \"I313766_Hubert.perslist.data::mymodel\" where \"Firstname\" = \''+ IDRequest +'\' ';
  
          //var query2 = 'Delete from \"IOREQUEST"."lucas.Exercise_UI.data::DaysUsed\" where \"IDRequest\" = \''+ IDRequest +'\' ';
  
          //select * FROM "IOREQUEST"."lucas.Exercise_UI.data::DaysUsed" where "IDRequest" like '%I033828%'
  
          $.trace.debug(query);
          var conn = $.db.getConnection();
          var pstmt = conn.prepareStatement(query);
          //var pstmt2 = conn.prepareStatement(query2);
          pstmt.execute();
          //pstmt2.execute();
          conn.commit();
          conn.close();
  
  
  
  
      } catch (e) {
          $.response.status = $.net.http.INTERNAL_SERVER_ERROR;
          $.response.setBody(e.message);
          return;
      }
  
      $.response.setBody(body);
      //$.response.contentType = 'application/vnd.ms-excel; charset=utf-16le';
      $.response.contentType = 'application/vnd.ms-excel; charset=utf-8';
      $.response.headers.set('Content-Disposition',
              'attachment; filename=Excel.txt');
      $.response.headers.set('access-control-allow-origin', '*');
      $.response.status = $.net.http.OK;
  
  }
  </code></pre>
  
  <p>I am totally new to sapui5 and tried everything I could find online, but I get the error when retrieving the selected value just before I call ExcelQuery.xsjs:</p>
  
  <p><a href="http://server-address/I313766_Hubert/perslist/services/ExcelQuery.xsjs?cmd=DeleteRequest&amp;idrequest=undefined" rel="nofollow noreferrer">http://server-address/I313766_Hubert/perslist/services/ExcelQuery.xsjs?cmd=DeleteRequest&amp;idrequest=undefined</a> Failed to load resource: the server responded with a status of 500 (Internal Server Error)
  sap-ui-core.js:27 GET <a href="http://server-address/I313766_Hubert/perslist/services/ExcelQuery.xsjs?cmd=DeleteRequest&amp;idrequest=undefined" rel="nofollow noreferrer">http://server-address/I313766_Hubert/perslist/services/ExcelQuery.xsjs?cmd=DeleteRequest&amp;idrequest=undefined</a> 500 (Internal Server Error)</p>
  
  <p>The above snippet is from the chrome debugger. Could you please kindly help? I did some debugging myself and found out that the value selected from the table seems to be undefined, unless I refer to it the wrong way. I spent about four days trying to fix it by now, also tried all of the tuts I could find online - none of them work because either I can't retrieve the model or the value to be deleted.</p>
  
  <p>Thanks in advance
  Below I attach a screen shot of the index.html when ran in the browser along with my file structure.<a href="https://i.stack.imgur.com/Rbp2s.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rbp2s.jpg" alt="enter image description here"></a></p>