Skip to Content
Author's profile photo Former Member

CRUD Operations using Odata Model (XSODATA) in SAP UI5

As CRUD operations are possible in SAP UI5 using XSJS services (SAP HANA as database) but as per new versions of Odata Model these operations are possible through ODATA. It reduces considerable lines of code and makes SAP UI5 more friendly for developers.

Following artifacts are used in this Document-

  • A SAP HANA Table – “<Schema Name>”.”DATA” which contains three columns- NAME (NVARCHAR), CITY(NVARCHAR), PHONE(INTEGER)
  • A Odata service is created DATA.xsodata
  • A Demo SAP UI5 Application is created having Table contents in home Screen and having separate buttons for “ADD, “UPDATE”, “DELETE” operations.
  • “ADD” & “UPDATE” Buttons opens dialogs having three Input Fields for each column of Table

Let us have a look on SAP UI5 Application used in this Demo. Please note that coding part of this application is not discussed here, As this article is focussed only on CRUD operations:

Home Screen of Application

/wp-content/uploads/2016/08/pic_1015770.jpg

Odata Service

service {

“SCHEMA”.”DATA” as “DATA”;

}

Odata READ Operation:

It is simplest operation and already widely used in development. For this, a OdataModel is to be instantiated in Application. Then this Model is to be bind to UI Element, here, Table

var oModel = new sap.ui. model. odata. ODataModel(“DATA.xsodata”);

  1. sap.ui. getCore (). setModel(oModel);

Note: Service Path may be different with respect to your package Structure.


/wp-content/uploads/2016/08/pic_1015770.jpg

This Odata service will load complete data of HANA Table in UI5 Application.

Odata CREATE Operation:

Once user click on “ADD” button on Home Screen, a Dialog will open for New Entry creation in table


/wp-content/uploads/2016/08/pic_1015770.jpg

Once user Clicks on “Add” in Dialog, it will trigger a function in Controller-


/wp-content/uploads/2016/08/pic_1015770.jpg

  • mNewEntry is a Javascript Object. It is used as oModel.create() method can only accepts String Data
  • I1, I2, I3 are IDs of Input Fields in Dialog.

Odata UPDATE Operation:

For this, Use has to select a row to be updated. The values of that row will appear in UPDATE Dialog in editable input fields.


/wp-content/uploads/2016/08/pic_1015770.jpg

Once user changes values, new values will be updated in HANA Table on clicking “Update” icon, which will call a function as –

/wp-content/uploads/2016/08/pic_1015770.jpg

Function(odata){} will be be called if Update is successful otherwise, Function(err) will be called. You can show any message in these function (Message Box usage is not mandatory).

Both Success & Error functions are optional to be defined.


Odata DELETE Operation:

For this operation, more than 1 row can be selected simultaneously to be deleted. Once those rows are selected “DELETE” button will trigger a function as-

/wp-content/uploads/2016/08/pic_1015770.jpg

aBatchtoDelete will hold ‘sPath’ of rows to be deleted. createBatchOperations are performed to Create/Delete Multiple records simultaneously. For more properties, please refer API Reference.

_____________________________________

Assigned tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Ashwin Narayan
      Ashwin Narayan

      Hi Ankit,

      Very well explained. Appreciate your efforts.

       

      Thanks,

      Ashwin

      Author's profile photo nibedita sahoo
      nibedita sahoo

      Nice blog .Very informative..

      Author's profile photo Ahmed khan
      Ahmed khan

      This blog is really awesome, can you please share the project to git git hub so that we will get more insights

      Author's profile photo Ahmed khan
      Ahmed khan

      Can someone please share the full code for this app at ahmedkan29789@gmail.com

      Author's profile photo Žygimantas Krasauskas
      Žygimantas Krasauskas

      When using Odata CREATE Operation I am getting this error: {"error":{"code":500,"message":{"lang":"en-US","value":"Error while executing a DB query"}}} is there a way to see what causes this error?

      Author's profile photo Shivam Bedwal
      Shivam Bedwal

      Hello Žygimantas ,

      Did you find a solution to your problem? I am also facing the same issue.

      Author's profile photo Rakan Harb
      Rakan Harb

      Hey guys, have you already found an answer? We're still facing the same issue.

       

      Error: {"error":{"code":500,"message":{"lang":"en-US","value":"Error while executing a DB query"}}}

       

      Request (JS):

      var entity = "/firma";
      var data = {
      	FirmaName: "hallo",
      	Bezeichnung: "halalo2e12"
      };
      FirmaModel.create(entity, 
      	data,
      	{
      	success: function(oData) {
      			console.dir(oData);
      		}, 
      		error: function (jqXHR) {
      			console.log("Fehler:Firma_Create" + jqXHR);
      		}
      	}
      );

      Service-Definition:

      service{
          "Orga.firma" as "firma";
      }
      

       

      As we see, the blog-posting above does not contain a description of the service itself. It might be the case that is the problem here.

      Thanks in advance for your support.

      Kind regards

       

      Rakan