SAP HR Guide: Query Effective-Dated Employee Data

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Checked on 2026-11-13 against CAP Node.js 10.2 (@sap/cds 10.2.2, @sap/cds-dk 10.2.1), OData V4, curl 8.7 and Node.js 24.21.

What you build

This is a teaching model built with CAP. It is not SAP S/4HANA or SAP SuccessFactors and does not reproduce their screens.

You build a small OData V4 service over employee records that are valid for a period, and you ask it two kinds of question. As of a date: which record was effective on 2025-06-30? Over a range: which records were effective at some time between 2026-06-01 and 2026-07-31? The three URL parameters are called asOfDate, fromDate and toDate, as in the SuccessFactors OData API, and they follow the rules that SAP’s reference states for them: a query uses either asOfDate or the pair fromDate and toDate, never both; the dates are written YYYY-MM-DD; and with no parameter the answer is the one of today. The service has no tenant behind it. It rejects a wrong question with HTTP 400 and a message, and for a record that does not exist on the date it answers an empty list.

Browser with the address of the organizational assignments as of 2024-06-01, marked by callout 13: the JSON response holds one record, the one of the employee 00001234 from 2024-03-01 in personnel subarea 0001, because Ben Okoye and Chloe Martin were hired later

Before you start

  • No account is needed: the CAP tools and curl install without signing in, and the service answers on your own computer. Installing @sap/cds-dk means accepting the SAP Developer License Agreement, version 3.2, which the lab README names.

  • Do the guide SAP HR Guide: Model Employee Records with Validity Periods first: this guide uses its model (the same entities and CSV data) and does not explain how a new record closes the old one. Knowing SAP Guide: Your First CAP Service and SAP Guide: Query OData Services helps too: cds init, cds watch, $select, $filter and $orderby are used here without much explanation.

  • Read SuccessFactors OData API (the section on asOfDate, fromDate and toDate), Employee Central Data Models and MDF and Time Constraints and Validity Periods for the ideas that the model copies.

  • You need curl 7.82 or newer, which has the option --json; the steps of this guide only use plain curl -s, so an older curl also works. curl --version prints the version. You also need a shell such as bash or zsh, where single quotes keep the text as it is.

  • Names that come from an SAP page: the parameter names asOfDate, fromDate and toDate, their YYYY-MM-DD format, the rule that a query uses one or the other, and the default of today come from the SAP SuccessFactors OData API reference (the pages Effective Dating and Effective Dated Query in OData, listed under Sources). Names that are the model’s own: the entities and fields of the model, the messages of the service, the rule that fromDate and toDate come as a pair, and the way a range is matched. SAP’s pages that were read describe the parameters for the OData v2 API and do not describe them for the v4 API; this service is OData V4 because that is what CAP serves, so it shows the idea and not the behavior of a particular SuccessFactors service.

  • The example is the one of the HR pages: Example Trading Co., personnel area EX01, and the employees Ana Torres 00001234, Ben Okoye 00001235 and Chloe Martin 00001236. All codes and dates are invented example values, not values delivered by SAP. Dates in the model are written YYYY-MM-DD, as in code; the SAP screens of the reference pages show DD.MM.YYYY, so 01.07.2026 is 2026-07-01. The high date 31.12.9999 is 9999-12-31.

  • The data starts where the first HR guide ended, and extends the example in the same made-up way: Ana moves from personnel subarea 0001 to 0002 on 2026-07-01, so she has two organizational records, and her parking permit A-1 ended on 2025-05-31. Ben and Chloe have one open-ended record each.

  • The figures are drawings of the editor, the terminal and the browser, not screenshots. A highlighted box with a number marks the file, the line or the page part of the step with the same number.

Steps

