Handle entities in an entity selection

FIRST RECORD, LAST RECORD, NEXT RECORD, PREVIOUS RECORD; does that ring any bells? I bet it does. You’ve already been using them to handle selections! ORDA provides useful and familiar methods to navigate through entities in an entity selection – and obviously, some perks!

For starters, you can handle several entity selections at the same time and navigate through them independently, whereas normally you can only manage a single ‘current selection’ per table. That’s precisely what we’re going to see in this blog post.

We’ve already seen that querying the datastore returns an entity selection. ORDA provides the means to access the entities in an entity selection very easily.

ORDA: Handle entities in entity selections


An empty entity selection is created using the newSelection() method. You can also add entities to it with the add() method.



//Create a new empty entity selection
$name:=Request("Which employee?")
 If (ok=1)
   //Get the first employee whose last name is $name
  //Add the entity to the new entity selection $es
  If ($employee#Null)
  End if
 End if
Until (ok=0)
alert(String($es.length) + " employees have been added to the new entity selection")


Remember, you can iterate through entities in a entity selection with the for each loop. Here is a quick example, but also take a look at this blogpost.

// Get employees working in Paris
//Iterate through each employee
For each ($employee;$employeesParis)
End for each

You can also load an entity with its index into an entity selection (index starts at 0). Here is an example:

//Get all the gamers sorted by rank
if($gamers.length >=3)
ALERT("The first three are: "+Char(13) \
End if


The first() and last() methods let you get the first or last entity of an entity selection. These methods may return Null if the entity selection is empty.

In the example below, we get the first entity of the $gamers entity selection:

// Get the gamers whose last name starts with "H"
// Get the first entity of the $gamers entity selection
If ($first#Null)
 ALERT("The first gamer has been found")
 ALERT("The entity selection $gamers is empty")
End if

Previous AND next ENTITIES

An entity may belong to an entity selection. In this case, you can iterate through the entities of an entity selection with the previous() and next() methods.

In this example, we use a form to display a list of gamers. The user can navigate through the details of each gamer with the Previous and Next buttons.

The Next button has the following object method:

//Form.gamer is an entity, edited on the form. It belongs to an entity selection which is the entire list of gamers.
If (Form.gamer.next()#Null)
 //Edit the next entity of the entity selection to which the Form.gamer entity belongs.
End if

info about an entity

And that’s not all! For any given entity, you can get the entity selection it comes from. Something else good to know, there are also methods to get contextual information about an entity, e.g. check if it belongs to a given entity selection, get its position in an entity selection, etc.

Here, we have two entity selections ($employeesParis and $employeesNY). The selectedEmployee method selects an employee in one of them. We can then determine which entity selection this employee belongs to with the contains() method:

// Get employees working in Paris
// Get employees working in New York
$employeesNY:=ds.Employee.query("city='New York'")
// The method selectedEmployee returns an employee in one of the two given entity selections
Case of
: ($employeesParis.contains($employee))
ALERT("The employee belongs to the entity selection $employeesParis")
: ($employeesNY.contains($employee))
ALERT("The employee belongs to the entity selection $employeesNY")
End case


Here, we have an entity selection ($employeesParis). The selectedEmployee method seelcts an employee from it. We can get the employee’s index in the entity selection with the indexOf() method:

// Get employees working in Paris
// The method selectedEmployee returns an employee in the given entity selection
$employee:=selectedEmployee ($employeesParis)
ALERT("The index of the selected employee is " + String($employee.indexOf()))

Take a look at the database example and you’ll know all about this subject!

• Product Owner • Marie-Sophie Landrieu-Yvert has joined the 4D Product team as a Product Owner in 2017. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.Marie-Sophie graduated from the ESIGELEC Engineering School and began her career as an engineer at IBM in 1995. She participated on various projects (maintenance or build projects) and worked as a Cobol developer. Then she worked as an UML designer and Java developer. Lately her main roles were analyzing and writing functional requirements, coordinate business and development teams.