Code examples · ABAP RAP · Advanced
RAP custom entity: serve a list from ABAP code with paging and a filter
The problem
A Fiori list report must show rows that are calculated in ABAP, not read from a table.
How we solve it
- 1Custom entitythe fields
- 2Query providerrows, filter, page
- 3ServiceOData V4
- 4Calltop, skip, count
The result
The service returns one page of the list, the total number of rows, and only rows that match the filter.
1. The requirement
A Fiori list report must show rows that are calculated in ABAP, not read from a table. The list must page (top and skip), report the total number of rows and honour a filter on the title, so that the app stays fast with large result sets.
2. How we design it
A CDS custom entity describes the fields of the list but has no database table behind it. Instead it names an ABAP class that implements IF_RAP_QUERY_PROVIDER. The framework calls the method SELECT for every request. The request object says whether data is wanted, which page, which filter and whether the total count is wanted. The provider builds the rows, applies the filter and the page, and hands the result to the response object.
Why we decided it this way
- 1Custom entity instead of a viewThe data does not come from the database, so there is nothing to put in a view. The query provider is the one place that creates the rows.
- 2Apply the filter before the pagingThe total number and the page must refer to the filtered rows. Paging first would give pages of unfiltered data.
- 3Return the count only when askedA count can be expensive. The framework says when it is needed.
- 4Handle 'unlimited' page sizeA caller can ask for all rows. The provider must not turn that into an error or an empty page.
Before you start
- ADT and a package for the custom entity and the class.
- Basics of CDS and OData services (service definition and service binding).
3. The code, step by step
Step 1 of 4: Describe the list
The fields and the class that provides the data.
// Custom entity with QUERY.IMPLEMENTEDBY
@EndUserText.label: 'Demo rows from ABAP code'
@ObjectModel.query.implementedBy: 'ABAP:ZCL_QUERY_DEMOROWS'
define custom entity ZI_DemoRow
{
key RowId : abap.int4;
Title : abap.char(40);
Amount : abap.dec(15,2);
}
Part by part (plain text)
- Custom entity with QUERY.IMPLEMENTEDBY. The annotation ObjectModel.query.implementedBy points to the class that delivers the rows. The key is needed so that the OData service can identify a row. There is no FROM clause because there is no table.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
The service returns one page of the list, the total number of rows, and only rows that match the filter.
What you should see
GET ...?$top=2&$skip=1&$count=true count: 5 rows: RowId 2 "Beta", RowId 3 "Gamma" GET ...?$filter=contains(Title,'a')&$count=true count: 4 (Alpha, Beta, Gamma, Delta)
Try it and check the result
- Activate the custom entity, the class and the service definition, then create and publish an OData V4 service binding. Use the Preview button of the binding or a REST client to call the two URLs.
- Check that the count of the first call is 5 although only 2 rows are returned.
- Call with $top=0&$count=true: the answer contains only the count. This is how a UI asks for the size of a list.
- Add a break point in the class and call again: the request object shows the filter and the paging the framework received.
Common mistakes
- Paging before filtering gives wrong counts and pages that contain unfiltered data. Always filter first.
- A custom entity is not a table: the framework does not sort or filter for you. Everything the client may ask for must be handled in the provider, or removed from the service.
- Loading all rows into memory works for small lists only. For large sources, translate the filter and the paging into the call to the data source.
- Do not log or return data that the user may not see. A custom entity bypasses the access control of CDS, so check authorization in the provider.