KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Custom Querying with Computed Attributes
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: May 20, 2025

Computed attributes in your dataclasses aren’t limited to simple getters and setters—they can also be used to implement dynamic querying logic. This lets you define how to filter data flexibly based on parameters passed through the special $event object, which holds key details like the operator and the input value.

The $event parameter is essential and cannot be replaced — it’s the bridge between your query logic and the data filtering engine.

Here’s an example of a computed query attribute called query age. This function builds a query to filter contacts by their age, dynamically interpreting the operator (such as equals, greater or equal) and calculating date ranges to match birthdays accordingly.

Function query age ($event : Object)->$result : Object

  var $operator : Text
  var $age : Integer
  var $_ages : Collection

  $operator := $event.operator
  $age:=Num($event.value) // convert input value to integer

  // Calculate date range based on age
  $d1 := Add to date(Current date; -$age-1; 0; 0)
  $d2:=Add to date($d1; 1; 0; 0)
  $parameters := New collection($d1; $d2)

  Case of
    : ($operator = "==")
      $query:="birthday > :1 and birthday <= :2" // between dates

    : ($operator = "===")
      $query:="birthday = :2" // exactly on date

    : ($operator = ">=")
      $query:="birthday <= :2"

    // other operators can be added here


  End case

  // Return the query object only if no result is predefined

  If (Undefined($event.result))
    $result:=New object
    $result.query:=$query
    $result.parameters:=$parameters
  End if

Usage Examples
// People aged between 20 and 21 years (exclusive upper bound)
$twenty:=people.query("age = 20") // Triggers the "==" case

// People turning exactly 20 years old today
$twentyToday:=people.query("age === 20") // Equivalent to: people.query("age is 20")