Create the project and its data

  1. Create a CAP project named people-queries and install it, as in the first guide:

    cds init people-queries --nodejs
    cd people-queries
    npm install
  2. Create db/schema.cds. It is the model of the guide on validity periods: Employees holds one row per personnel number, and each record entity has the employee and the BeginDate as its key and an EndDate that defaults to the high date 9999-12-31. The organizational assignments and the basic pay records follow time constraint 1 (exactly one record is valid at any time); the parking permits follow time constraint 2 (at most one record is valid, gaps are allowed).

    namespace sap.guide.people;
    
    // A teaching model of infotype-style employee records, not SAP S/4HANA and not
    // SAP SuccessFactors. Names, numbers and dates are invented example values.
    entity Employees {
      key PersonnelNumber : String(8);
      Name                : String(40);
      EntryDate           : Date;
      OrgAssignments      : Composition of many OrganizationalAssignments
                              on OrgAssignments.Employee = $self;
      BasicPay            : Composition of many BasicPayRecords
                              on BasicPay.Employee = $self;
      ParkingPermits      : Composition of many ParkingPermits
                              on ParkingPermits.Employee = $self;
    }
    
    // Time constraint 1: exactly one record is valid at any time.
    entity OrganizationalAssignments {
      key Employee      : Association to Employees;
      key BeginDate     : Date;
      EndDate           : Date default '9999-12-31';
      PersonnelArea     : String(4);
      PersonnelSubarea  : String(4);
      EmployeeGroup     : String(1);
      EmployeeSubgroup  : String(2);
      Position          : String(8);
      PayrollArea       : String(2);
    }
    
    // Time constraint 1 as well.
    entity BasicPayRecords {
      key Employee  : Association to Employees;
      key BeginDate : Date;
      EndDate       : Date default '9999-12-31';
      PayBasis      : String(10);
      Amount        : Decimal(9,2);
      Currency      : String(3) default 'EUR';
    }
    
    // Time constraint 2: at most one record is valid at any time; gaps are allowed.
    // A record of the model's own, not an SAP infotype.
    entity ParkingPermits {
      key Employee  : Association to Employees;
      key BeginDate : Date;
      EndDate       : Date default '9999-12-31';
      Permit        : String(10);
    }
    Visual Studio Code with the file db/schema.cds of the people-queries project: an excerpt with the comment Time constraint 1 and the entity OrganizationalAssignments, whose key is the employee and the begin date and which has an end date that defaults to 9999-12-31; callout 2 marks the file in the explorer
  3. Create the four CSV files in db/data. Ana has two organizational records, the second beginning the day after the first ends. Ben and Chloe have one record each, beginning on their hire dates. Ana’s permit A-1 ends on 2025-05-31, so from 2025-06-01 she has no permit.

    PersonnelNumber,Name,EntryDate
    00001234,Ana Torres,2024-03-01
    00001235,Ben Okoye,2024-09-01
    00001236,Chloe Martin,2025-01-15
    Employee_PersonnelNumber,BeginDate,EndDate,PersonnelArea,PersonnelSubarea,EmployeeGroup,EmployeeSubgroup,Position,PayrollArea
    00001234,2024-03-01,2026-06-30,EX01,0001,1,AA,51000001,E1
    00001234,2026-07-01,9999-12-31,EX01,0002,1,AA,51000001,E1
    00001235,2024-09-01,9999-12-31,EX01,0001,1,AA,51000002,E1
    00001236,2025-01-15,9999-12-31,EX01,0002,1,AB,51000003,E1
    Employee_PersonnelNumber,BeginDate,EndDate,PayBasis,Amount,Currency
    00001234,2024-03-01,9999-12-31,Monthly,4000.00,EUR
    00001235,2024-09-01,9999-12-31,Monthly,2400.00,EUR
    00001236,2025-01-15,9999-12-31,Hourly,14.00,EUR
    Employee_PersonnelNumber,BeginDate,EndDate,Permit
    00001234,2025-01-01,2025-05-31,A-1

