3

Previous posts (such as Adding automated values to attribute field?) answer how to calculate sequential values in an attribute table. The posts all seem to rely on the process described in this ESRI Tech Support page:

https://support.esri.com/en/technical-article/000011137

Here are the article's details:

  1. Create a new short integer field.
  2. Set the Parser to Python.
  3. Select Show Codeblock.
  4. Paste the following into the Pre-Logic Script Code:

    rec=0 
    def autoIncrement(): 
     global rec 
     pStart = 1  
     pInterval = 1 
     if (rec == 0):  
      rec = pStart  
     else:  
      rec += pInterval  
     return rec
    
  5. Paste the following code in the smaller box below the Pre-Logic Script Code:

    autoIncrement()
    
  6. Click OK.

But at 10.5.1 I can achieve the same results with this single line in the field calculator:

!FID! + 1

The results are exactly the same as the more complex ESRI-supplied code, even if the the table is sorted on a non-FID field and/or there is a selected set of records.

What am I missing here? Does the ESRI-supplied code do something that I'm not aware of?

  • 1
    Fid + 1 valid for shapefiles only. Edited feature class in database will likely fail. – FelixIP Dec 23 '17 at 1:16
7

The Esri supplied code starts from 0 and increases by 1. Your method will do the same only if your FID starts at 0 and increases sequentially. An ObjectID or FID is system managed, so if you delete a record you will have gaps, and in a geodatabase you could have a feature class that has an ObjectID starting at 300000 (for example), so you're not going to get a straight 1,2,3,4 etc. from that using your code.

  • 1
    To add to this. The ESRI code will work on a selection. The FID + 1 won't. So has a few more applications. – Keagan Allan Dec 24 '17 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.