Skip to main content

Module 11: CRUD Operations in SAPUI5

CRUD (Create, Read, Update, Delete) operations define how a UI5 application interacts with backend business data via OData services.

This module explains how CRUD really works in UI5, including batching and submitChanges, which are critical for performance and data consistency.


1. CRUD in SAPUI5 – Big Picture​

UI Control
↓ (Data Binding / Events)
ODataModel
↓ (HTTP)
OData Service (SEGW / RAP)
Core Principle

UI5 does not call REST APIs manually — it delegates CRUD to ODataModel.


2. Read Operations​

2.1 Read EntitySet (Automatic via Binding)​

<Table items="{/SalesOrderSet}">
<columns>
<Column>
<Text text="Order" />
</Column>
</columns>
<items>
<ColumnListItem>
<cells>
<Text text="{SalesOrderID}" />
</cells>
</ColumnListItem>
</items>
</Table>
Best Practice

Prefer binding-based reads over manual read() calls.

2.2 Read Entity Programmatically​

this.getView().getModel().read("/SalesOrderSet('50000001')", {
success: function (oData) {
// Handle success
},
error: function () {
sap.m.MessageBox.error("Read failed");
}
});

3. Create Entity​

3.1 Create Using ODataModel​

var oEntry = {
SalesOrderID: "50000010",
Customer: "C1001",
Amount: 1500
};

this.getView().getModel().create("/SalesOrderSet", oEntry, {
success: function () {
sap.m.MessageToast.show("Created successfully");
},
error: function () {
sap.m.MessageBox.error("Create failed");
}
});

3.2 Create via Two-Way Binding (Preferred)​

<Input value="{/Customer}" />
this.getView().getModel().submitChanges();
Modern Pattern

Use two-way binding + submitChanges for transactional scenarios.


4. Update Entity​

var oContext = oEvent.getSource().getBindingContext();
oContext.getModel().submitChanges();

UI5 automatically detects changed properties.

4.2 Explicit Update Call (Rare)​

this.getView().getModel().update(
"/SalesOrderSet('50000001')",
{ Amount: 2000 }
);
Recommendation

Avoid explicit update() when binding already tracks changes.


5. Delete Entity​

Delete Using Binding Context​

var oContext = oEvent.getSource().getBindingContext();
oContext.getModel().remove(oContext.getPath(), {
success: function () {
sap.m.MessageToast.show("Deleted");
}
});
UX Rule

Always ask for confirmation before delete.


6. Batch Requests (Very Important)​

What is Batch?​

Batching groups multiple OData requests into one HTTP call.

Benefits​

  • Better performance
  • Transactional consistency
  • Reduced network traffic

Enabling Batch (manifest.json)​

"settings": {
"useBatch": true
}
Default

Batch is enabled by default in most UI5 apps.


7. submitChanges()​

What is submitChanges()?​

submitChanges():

  • Sends all pending changes
  • Executes them as a batch
  • Commits data on backend
this.getView().getModel().submitChanges({
success: function () {
sap.m.MessageToast.show("Changes saved");
},
error: function () {
sap.m.MessageBox.error("Save failed");
}
});

Typical Usage Flow​

User edits fields
↓
Model holds changes
↓
submitChanges()
↓
Backend commit
Interview Gold

Say: "UI5 tracks changes automatically and submitChanges sends them in batch."


8. Error Handling in CRUD​

Handling Batch Errors​

error: function (oError) {
sap.m.MessageBox.error("Operation failed");
}

Partial Success Scenario​

  • Some operations succeed
  • Some fail
  • Must parse response carefully
Real Projects

Batch responses can contain multiple messages.


9. CRUD with OData V4 (RAP – Intro)​

AspectOData V2OData V4
submitChangesYesNo
UpdatePATCHPATCH
DraftManualNative
BindingSimilarSimilar
Note

OData V4 CRUD is handled differently and covered in RAP modules.


10. Common CRUD Mistakes​

Avoid These
  • Calling create/update for each field
  • Disabling batch mode
  • Ignoring submitChanges
  • No error handling
  • Mixing JSONModel with ODataModel

11. Interview-Grade Explanation​

Q: How does CRUD work in SAPUI5?

Answer:

SAPUI5 performs CRUD operations through the ODataModel. Reads are usually triggered automatically via data binding, while create, update, and delete operations are tracked by the model and sent to the backend in batch using submitChanges for performance and transactional consistency.


12. Summary​

  • CRUD is handled by ODataModel
  • Reads are often automatic via binding
  • Create, update, delete are tracked
  • Batch improves performance
  • submitChanges commits data
  • Error handling is mandatory

13. What's Next?​

➡️ Module 12: Tables & Lists

Learning Tip

If you misuse CRUD, apps become slow — batching saves you.