Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Ravikumar_H1
Explorer
12,582

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: 

Ravikumar_H_12-1727440234559.png

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: 

Ravikumar_H_13-1727440234566.png

  • 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 

Ravikumar_H_14-1727440234571.png

In this case, only the customer with Custid = '0001000005' and Country = 'IN' is returned. 

Case 2: Input - Only Country Provided 

Ravikumar_H_15-1727440234574.png

 

Ravikumar_H_16-1727440234577.png

 

Ravikumar_H_17-1727440234581.png

Here, the filter is applied only for the Country = 'US', and all customers in the US are returned. 

Case 3: Input - No Input Provided 

Ravikumar_H_18-1727440234585.png

Ravikumar_H_19-1727440234588.png

Ravikumar_H_20-1727440234591.png

Ravikumar_H_21-1727440234594.png

Ravikumar_H_22-1727440234598.png

Ravikumar_H_23-1727440234601.png

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. 

3 Comments