Tech Tip: Making Small Optimization Over DataClass.query()
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: November 19, 2025
The DataClass.query() function is a powerful tool that allows searching of records of an entire database table. However, sometimes it is not necessary to use to retrieve the specific entity needed. If one finds themselves searching for an entity based on ID alone, for example:
The above example queries a “Users” DataClass on “ID” attribute and takes the first (and only) result, and would technically work. However, it would be better to use the DataClass.get() function instead of DataClass.query() in this case, for instance:
This is a small optimization that saves some time writing the code. It also condenses the tasks into a single step. Note that DataClass.get() only works on primary key fields.
| $entSel:=ds.Users.query("ID == :1"; $userID) $user:=$entSel.first() |
The above example queries a “Users” DataClass on “ID” attribute and takes the first (and only) result, and would technically work. However, it would be better to use the DataClass.get() function instead of DataClass.query() in this case, for instance:
| $user:=ds.Users.get($userID) |
This is a small optimization that saves some time writing the code. It also condenses the tasks into a single step. Note that DataClass.get() only works on primary key fields.