Search code examples
odatasapui5

UI5 OData Service with batch GET with filters


i have a OData model i need to make batch read request the model is ctreated as below

this.oModel = new sap.ui.model.odata.ODataModel(sURI,{
                 json     : true,
                 user     : "<username>",
                 password : "<password>",
                 useBatch : true
        });

The filter and batch requests are created as below

var allfilters = [new sap.ui.model.Filter({
                path:'filter1',
                operator : sap.ui.model.FilterOperator.EQ,
                value1 : this.filter1value
            }),
            new sap.ui.model.Filter({
                path:'DateField',
                operator : sap.ui.model.FilterOperator.EQ,
                value1 : 'SCHED'
            }),
            new sap.ui.model.Filter({
                path:'StartDate',
                operator : sap.ui.model.FilterOperator.EQ,
                value1 : oDateFormat.format(this.startDate.toDate())
            }),
            new sap.ui.model.Filter({
                path:'EndDate',
                operator : sap.ui.model.FilterOperator.EQ,
                value1 : oDateFormat.format(this.endDate.toDate())
            })];

        var batchrequest  = this.oModel.createBatchOperation('/ReadEntitySet','GET',{
            filters : allfilters
        });
        this.oModel.addBatchReadOperations([batchrequest]);  
        this.oModel.submitBatch(this._gotData.bind(this),function(err){
            console.log(err);
        });

When we debug the ABAP code we are not getting the filters.


Solution

  • filters is not a valid property for the oData parameter of createBatchOperation. You can achieve this either by appending the $filter directly to your path, or you can use v2 ODataModel, an example as follows...

    this.oModel = new sap.ui.model.odata.v2.ODataModel(sURI,{
                 json     : true,
                 user     : "<username>",
                 password : "<password>",
                 useBatch : true
    });
    
    this.oModel.setDeferredGroups(["myDeferredGroup"]);
    
    this.oModel.read("/ReadEntitySet",{
        groupId: "myDeferredGroup",
        filters: allFilters,
        success: this._gotData.bind(this),
        error  : function(err){ 
            console.log(err); 
        }
    });
    
    this.oModel.submitChanges({
        groupId: "myDeferredGroup",
        success: function(oData){
    
        },
        error  : function(err){
        }
    });