Write the date parameters

  1. Create srv/people-service.cds. It exposes the four entities as an OData V4 service. The annotation @readonly makes the service answer reads only, because this guide only asks questions.

    using { sap.guide.people as db } from '../db/schema';
    
    // Read-only: the guide only asks questions. The date parameters asOfDate,
    // fromDate and toDate are handled in people-service.js.
    @readonly
    service PeopleService {
      entity Employees                  as projection on db.Employees;
      entity OrganizationalAssignments  as projection on db.OrganizationalAssignments;
      entity BasicPayRecords            as projection on db.BasicPayRecords;
      entity ParkingPermits             as projection on db.ParkingPermits;
    }
  2. Create srv/people-service.js. The before handler for READ runs before every read of the three record entities. It takes asOfDate, fromDate and toDate from the query string of the request. Each value that is given must be a real calendar day written YYYY-MM-DD; asOfDate must not come with fromDate or toDate; fromDate and toDate must come together, and fromDate must not be after toDate. Then the handler adds one condition to the query. A record is effective on a date when BeginDate is on or before it and EndDate is on or after it. For a range, a record is returned when it is effective on at least one day of the range: it begins on or before toDate and ends on or after fromDate. With no parameter the date is today. The parameters have no dollar sign, because OData reserves that for its own system query options and lets a service define others. The figure shows the part of the file that handles the parameters; the lines above it (the date check and today) are in the listing.

    import cds from '@sap/cds'
    
    const DATE = /^\d{4}-\d{2}-\d{2}$/
    
    // A date as YYYY-MM-DD that is a real calendar day (2026-02-30 is not).
    const isDate = (text) =>
      DATE.test(text) && new Date(`${text}T00:00:00Z`).toISOString().startsWith(text)
    
    // Today as YYYY-MM-DD in UTC.
    const today = () => new Date().toISOString().slice(0, 10)
    
    export default class PeopleService extends cds.ApplicationService {
      init() {
        const { OrganizationalAssignments, BasicPayRecords, ParkingPermits } =
          this.entities
        const effectiveDated =
          [OrganizationalAssignments, BasicPayRecords, ParkingPermits]
    
        // The model's own parameters, written like the ones of the SuccessFactors
        // API: asOfDate alone, or fromDate together with toDate, never both. They
        // are not OData system options, so they have no dollar sign.
        this.before('READ', effectiveDated, (req) => {
          if (req.query.SELECT.one) return // a read by key asks for that record
          const { asOfDate, fromDate, toDate } = req.http.req.query
          for (const [name, value] of Object.entries({ asOfDate, fromDate, toDate })) {
            if (value !== undefined && !isDate(value)) {
              return req.error(400, `${name} must be a date written YYYY-MM-DD`)
            }
          }
          if (asOfDate && (fromDate || toDate)) {
            return req.error(400, 'Use asOfDate or fromDate with toDate, not both')
          }
          if (Boolean(fromDate) !== Boolean(toDate)) {
            return req.error(400, 'fromDate and toDate must be used together')
          }
          if (fromDate > toDate) {
            return req.error(400, 'fromDate must not be after toDate')
          }
          // A record is effective on a date when BeginDate <= date <= EndDate.
          if (fromDate) {
            // A range returns every record that is effective on one day or more
            // of it: the record begins on or before toDate and ends on or after
            // fromDate.
            req.query.where({
              BeginDate: { '<=': toDate },
              EndDate: { '>=': fromDate },
            })
          } else {
            const date = asOfDate ?? today() // no parameter means today
            req.query.where({
              BeginDate: { '<=': date },
              EndDate: { '>=': date },
            })
          }
        })
    
        return super.init()
      }
    }
    Visual Studio Code with the file srv/people-service.js: the excerpt reads asOfDate, fromDate and toDate from the query, rejects a value that is not a date written YYYY-MM-DD, rejects asOfDate together with a range, and adds a where condition that keeps the records whose begin date is on or before the date and whose end date is on or after it; callout 5 marks the file in the explorer

