Skip to content

Instantly share code, notes, and snippets.

@Maverick-I0
Last active May 18, 2025 07:35
Show Gist options
  • Select an option

  • Save Maverick-I0/2182eb0403cf6c89252a6ccc5be81e83 to your computer and use it in GitHub Desktop.

Select an option

Save Maverick-I0/2182eb0403cf6c89252a6ccc5be81e83 to your computer and use it in GitHub Desktop.
A simple odata v4 CRUD operations in UI5 development
try {
this.getView().setBusy(true);
let oModel = this.getOwnerComponent().getModel("oDataV4Model");
// data is an object containing the properties for the new entry
const data = {key_1: "value_1", property_1: "value_2"};
const oModelListBinding = oModel.bindList("/entity");
// Create:
oModelListBinding.create({
...data,
});
// Read:
oModelListBinding.requestContexts().then(function(contexts) {
if (contexts.length === 0) {
return;
}
// Map each context to its underlying JavaScript object, filtering out any null values
return contexts.map((context) => context?.getObject()).filter(Boolean);
}.bind(this)).catch(function(err) {
throw err;
});
// Update:
const key = 'key_1';
const keyFilter = new sap.ui.model.Filter("Key", sap.ui.model.FilterOperator.EQ, key);
oModelListBinding.filter(keyFilter).requestContexts().then(function(contexts) {
if (contexts.length === 0) {
return;
}
const contextToUpdate = contexts[0];
// Set the 'property_1' of the found entity to 'updated_value'
contextToUpdate.setProperty("property_1", "updated_value");
return;
}.bind(this)).catch(function(err) {
throw err;
});
// Delete:
const key = 'key_2';
const keyFilter = new sap.ui.model.Filter("Key", sap.ui.model.FilterOperator.EQ, key);
oModelListBinding.filter(keyFilter).requestContexts().then(function(contexts) {
if (contexts.length === 0) {
return;
}
const contextToDelete = contexts[0];
// Mark the found entity for deletion
contextToDelete.delete();
return;
}.bind(this)).catch(function(err) {
throw err;
});
this.getView().setBusy(false);
} catch (err) {
console.error(`[ERROR] Error when running CRUD operations on odata.`, err);
throw err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment