In SAP ABAP, Core Data Services (CDS) views provide a powerful way to model and consume data. However, one of the common requirements is to filter data dynamically based on user input. This capability allows users to view tailored data without requiring multiple static views. In this blog, we’ll explore how to achieve dynamic data filtering in CDS views based on user input and provide a complete coding example.
Why Use Dynamic Data Filtering?
Dynamic data filtering enables:
- Flexibility: Users can filter the results based on the criteria they choose at runtime.
- Performance: Only the relevant data is fetched, which reduces unnecessary data transfer and enhances performance.
- Interactivity: Applications become more user-centric by allowing dynamic input for filtering.
What We’ll Build:
We’ll create a CDS view that filters customer data based on user-provided input for:
- Customer ID
- Country
We’ll then expose this CDS view for consumption and demonstrate how to pass dynamic filter values through the view.
Step 1: Define the CDS View with Input Parameters
Here’s how you can define a CDS view with input parameters for dynamic filtering:
Explanation:
- Parameters (p_cust_id and p_Country): These input parameters allow users to specify the customer ID and country for filtering.
- WHERE Clause: The filtering is done dynamically based on the input. If a parameter is not provided (i.e., is null), that condition is ignored.
- @odata.publish: true: This exposes the CDS view as an OData service, allowing it to be consumed via SAP Fiori or other front-end tools.
Step 2: Testing the CDS View with Dynamic Inputs
To test the CDS view, you can use SAP GUI or any OData client to pass the input parameters dynamically. Below is how you can do this:
- p_custid and p_cntry: These are dynamic inputs provided at runtime.
- lt_results: The filtered results based on user input are stored here.
Once the CDS view is exposed as an OData service, you can query it from a browser or postman using the following URL:
/sap/opu/odata/SAP/ZRH_CUSTOMER_SRV_01/ZRH_CUSTOMER_FILTERSet(Custid='0001000005',Country='IN')
- Custid ='0001000005' filters by the customer ID.
- Country ='IN' filters by the country.
Step 3: Sample Output Based on Dynamic Input
Case 1: Input - Customer ID and Country Provided
In this case, only the customer with Custid = '0001000005' and Country = 'IN' is returned.
Case 2: Input - Only Country Provided
Here, the filter is applied only for the Country = 'US', and all customers in the US are returned.
Case 3: Input - No Input Provided
When no input is provided, all customers are returned as the filter conditions are ignored.
Conclusion
Dynamic data filtering in CDS views based on user input allows you to create flexible, interactive applications that provide users with exactly the data they need. By using input parameters in your CDS views, you can build powerful, performance-optimized filters that adjust based on runtime inputs. Whether you're building an SAP Fiori application or an OData service, this technique enhances both performance and user experience.