简体   繁体   中英

SAP UI5 oData2 Model use of read method on non-key field error

I am trying to read data from Employee table using SAP UI5 and oData 2 Model. I am using Read method of oData model with primary key and it is working well but when I am trying to use with non key field then it is getting error with message

No key property 'Employee_Name' exists

.

 oModel.read("/Employee_Service(Employee_ID=2)", { success: function(oData, response) { alert("Success!"); }, error: function(response) { alert("Error"); } 

Here Employee_ID is primary key and this code is running well.

oModel.read("/Employee_Service(Employee_Name='Bob')", {
success: function(oData, response) {
alert("Success!");
},
error: function(response) {
alert("Error");
}

This code is getting error as said earlier.

Can you please help me to solve the issue?

Only properties which are assigned as Primary key can be accessed in this way:

Employee_Service(Employee_ID=2)

To access records with other properties you can use $filter query option.

The $filter system query option allows clients to filter a collection of resources that are addressed by a request URL. The expression specified with $filter is evaluated for each resource in the collection, and only items where the expression evaluates to true are included in the response.

So your URL will be like this:

"/Employee_Service?$filter=Employee_Name eq 'Bob'"

I found an article which helps me a lot to solve the issue. Which I was trying this should work but I do not know why it is getting error. However if any one can help me to find answer of my first approach (as above) always welcome. Here is the solution which I did.

var la_filters = new Array();
var lv_Filter = new sap.ui.model.Filter({
path: "BOWTIE_CLASS",
operator: sap.ui.model.FilterOperator.EQ,
value1: bowtieClass
});

la_filters.push(lv_Filter);

oModel.read("/Employee_Service", {
filters: la_filters,
success: function(oData, response) {
alert("Success");
},
error: function(response) {
alert("Error");

}

Hint URL I found here https://scn.sap.com/thread/3781251

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM