ERPClimb logoERPClimb

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

  1. 1View with parameterZI_LeaveByYear
  2. 2Call from ABAPABAP SQL

The result

A reusable view that returns the requests of one year with readable status text.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

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

  1. 1A parameter instead of a hard-coded yearOne view replaces a view per year, and the caller decides.
  2. 2CASE in the viewThe mapping from code to text lives in one place. If a status is added later, one view changes.
  3. 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.

CDS · Data definition · ZI_LeaveByYear
// 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)
  1. 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.
  2. Plain fields. The key and the fields we show as they are.
  3. 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.
  4. 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.

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.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP CDS view entities, ABAP Cloud and S/4HANA 2020+; check on your release.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.