KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Entity queries will always return an entity selection even when there is one entity
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: February 8, 2019

When using query commands like entitySelection.query() and dataClass.query(), one can easily assume that a single returned result would be an entity. However, these commands will always return an entity selection even when there was only one entity that matched the query. In that case, always remember to specify the entity selection index or use the command entitySelection.first() to get the single returned entity from the resulting entity selection.

// Incorrect example
C_OBJECT($result)
C_TEXT($fname)

$result:=ds.People.query("ID = :1";2)
$fname:=$result.first_name // incorrect, entity selections do not have properties


// Correct example
C_OBJECT($result)
C_TEXT($fname)

$result:=ds.People.query("ID = :1";2)

If ($result.length=1) // check that only one entity is in entity selection
   $fname:=$result.first().first_name // correct
   $fname:=$result[0].first_name // also correct
End if