Start the service and ask as of a date

  1. In the terminal, in the project folder, start the server. The log shows the four CSV files loaded and PeopleService served at /odata/v4/people with the implementation srv/people-service.js. The figure leaves out the banner lines of the log.

    cds watch
    Terminal after cds watch in the people-queries project: the log lists the model files, loads the four CSV files into an in-memory SQLite database, serves PeopleService at /odata/v4/people with the implementation srv/people-service.js and ends with server listening on http://localhost:4004; callout 6 marks the command
  2. Open a second terminal in the project folder. Ask for the organizational assignments without a date parameter. The address is in single quotes, so that the shell leaves the $ signs alone. The answer holds today’s record of each employee: three records, and Ana’s is the one from 2026-07-01 in personnel subarea 0002. This is the answer of a run on 2026-11-13. The answer also holds the key BeginDate, which the service adds to a $select.

    curl -s 'http://localhost:4004/odata/v4/people/OrganizationalAssignments?$select=Employee_PersonnelNumber,PersonnelSubarea&$orderby=Employee_PersonnelNumber'
  3. Ask as of 2025-06-30 by adding asOfDate. The answer again holds one record per employee, but Ana’s is now the first one, from 2024-03-01 in personnel subarea 0001. On 2025-06-30 she had not moved yet. Ben’s and Chloe’s records are the same as before, because each has only one.

    curl -s 'http://localhost:4004/odata/v4/people/OrganizationalAssignments?asOfDate=2025-06-30&$select=Employee_PersonnelNumber,PersonnelSubarea&$orderby=Employee_PersonnelNumber'
  4. Ask as of 2026-07-01, the day the second record begins. Ana’s answer is the second record, in personnel subarea 0002. The day before, 2026-06-30, belongs to the first record, because the first record ends on that day, and the dates are inclusive. On every day there is exactly one record for her, which is time constraint 1.

    curl -s 'http://localhost:4004/odata/v4/people/OrganizationalAssignments?asOfDate=2026-07-01&$select=Employee_PersonnelNumber,PersonnelSubarea&$orderby=Employee_PersonnelNumber'
    Terminal with three curl commands on the organizational assignments: without a date parameter the answer is the current records, where Ana Torres 00001234 is in personnel subarea 0002 from 2026-07-01 (callout 7); asOfDate=2025-06-30 returns her record from 2024-03-01 in subarea 0001 (callout 8); asOfDate=2026-07-01 returns her record from 2026-07-01 in subarea 0002 (callout 9); Ben Okoye and Chloe Martin have one record each; the long answers wrap onto several lines

Ask over a range and across a gap

  1. Ask for the records of Ana that were effective at some time from 2026-06-01 to 2026-07-31, with fromDate and toDate. %20 is a space and %27 a single quote, because curl refuses an address with a raw space. Both of her records are returned, the first because it ends on 2026-06-30, inside the range, and the second because it begins on 2026-07-01. A range returns every record that touches it, not only the one that is effective at the end.

    curl -s 'http://localhost:4004/odata/v4/people/OrganizationalAssignments?fromDate=2026-06-01&toDate=2026-07-31&$filter=Employee_PersonnelNumber%20eq%20%2700001234%27&$select=BeginDate,EndDate,PersonnelSubarea&$orderby=BeginDate'
  2. Ask for the parking permits as of 2025-05-31, the last day of Ana’s permit. The answer is the permit A-1, which is effective through its end date.

    curl -s 'http://localhost:4004/odata/v4/people/ParkingPermits?asOfDate=2025-05-31'
  3. Ask the same one day later, as of 2025-06-01. The answer is an empty list, "value":[], with HTTP 200. A permit follows time constraint 2, which allows a gap, so no record is effective on that day. An empty list is the answer for a date outside every record, not an error.

    curl -s 'http://localhost:4004/odata/v4/people/ParkingPermits?asOfDate=2025-06-01'
    Terminal with three curl commands: the range from 2026-06-01 to 2026-07-31 for Ana Torres returns both of her records, the one from 2024-03-01 to 2026-06-30 in subarea 0001 and the one from 2026-07-01 to 9999-12-31 in subarea 0002 (callout 10); asOfDate=2025-05-31 on the parking permits returns the permit A-1 (callout 11); asOfDate=2025-06-01 returns an empty value list, the gap after the permit (callout 12); the long answers wrap onto several lines
  4. Ask who was employed on 2024-06-01. Open http://localhost:4004/odata/v4/people/OrganizationalAssignments?asOfDate=2024-06-01&$select=PersonnelSubarea in the browser. Only Ana’s record comes back, from 2024-03-01: Ben was hired on 2024-09-01 and Chloe on 2025-01-15, so on the asked date they have no record yet. The figure at the top of the page shows this answer.

