An insurer has extended the ABContact entity in ContactManager with an array of Notes to capture information of interest about the contact over time. A developer has been asked to write a function to process all the notes for a given contact. Which code satisfies the requirement and follows best practices?
Show answerHide answer
Correct answer: B for ( note in anABContact.Notes) { //do something }
Explanation
Answer Option (Which option is correct) B. for ( note in anABContact.Notes) { //do something }Explanation:
A for-in loop visits each note in the contact Notes collection directly. It does not need a manually maintained index and does not stop at the first matching note. A while condition that only checks whether a note exists does not itself advance through the collection.
Why the other options are incorrect:
A. while (exists (note in anABContact.Notes)) { //do something }:
The while condition tests existence without advancing. It can repeat indefinitely while a note still exists. A for-in loop visits each note once.
C. for (i = 1..anABContact.Notes.length) { //do something }:
The manual index starts at one. Gosu collection indices are zero-based. Direct iteration avoids that index-boundary risk.
D. var aNote = anABContact.Notes.firstWhere( \ note -> note.Author != null) //do something:
firstWhere returns one qualifying note. It does not process every note. The requirement concerns the entire collection.
References:
Gosu language project - Operators, loops, functions, and properties
Guidewire - Query-backed collections and buildBaseQuery