This is probably a very simple question: I would like to run a standalone script that populates a field with a sequential ID sorted by a datetime field. I can run the code (below) successfully in the ArcGIS Pro python window, but when I try to run it in IDLE 3.7 the “rec” variable is not recognized.
The error I receive is: “NameError: name 'rec' is not defined”. As I understand it, this means the code doesn’t know what “rec” is. When I move rec under “Global rec” and the script runs in IDLE, but populates the entire column with 1.
Here is the python code I'm trying to use. Any help would be greatly appreciated!
sortFeat = Road_Activity_Reports
sortField = 'open_datetime'
idField = 'open_rep_cnt'
rec = 0
def autoIncrement():
global rec
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec += pInterval
return rec
rows = arcpy.UpdateCursor(sortFeat, "", "", "", sortField + " A")
for row in rows:
row.setValue(idField, autoIncrement())
rows.updateRow(row)
del row, rows