Skip to Content

Invoking oData with multiple filters from SAPUI5

Hello All,

I want to execute an oData Get service with filter from SAPUI5. The oData url would look as follows:

/sap/opu/odata/MAT_SRV/Get_MATSet?$filter=(Plant eq 'A1HG') and (MaterialType eq 'PS01') and ((MaterialNumber eq '61345280') or (MaterialNumber eq '61345280'))

Here, I have created the filter array for oData service (oDataFilter in the example below) as follows:

var oPlant = new sap.ui.model.Filter("Plant", sap.ui.model.FilterOperator.EQ, 'A1HG');

var oPlantFilter = new sap.ui.model.Filter({ filters: [oPlant], and: true });

var oMatType = new sap.ui.model.Filter("MaterialType", sap.ui.model.FilterOperator.EQ, 'PS01');

var oMatTypeFilter = new sap.ui.model.Filter({ filters: [oMatType], and: true });

var oMaterialNumberFilter = this._getMatNumbers();//GET ALL MATERIAL NUMBER FILTERS SEPERATED By OR

var oDataFilter = new sap.ui.model.Filter({ filters: [oPlantFilter, oMatTypeFilter, oMaterialNumberFilter], and: true });//ADDING AND TO ALL FILTERS

oBackEndModel.read( "/Get_MATSet", {

filters: oDataFilter,

success: jQuery.proxy(this.fSuccess, this),

error: jQuery.proxy(this.fError, this) });

_getMatNumbers: function() {

var aTokens = this.byId("Mat_MultiInput").getTokens();//ITERATE THROUGH EACH VALUES ENTERED BY USER

var aMatFilter = [];

for (var i in aTokens) {

aMatFilter.push(new sap.ui.model.Filter("MaterialNumber", sap.ui.model.FilterOperator.EQ, aTokens[i].getKey())); }

var oFilter = new sap.ui.model.Filter({ filters: aMatFilter, and: false });

return oFilter;

}

However, I got the following error while execution: "Cannot Read property sPath of undefined".

Can someone please tell me what I am doing wrong here? Or guide with the filter expression to be passed to the oData service which corresponds to the filter values mentioned in the oData url?

Regards,

Faddy

Add comment
10|10000 characters needed characters exceeded

  • Can anyone please help me here?

    I have tried to debug it using Chrome. What I can see is: the following filter is causing the error

    var oDataFilter = new sap.ui.model.Filter({ filters: [oPlantFilter, oMatTypeFilter, oMaterialNumberFilter], and: true });//ADDING AND TO ALL FILTERS

    Here, I don't have an sPath defined, hence it complains about it.

    However, i have used this filter to combine all the other filter conditions with "and" operator, so I don't have an sPath or I don't know what sPath to be mentioned here.

    Regards,

    Faddy

  • Get RSS Feed

2 Answers

  • Best Answer
    Oct 14, 2017 at 12:42 PM

    Hello,

    Instead of

    var oFilter =newsap.ui.model.Filter({filters:[orFilter, oPlant, oMatType],and:true});

    Use

    var oFilter =new Array(newsap.ui.model.Filter({filters:[orFilter, oPlant, oMatType],and:true}));

    Regards,

    FH

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 27, 2017 at 08:37 AM

    Hi,

    I think how the filters are set up there is an issue, try this :

    var oPlant = new sap.ui.model.Filter("Plant", sap.ui.model.FilterOperator.EQ, 'A1HG');
    var oMatType = new sap.ui.model.Filter("MaterialType", sap.ui.model.FilterOperator.EQ, 'PS01');
    // here lets consider two mat numbers 
    var matNum1 = new sap.ui.model.Filter("MaterialNumber", sap.ui.model.FilterOperator.EQ, '61345280');
    var matNum2 = new sap.ui.model.Filter("MaterialNumber", sap.ui.model.FilterOperator.EQ, '61345280');
    // adding or filter for all the material numbers. 
    var orFilter = new sap.ui.model.Filter({filters:[matNum1, matNum2], and:false});
    var oFilter = new sap.ui.model.Filter({filters:[orFilter, oPlant, oMatType], and:true});
    

    you don't need to enable 'and' operator for each filter.

    Hope this works.

    regards

    GB

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Vaibhav,

      Thanks for your response and sorry for my late reply.

      I have tried the code snippet you have provided me. In the chrome debugger, value for oFilter (which you have mentioned in your response) looked like this:

      I have used this oFilter variable to invoke the oData as follows:

      oBackEndModel.read( "/Get_MATSet", {
      filters: oFilter,
      success: jQuery.proxy(this.fSuccess, this),
      error: jQuery.proxy(this.fError, this) });
      

      However I got the error: Cannot read property 'sPath' of undefined during execution.

      You can see that the call is not reaching the oData service in backend due to this error. Can you please tell me what is wrong here?

      Regards,

      Faddy

      img1.png (56.9 kB)
      img2.png (61.4 kB)