See what the service rejects

  1. Send asOfDate together with a range. The service answers HTTP 400 and the message Use asOfDate or fromDate with toDate, not both. -w makes curl print the status after the answer.

    curl -s -w '\nHTTP %{http_code}\n' 'http://localhost:4004/odata/v4/people/OrganizationalAssignments?asOfDate=2025-06-30&fromDate=2025-01-01&toDate=2025-12-31'
  2. Send a date written the SAP screen way, 30.06.2025. The service answers HTTP 400 and asOfDate must be a date written YYYY-MM-DD. The dates of the parameters are always written year first, as in code.

    curl -s -w '\nHTTP %{http_code}\n' 'http://localhost:4004/odata/v4/people/OrganizationalAssignments?asOfDate=30.06.2025'
    Terminal with two curl commands that the service rejects with HTTP 400: asOfDate together with fromDate and toDate is answered with the message Use asOfDate or fromDate with toDate, not both (callout 14), and asOfDate=30.06.2025 is answered with the message asOfDate must be a date written YYYY-MM-DD (callout 15)

Check the result

  • Without a parameter the answer is the record of today for each employee; the same address with asOfDate set to today’s date gives the same answer.

  • asOfDate=2025-06-30 returns Ana’s first record (personnel subarea 0001) and asOfDate=2026-07-01 her second (0002).

  • The range from 2026-06-01 to 2026-07-31 returns both of Ana’s records.

  • asOfDate=2025-06-01 on the parking permits returns an empty list.

  • asOfDate=2024-06-01 returns only Ana’s record.

  • The two wrong questions of steps 14 and 15 answer HTTP 400 with their messages.

  • In a copy of the lab, npm test in the project folder runs the tests that assert all of this.

Variations

  • Ask for a range that ends before the hire date, such as fromDate=2024-01-01&toDate=2024-02-29 on the organizational assignments. The answer is an empty list: no record touches the range.

  • Ask for a range that covers the whole history, fromDate=2024-01-01&toDate=9999-12-31, to read every record of an employee in order. A plain read returns only today’s records, so a range is how you read the history.

  • Add $count=true&$top=0 to an asOfDate query to count the records without reading them. The count of asOfDate=2025-06-30 is 3, one per employee.

  • Ask for BasicPayRecords with asOfDate=2025-06-30. The answer holds Ana’s 4,000.00 and Ben’s 2,400.00 EUR a month and Chloe’s 14.00 EUR an hour, with the pay basis Monthly or Hourly.

  • Add asOfDate to a query of Employees. The model does not read the parameter there, because an employee row is not effective-dated, and the answer is the three employees. Employee Central also has entities that are not effective-dated, such as Employment Information.

  • Read an employee with $expand=OrgAssignments, with or without asOfDate. The expanded records are not filtered by the date, so Ana’s two records come back. SAP’s reference states that in the SuccessFactors API the date parameters also apply to expanded entities; this model does not copy that.

What can go wrong

Symptom Cause Fix

asOfDate must be a date written YYYY-MM-DD and HTTP 400

The date has another format (30.06.2025), is empty (asOfDate=) or is not a day of the calendar (2026-02-30).

Write the date year first with two-digit month and day, and use a real day.

Use asOfDate or fromDate with toDate, not both and HTTP 400

The query has asOfDate and also fromDate or toDate.

Use one question: asOfDate, or fromDate with toDate.

fromDate and toDate must be used together and HTTP 400

The query has only one of the two dates of a range.

Give both. For an open end use the high date 9999-12-31 as toDate.

fromDate must not be after toDate and HTTP 400

The two dates of the range are the wrong way round.

Swap them.

The answer is today’s records although asofdate=2025-06-30 is in the address

The parameter name is case sensitive in the model, so asofdate is an unknown custom query option, which the service ignores, and the default of today applies.

Write asOfDate.

The shell prints zsh: no matches found: and curl does not start

The address is not in quotes and zsh reads the ? as a pattern.

Put the address in single quotes.

curl prints nothing, and echo $? prints 7

cds watch is not running in the first terminal, or it listens on another port.

Start the server and use the port that its last log line shows.

The answer is an empty list "value":[]

No record is effective on that date: before the hire date, or inside a gap of time constraint 2.

This is the answer. Ask for another date, or use a range to see the neighboring records.

Clean up

Press Ctrl+C in the terminal that runs cds watch. The database lives only in memory and the service never writes, so there is nothing to undo.

Where to go next