The For-each loop

This is the simplest form of loop available as it always behaves the same way. When adding a for-each loop to your script, you must specify an entity in the Datastore to use for the loop. When the system encounters the loop, it will retrieve all the instances of that entity within its parent entity, and perform one iteration of the loop (presenting each page within the loop) for every instance returned. For example, the following loop could be used to display the ExtraPersonDetailsPage for each person in this application (the root entity in our earlier example):

Figure 1. For-each Loop
<loop loop-type="for-each" entity="Person">
  <question-page id="ExtraPersonDetailsPage">
    ...        
  </question-page>
</loop>

More commonly, you might not want to loop through all instances of an entity, in which case you can add a criteria which the system will use to select only those instances of the entity which match the criteria. For example, to loop through all the people for whom the 'hasIncome' attribute has been set to true, use a loop like this:

Figure 2. For-each Loop with Criteria
<loop loop-type="for-each" entity="Person" 
criteria="hasIncome==true"> 
  <question-page id="IncomeDetailsPage" entity="Income"> 
    ...       
  </question-page>
</loop>

If a criteria is used in a for-each loop it is recommended the criteria should contain a simple expression referring just to a single attribute of type Boolean (for example "hasIncome==true"). If a single attribute is referred to in the criteria the attribute can be automatically updated by IEG when using summary links (for example when all the nested entities are removed the attribute can be set to false or when the first nested entity is added the attribute can be set to true). This functionality is not available if the criteria contains a complex expression. It is also recommended a default value should be defined in the Datastore schema for this attribute.