KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Use Find in field to efficiently check for a value in record
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: June 11, 2020

The Find in field command is a great tool to use if you need to quickly look through a table to see if at least one record has a value for a certain field. For example, imagine you have an application with a database of Users. Users have a username and a nickname. This particular application allows duplicate nicknames, but you want to warn a user if they are trying to register a nickname that already exists. To check for an existing nickname, instead of doing something like this:

QUERY([Users];[Users]nickname=$desired_nickname)
If (Records in selection([Users])>0)
   // Warn the user
Else
   // Do something else
End if
UNLOAD RECORD([Users])

Where you perform a query on the table, load current selection, then unload the records, a more optimized approach would be to use Find in field like this:

If (Find in field([Users]nickname;$desired_nickname)>=0)
   // Warn the user
Else
   // Do something else
End if

The advantage of the latter code is that it checks for the value without needing to load a current selection (which has to be unloaded afterwards), making it a more efficient solution.