Use a lookup?
Create a field previousRecord = RecordID - 1 //assumes the RecordID field is a number.
Set up a relationship of previousRecord = RecordID
Set up the beginningOdometer field as a lookup using this relationship and the endingOdometer field.
Your script doesn't create a new record. Either you created it first and ran the script or you aren't getting a new record at all. Either way, your script doesn't work as it tries to go to the last record ( and you should just specify "last" rather than using the calculation option as your max function isn't likely to work in this context).
But here's a simpler, non scripted method to look up the previous record's Odometer Reading:
Set up the following auto-enter calculation:
Let ( R = Get ( RecordNumber ) ; If ( R > 1 ; GetNthRecord ( Previous Odometer Reading ; R - 1 ) ) )
This assumes that you have either an unsorted found set (which will automatically list the records in creation order) or sorted by RecordID with the focus on the last record immediately before creating a new record in this table.
The first problem is that in using Get(recordid) to create a calculated field, you don't know if the highest record id exists or was it deleted.
So, before creating your new record
show all records
sort by id field
go to last record
set variable $x to id
set field y to $x
there is another technique I'm tired. It involves getting the max value in the field and then searching for that...
My suggested method does not use get ( RecordID ), but Get ( RecordNumber ) and there is always a Get ( RecordNumber ) equalt to 1 in a given found set.
The request was for the last record not the last one in a found set, which might be valid.
A calculated field using Get(recordid) will always show the last record id no matter what client created it. It also will reveal missing records in the table which is how I proved to a client that records were deleted long before I was hired so get off my back about missing records...
The original post states:
I want to write a script that will get the value from the previous record's ENDING ODOMETER field
..."(as determined by a RECORD ID field)"
I rest my case.