Skip to main content

Module 10: OData Model Basics

OData is the primary integration mechanism between SAPUI5 applications and the SAP backend.
Understanding OData models is mandatory for building real-world Fiori applications.

This module explains what OData is, how UI5 consumes it, and how to handle errors properly.


1. What is OData?

Definition

OData (Open Data Protocol) is a REST-based protocol used to:

  • Expose business data
  • Perform CRUD operations
  • Enable standard, URL-based access
SAP Reality

Almost all SAPUI5 applications consume data via OData services.


2. Key Characteristics

OData is:

  • RESTful (HTTP-based)
  • Stateless
  • Supports CRUD
  • Uses standard HTTP verbs
  • Returns JSON or XML

3. Example OData URL

/sap/opu/odata/sap/Z_SALES_SRV/SalesOrderSet

This URL retrieves all sales orders from the Z_SALES_SRV service.

OData Convention

Service URLs follow a consistent naming pattern in SAP systems.


4. OData Versions

VersionStatusUsage
V2LegacyExisting apps
V4ModernNew S/4HANA apps
SAP Direction

Prefer OData V4 for new developments.


5. Creating ODataModel in UI5

Declarative (manifest.json)

"models": {
"": {
"type": "sap.ui.model.odata.v2.ODataModel",
"settings": {
"serviceUrl": "/sap/opu/odata/sap/Z_SALES_SRV/"
}
}
}

Programmatic

var oModel = new sap.ui.model.odata.v2.ODataModel(
"/sap/opu/odata/sap/Z_SALES_SRV/",
{
json: true
}
);
this.getView().setModel(oModel);
Best Practice

Use manifest.json for configuration.


6. Reading Data from OData

Using ODataModel with Binding

<List items="{/SalesOrderSet}">
<StandardListItem
title="{SalesOrderID}"
description="{CustomerName}" />
</List>

UI5 automatically requests data from OData.

Reading Specific Entity

var oModel = this.getView().getModel();
oModel.read("/SalesOrderSet('123')", {
success: function (oData) {
console.log(oData);
},
error: function (oError) {
console.log(oError);
}
});
Best Practice

Use binding for lists, read() for details.


7. Creating Data (POST)

var oNewOrder = {
SalesOrderID: "999",
CustomerName: "Acme Corp"
};

oModel.create("/SalesOrderSet", oNewOrder, {
success: function (oData) {
sap.m.MessageToast.show("Order created");
},
error: function (oError) {
sap.m.MessageBox.error("Creation failed");
}
});
Error Handling

Always provide error callbacks.


8. Updating Data (PUT/PATCH)

oModel.update("/SalesOrderSet('123')", {
CustomerName: "New Name"
}, {
success: function () {
sap.m.MessageToast.show("Updated");
},
error: function (oError) {
sap.m.MessageBox.error("Update failed");
}
});

9. Deleting Data (DELETE)

oModel.remove("/SalesOrderSet('123')", {
success: function () {
sap.m.MessageToast.show("Deleted");
},
error: function (oError) {
sap.m.MessageBox.error("Delete failed");
}
});
Confirmation

Always confirm before deleting.


10. OData vs JSONModel

AspectODataModelJSONModel
Data SourceBackendClient-side
CRUDFull supportManual
PerformanceOptimizedSimple
Use CaseProductionTesting
Enterprise Apps

Production apps use ODataModel.


11. Interview-Grade Explanation

Q: What is OData and why is it important in SAPUI5?

Answer:

OData is a REST-based protocol that enables standard data access from SAP backends. SAPUI5 uses ODataModel to consume OData services declaratively through binding or programmatically through read/create/update/delete methods, making it the primary integration mechanism for Fiori applications.


12. Summary

  • OData is REST-based protocol for data access
  • ODataModel is the UI5 adapter
  • Data binding integrates seamlessly
  • CRUD operations are straightforward
  • Error handling is mandatory
  • V2 is legacy, V4 is modern

13. What's Next?

➡️ Module 11: Lists & Tables

Learning Tip

Mastering OData Model makes you a productive SAPUI5 developer.