Code examples · CDS views · Beginner
CDS view entity with an input parameter and a CASE expression
The problem
HR wants a list of leave requests for one chosen year, with the status shown as words (New, Approved, Rejected) instead of N, A and R.
How we solve it
- 1View with parameterZI_LeaveByYear
- 2Call from ABAPABAP SQL
The result
A reusable view that returns the requests of one year with readable status text.
1. The requirement
HR wants a list of leave requests for one chosen year, with the status shown as words (New, Approved, Rejected) instead of N, A and R. The year must be chosen by the caller, not fixed inside the view.
2. How we design it
Put the changing part (the year) into a view parameter, so one view serves every year. Do the code-to-text translation inside the view with CASE, so every report that uses the view shows the same wording. Consumers hand in the parameter when they select.
Why we decided it this way
- 1A parameter instead of a hard-coded yearOne view replaces a view per year, and the caller decides.
- 2CASE in the viewThe mapping from code to text lives in one place. If a status is added later, one view changes.
- 3An explicit ELSE branchUnknown codes show as 'Unknown' instead of silently becoming empty.
Before you start
- The table ZLEAVE_REQ from the managed RAP example (or any table with a date and a status column).
3. The code, step by step
Step 1 of 2: Define the view
A view that needs a year, filters on it and translates the status.
// Declare the view and its parameter
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Leave requests of one year'
define view entity ZI_LeaveByYear
with parameters
p_year : abap.numc( 4 )
as select from zleave_req
{
// Plain fields
key leave_uuid as LeaveUuid,
employee_id as EmployeeId,
start_date as StartDate,
status as Status,
// CASE: status code to text
case status
when 'N' then 'New'
when 'A' then 'Approved'
when 'R' then 'Rejected'
else 'Unknown'
end as StatusText
}
// Filter with the parameter
where substring( start_date, 1, 4 ) = $parameters.p_year
Part by part (plain text)
- Declare the view and its parameter. 'with parameters' adds an input to the view. The parameter has a name and a type; here a four-digit year. Inside the view it is written $parameters.p_year.
- Plain fields. The key and the fields we show as they are.
- CASE: status code to text. A CASE expression compares the status with each WHEN value from top to bottom and returns the first match. ELSE covers all other values. The result is a text column named StatusText.
- Filter with the parameter. The WHERE clause keeps only requests that start in the chosen year. substring( start_date, 1, 4 ) takes the first four characters of the date (YYYYMMDD), the year.
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
A reusable view that returns the requests of one year with readable status text.
What you should see
SELECT with p_year = '2027' returns only requests that start in 2027. A request with status A shows StatusText 'Approved', N shows 'New', R shows 'Rejected', anything else 'Unknown'.
Try it and check the result
- Create two requests through the RAP example, one starting in 2027 and one in 2028.
- Run the class. Only the 2027 request is printed, with its status as text.
- Change p_year to '2028' and run again: now only the other request appears.
Common mistakes
- A parameter is mandatory for every consumer. Selecting from the view without it is a syntax error.
- CASE returns one data type. Literal branches of different length give a result as long as the longest literal.
- Do not use CASE for large mapping tables. Use a text association to a dictionary or domain-based text when the list is long.