Using the for…of loop on Detail Data
Utilizing section cloning from a Control Script often requires iterating over data in a Detail Table. The for...of loop offers a modern and readable approach to iterating over arrays or collections compared to the traditional for loop. While this technique is very useful for iterating detail tables, it can also be applied to a wide range of other use cases. Here’s how you can use the for...of loop and how it compares to the traditional for loop.
Traditional For loop
The traditional for loop is structured with an index that increments with each iteration. This loop requires manual management of the index and accessing each element in the array or collection by its index.
letdetailTable= record.tables.Policies
for (let i = 0; i <detailTable.length; i++) {
let row =detailTable[i]
logger.info(row.policyName)
}
In the example above:
iis the loop counter, starting at0and incrementing until it reaches the length of the detail table.detailTable[i]accesses each record in the detail table.row.fieldName(orrow.fields.Fieldname) accesses a specific field in the current detail record.
Drawbacks of the traditional for loop:
- You have to manually manage the index (
i), which can be error-prone. - The code can be harder to read and maintain, especially in more complex loops.
Modern For…of loop
The for...of loop simplifies iteration by directly looping through the elements of an array or collection without needing to manage an index.
let detailTable = record.tables.Policies
for (let row of detailTable) {
logger.info(row.policyName)
}
In the example above:
rowis a variable that represents the current element (in this case, the current record of the detail table) in each iteration.- The loop automatically goes through each element in
detailTable, and record data is automatically assigned to therowvariable.
Advantages of the for...of loop:
- Simplified syntax: No need to manage the loop index manually. This reduces the chance of errors, such as off-by-one errors.
- Improved readability: The loop is more concise and easier to understand. It clearly communicates that you’re iterating over each element in the collection.
- Direct access to elements: The current element (e.g., the record from the Detail Table) is directly assigned to a variable, making it straightforward to access and manipulate the data within each element.
The following example demonstrates the for...of loop in action, used to clone print sections based on a Detail Table.

Accessing the Index in a For…of loop
When using a for...of loop with a Detail Table in the OL Connect data model, you can easily access the index of each iteration. Here’s how you can do it:
In Detail Tables, the index of the current row is directly accessible using row.index. This is particularly useful for keeping track of the position of each detail record as you process the data.
let detailTable = record.tables.Policies;
for (let row of detailTable) {
logger.info(`Index: ${row.index}, Field: ${row.policyName}`)
}
Explanation:
- In the example
row.indexprovides the index of the current record in the Detail Table directly. - This allows you to log or use the index within your loop without needing additional methods or calculations.
Conclusion
Using the for...of loop is generally recommended when iterating over arrays or collections like Detail Tables in the OL Connect data model. It makes your code cleaner, easier to read, and less prone to errors compared to the traditional for loop. By assigning each element directly to a variable during iteration, you streamline the process of accessing and working with the data, making your code more maintainable in the long run.