KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Computed Attributes ordering with orderBy
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: August 26, 2024

Computed attributes are attributes of a dataclass that allow the implementation of a number of calculations. These include getters, setters, queries, and an orderBy function, which will be the topic of this Tech Tip. It is worth noting that the $event that is inside these functions is required and is not a parameter that can be changed; it's an object that contains values that helps compute the result.

Here is an example of an entity class, this one is named ContactEntity and contains a getter for a contact's full name as well as an orderBy function. The orderBy example is ordering by the fullName attribute. orderBy requires a get for the attribute the developer is ordering by. If the orderBy is not implemented, the orderBy function will sequentially sort based on the value inside this getter. When it is defined here it essentially overwrites the usual sort. In this particular example if the implementation of orderBy is left out it will be sorted by the getter's fullName which will start with the first name. When it is implemented this way it will sort by last name and then first and middle.

Class extends Entity

Function get fullName($event : Object)->$fullName_t : Text
   If (This.MiddleName="")
      $fullName_t:=This.FirstName+" "+This.LastName
    Else
      $fullName_t:=This.FirstName+" "+This.MiddleName+" "+This.LastName
   End if

Function orderBy fullName($event : Object)->$result : Text
   If ($event.descending=True)
      $result:="LastName desc, FirstName desc, MiddleName desc"
    Else
      $result:="LastName asc, FirstName asc, MiddleName asc"
   End if

Here is an example of this piece of code being called. For orderBy, the way it is set up means the default order will end up being ascending and if the $event's descending attribute is set to True by inputting "desc" into the string here it will order the contact selection in descending order.

var $nameAsc; $nameDesc : cs.ContactSelection

//orderBy ascending
$nameAsc:=ds.Contact.all().orderBy("fullName")

//orderBy descending
$nameDesc:=ds.Contact.all().orderBy("fullName desc")