Accessing a specific row's data in a subform

Accessing a specific row's data in a subform

In my sub form i have several rows and I wish to access the last row inputted in order to send some of the info in it via email when the form is submitted (On Add -> On Success in the main form).

I have tried using the script from this forum: https://help.zoho.com/portal/en/community/topic/subform-get-value-from-the-row-above

My rows in the sub form ( AKA Actions_Taken) before the script is executed:

Date ROW ID
4/21 1
4/22 2
4/23 3
4/24 4


My Script (I tried it both on the 'On Edit' -> 'On Success' on the main form and also on the 'On Add Row' in the sub form and I am getting the same results):

max_row_id = 0;
info max_row_id; // Added for debugging - returned 0
//Find the previous max row ID
for each Action_Taken_row in input.Action_Taken
{
if(Action_Taken.Row_ID > max_row_id)
{
max_row_id = input.Action_Taken.Row_ID;
}
}

info max_row_id; // Added for debugging - returned 1. I expected it to return 4, seems the loop above is not looping but just returning the value in the first row.

//Assign the row id for current row
input.Action_Taken.Row_ID = max_row_id + 1;

info Action_Taken.Row_ID; // Added for debugging - returned 2. I expected it to update the last record with the value '5' but nothing happens.

rows in the sub form after the script had been executed:

Date ROW ID
4/21 1
4/22 2
4/23 3
4/24 4


My questions:

1) How can I retrieve the information from the latest row?
2) How can I automatically populate the current row I am adding with info from previous rows?

Thank you,
Gaby