In your Select Next Resident script, line 21 is using the Residents::gPortalRowSelected field, but that field has not been set within the script (see line 5 of Select Resident script)
Sorry about that, previous response was incorrect. Residents::gPortalRowSelected is set in the Select Resident script.
There is an inconsistency in where the Select Resident and Select New Resident scripts are leaving the gPortalRowSelected field. Select Resident leaves the gPortalRowSelected field as the currently selected portal row, whereas Select New Resident leaves gPortalRowSelected as the next portal row.
Increment the Residents::gPortalRowSelected field at the end of the Select Resident script to match what is happening in the Select Next Resident (line 30) script or change your Select Next Resident script to move down two rows to find the Next Resident.
Thanks, philipHPG - I had tried moving down two rows before, but of course it then jumped two rows every time. I may have tried that in the wrong place, of course. The original script is more complex, but I stripped it down to this bare minimum, and the oddity still shows.
The logic for the 'Select Resident' script leaving the gPortalRowSelected field as the currently selected portal row is to have that benchmark of 'which row the user last clicked on', and then I can increment up and down from that known point.
The logic for exiting the 'Select Next Resident' script as the next row down (from the last row actually clicked on) was to imitate the user actually clicking in the portal, one row down from the last click. I hoped that would make other associated scripts more reliable - no matter how the ResidentID has been selected, the associated scripts starting point will always be the same.
When I watch the script in the data viewer it shows the correct portal row as active (the cursor flashes in a field in the correct row) but the next step of capturing the relevant ResidentID actually captures the ID of the previous portal row - except on every subsequent click.
In that case, in the Select Next Resident script, move lines 29 and 30 up to line 8 (move the portal row down before you start reading it).
Also, you probably want to add the error checking from Select Resident into Select Next Resident, but you've probably done that in your full script.
As philip noted, you're setting your gPortalRowSelected field differently in your Select vs. Select Next script. That's the issue.
But I'd suggest taking a different approach. There's no need to keep jumping in and out of the portal. Use the List function to grab all the IDs when you click Select, and then when you click Next, use GetValue from that ID list to set your Selected and Next values.
Thanks, Tom - unfortunately I can't use the 'List' and 'GetValue' trick as in the real file the user types in a global field to filter the portal.
You've both made me realise I was confusing the 'Next' Row with the 'next-Next' Row. I made it more clear (to myself) by returning to the 'Original' Row, stepping down one to the 'Original Next' Row (as before), then correctly trapping it as the new 'Row last clicked on' (as Philip suggested), then stepping down another Row to capture the 'New Next' Row.
Thanks very much for your help,