How to create a sequential unique ID

7642
5
Jump to solution
09-01-2020 10:37 AM
Soper_Chuck
New Contributor III

I have a Survey123 form that will be used several times a day by over a hundred users. Every record (survey submitted) in my feature layer needs a unique ID. It isn't easy to ensure unique numbers in Survey123. I want to populate the Unique_ID field with COB1, COB1, COB2, and so on. (I prefer that the starting number is 100). Once a record is set, I can't change it again (it may have been exported). I think that a calculated field could be scheduled to run daily in ArcGIS Pro. (I don't entirely know how to do that.) I'm using AcrGIS Pro 2.6 on Windows 10. To start the sequence, I would need to count the number of non-null (or not empty string) values in the Unique_ID field. 

I'm new to ArcGIS Pro and Python. I wrote the code below from this example:

https://pro.arcgis.com/en/pro-app/tool-reference/data-management/calculate-field-examples.htm

 

rec=0

def autoIncrement():

 global rec

 pStart = 1 

 pInterval = 1

 if (rec == 0): 

  rec = pStart 

 else: 

  rec += pInterval 

 textRec = "COB" + str(rec)

return textRec

To get the starting value of the sequence, I would need to count all non-empty fields (not null or not empty string). I think that this post shows how to do that: https://community.esri.com/thread/224234-field-calculator-for-not-null-fields.

I'm trying to put this all together. Am I on the right track? Again, I'm new to ArcGIS Pro and Python.

Thanks,

Chuck

0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Emeritus

Have you taken a look at Database Sequences? You would then need to create an Attribute Rule and you use Arcade rather than python in the rule.

Another approach is to use the FID or OID of your record and concatenate it with your prefix in the field calculator:

yourFieldName = f'COB{!OBJECTID!}'

this uses what is called f-strings that is a relatively new feature in python 3.x.  The object id field is a built in sequence so it is always unique and will never be re-used if the record is deleted.

That should just about do it....

View solution in original post

0 Kudos
5 Replies
JoeBorgione
MVP Emeritus

Have you taken a look at Database Sequences? You would then need to create an Attribute Rule and you use Arcade rather than python in the rule.

Another approach is to use the FID or OID of your record and concatenate it with your prefix in the field calculator:

yourFieldName = f'COB{!OBJECTID!}'

this uses what is called f-strings that is a relatively new feature in python 3.x.  The object id field is a built in sequence so it is always unique and will never be re-used if the record is deleted.

That should just about do it....
0 Kudos
Soper_Chuck
New Contributor III

Using the ObjectID seemed like the best approach. I don't need an exact sequence. I just can't have the field data ever change. Using the following works great. Thanks for your help.

f'COB{100 + !objectid!}'

A co-worker and I also discovered that the following (older style Python) also works:

'COB' + str(100 + !objectid!)

Thanks,

Chuck

0 Kudos
DanPatterson
MVP Esteemed Contributor
"COB100{}".format(!objectid!)

# --- or

f"COB100{!objectid!}"

For more current format string operations with 2 variants


... sort of retired...
Soper_Chuck
New Contributor III

Joe, Thank you very much for your response. I appreciate it. I had to fix some other issues in Survey123. I'll look into your suggestions within the next few days. Thanks, Chuck

0 Kudos
AndrewAidt2
New Contributor III

I am trying to get a unique ID field populated. The field is CarteID. I want a prefix of SSP and use the ObjectID field. Example: ObjectID is 10234 - I want the CarteID to be SSP10234. Using Python 3, how do I use the field calculator to populate this field. What Expression do I use?

Then, going forward, is there a way to automatically populated this field when someone adds a new feature?

0 Kudos