Preface – This post is part of the SAP ABAP OData Tutorial series.
In this section, we will learn the concept of the $filter query in SAP OData and its usage.
Table of Contents
Supported System Version
SAP NetWeaver Gateway Release 2.0 Support Package >=03
Syntax
http://<server>:<port>/sap/opu/odata/sap/<service_name>/EntitySet?$filter=ProductId eq ‘HP-101’
Business Example
The shopping site, say, Amazon is displaying all the products but you want some specific category to be displayed. So, you will apply filters (select category phones or laptops) to display the data of your choice.
Implementation
In this section, we will implement $filter on our Product Entity Set.
Step 1: After the creation of the OData service, right Click on Entity Set under Service Implementation, go to ABAP workbench.
Step 2:
Adjust your code for $filter query.
The filter query option is available by accessing the parameter IT_FILTER_SELECT_OPTIONS.
In the above code, we first retrieved the filter options passed from the frontend by reading IT_FILTER_SELECT_OPTIONS. And then passed the select-option range to BAPI to get the desired filtered output.
Step 3:
If we do not pass any filter option, we will get all the products from the system.
Add $filter query in OData URL and execute the service. You can add one or more filter options.
Service URL: /sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/ProductsSet?$filter=Name eq ‘Notebook Basic 15.
Operators used with $filter
Logical Operators
Logical Operator | Description | Example |
eq | Equal | https://<API-Server>/odata/v2/User?$filter=gender eq ‘F’ . |
ne | Not Equal | hhttps://<API-Server>/odata/v2/User?$filter=place ne ‘’London’ |
gt | Greater than | https://<API-Server>/odata/v2/PicklistLabel?$filter=amount gt 200 |
ge | Greater than or equal | https://<API-Server>/odata/v2/PicklistLabel?$filter=amount ge 200 |
lt | Less than | https://<API-Server>/odata/v2/PicklistLabel?$filter=amount lt 200 |
le | Less than or equal | https://<API-Server>/odata/v2/PicklistLabel?$filter=amount le 200 |
and | Logical and | https://<API-Server>/odata/v2/PicklistLabel?$filter=amount le 200 and amount ge 300 |
or | Logical or | https://<API-Server>/odata/v2/PicklistLabel?$filter=amount le 200 or amount ge 300 |
not | Logical not | https://<API-Server>/odata/v2/PicklistLabel?$filter=amount le 200 not amount ge 300 |
Arithmetic Operators
Arithmetic Operator | Description | Example |
add | Addition | https://<API-Server>/odata/v2/PicklistLabel?$filter=amount add 5 gt 10 |
sub | Subtraction | https://<API-Server>/odata/v2/PicklistLabel?$filter= amount sub 5 gt 10 |
mul | Multiplication | https://<API-Server>/odata/v2/PicklistLabel?$filter= amount mul 5 gt 10 |
div | Division | https://<API-Server>/odata/v2/PicklistLabel?$filter= amount div 5 gt 10 |
mod | Modulus | https://<API-Server>/odata/v2/PicklistLabel?$filter= amount mod 5 gt 10 |
Grouping Operators
Grouping Operator | Description | Example |
( ) | Precedence grouping | https://<API-Server>/odata/v2/PicklistLabel?$filter=(number sub 20) gt 30 |
0 Comments