cancel
Showing results for 
Search instead for 
Did you mean: 

UI5 multiple filter parameters with AND operator with v2.odata model

0 Kudos

Hi All,

I want to pass multiple filter parameters with AND operator. for requirement I need to construct my Odata query like below

$filter: EntityType eq 'Duplicate' and Attribute eq 'Name' and CharacteristicValue eq 'Abc' and Attribute eq 'Country' and CharacteristicValue eq 'US'

This is hapening when IU manaully construct the above , but if I want to pass as a filters parameter in Odata call(v2.ODataModel) its constructing in a different way like below

$filter: EntityType eq 'dupchk' and ((Attribute eq 'NameOrg1' and CharacteristicValue eq 'ABCD ') or (Attribute eq 'NameOrg2' and CharacteristicValue eq 'ABCD '))

An OR operator between the filter values( highlighted in BOLD) is miss leading my odata.

Below the implementation.

		_oFilter1: function() {
		return new sap.ui.model.Filter("EntityType", sap.ui.model.FilterOperator.EQ,                  "dupchk");
		},
		_oFilter2: function() {
			var oDataModel = this.getView().getModel("dataModel");
			var vNameorg1 = oDataModel.getProperty('/NameOrg1');
			var oFilter2;

		oFilter2 = new sap.ui.model.Filter({
		filters: [new sap.ui.model.Filter("Attribute", sap.ui.model.FilterOperator.EQ, "NameOrg1"),
					new sap.ui.model.Filter("CharacteristicValue", sap.ui.model.FilterOperator.EQ, vNameorg1)
				], 
				and:true
			});
			return oFilter2;
		},
		_oFilter3: function() {
			var oDataModel = this.getView().getModel("dataModel");
			var vNameorg2 = oDataModel.getProperty('/NameOrg2');
			var oFilter3;
			oFilter3 = new sap.ui.model.Filter({
				filters: [
					new sap.ui.model.Filter("Attribute", sap.ui.model.FilterOperator.EQ, "NameOrg2"),
					new sap.ui.model.Filter("CharacteristicValue", sap.ui.model.FilterOperator.EQ, vNameorg2)
				],
				and : true
			});
			return oFilter3;
		},
                this.oModelCustomer.read(oModelObjEntity, {	
		filters: [that._oFilter1(), that._oFilter2(), that._oFilter3()],
		and : true,
		success: function(data, response) {
		sap.ui.core.BusyIndicator.hide();	
  		},
		error: function(oError) {
         	sap.ui.core.BusyIndicator.hide();							}
	        });
0 Kudos

I am expecting an AND operator so all the parameters passed from UI should be considered by ODATA, but a OR operator between the condition is miss leading query, with V2.Odata filter values must be passed via filters parameter in Odata call

Thanks

Rajesh

Accepted Solutions (0)

Answers (2)

Answers (2)

0 Kudos

Hi Vaibhav,

I tried even that way before, which is throwing me "Cannot read property 'sPath' of undefined" and I dont see reason for it.

I am wondering is there any way to pass filter query directly with entityType in v2.oDataModel call ?

				this.oModelCustomer.read(oModelObjEntity, {
					success: function(data, response) {
						sap.ui.core.BusyIndicator.hide();						
					},
					error: function(oError) {
						sap.ui.core.BusyIndicator.hide();						
					}
				});
oModelObjEntity = "/dupchkSet?$filter=EntityType eq 'dupchk' and Attribute eq 'NameOrg1' and CharacteristicValue eq 'ABCD'"

But I can't do with v2.odata, if at least I can pass my filter query like that would slove my issues

Thanks

Rajesh

mvaibhav
Contributor
0 Kudos

Hi Rajesh,

Create your filter like this before passing to the read request :

var aFinalFilter = new sap.ui.model.Filter({ filters: [that._oFilter1(), that._oFilter2(), that._oFilter3()], 
and:true
});

And then pass the aFinalFilter to read

this.oModelCustomer.read(oModelObjEntity,{filters:aFinalFilter ,
		success:function(data, response){sap.ui.core.BusyIndicator.hide();},
		error:function(oError){sap.ui.core.BusyIndicator.hide();}});

Thanks,

Vaibhav

nabheetscn
Active Contributor
0 Kudos

Rajesh i was wondering logically it should be OR not AND. You want to fetch data kind of for attribute and char value, with AND you will can logically have one combination, OR is correct. What is the output you are expecting from Odata filter cal?

Nabheet

0 Kudos

hi Vaibhav, I have written code somewhat similar to your suggestion. I can see that Odata is now getting called when I put external debugger called however I dont see filter parameters in my DPC ext class method.

Please advise what am I missing here.

		// Get Date Range from Selection Screen 
		handleChange: function (oEvent) {


			var sValue = oEvent.getParameter("value");


			var filter2 = new sap.ui.model.Filter({
				path: "/EntitySet",
				operator: sap.ui.model.FilterOperator.EQ,
				value1: sValue
			});


			this.getView().getModel().read("/EntitySet", {
				filters: [filter2],
				success: function (oData) {},
				error: function (oError) {}
			}); 
		debugger;


		}