Rule Definition
To use the key generation has those advantages:
The key gets formatted automatically. The key gets formatted into the right format of the primary key specified in the OData service. For example, if the key is stated as 0 but in the OData service, the key is defined as a Boolean, then the 0 gets automatically formatted to false.
If there is more than one key, then the keys get aligned in the correct order. For example, there is a PrimaryKey1 and a PrimaryKey2. If those get mixed up in the wrong order in a path as a String as in the first code sample, then the read request fails. But the key generation takes care of the right order. If the order of the keys is mixed up in the key generation as in the second code sample, then the key generation sorts it out. The key gets aligned in the right order, and the read request succeeds.
Remediation
Use the key generation.
Violation Code Sample
// controller
// without key generation
// get model
var oModel = this.getView().getModel();
// set path with primary keys in a String
var sPath = "/Products(ProductID1='foo',ProductID2='bar')";
// read the product with the primary keys foo and bar
oModel.read(sPath, {
success: function(oData) { sap.base.Log.info(oData); },
error: function(oData) { sap.base.Log.info(oData); }
});
Fixed Code Sample
// controller
// with key generation
// get model
var oModel = this.getView().getModel();
// create primary keys with key generation
var sPath = oModel.createKey("/Products", {
ProductID1: "foo",
ProductID2: "bar"
});
// read the product with the primary keys foo and bar
oModel.read(sPath, {
success: function(oData) { sap.base.Log.info(oData); },
error: function(oData) { sap.base.Log.info(oData); }
});
Reference
https://inui.io/sapui5-best-practices/#1-create-keys-for-crud-statements
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.