Auto calculate unique id in hosted feature service

1605
3
Jump to solution
03-15-2022 05:54 AM
Labels (1)
HanliePetoors
New Contributor III

Hi,

I have a hosted feature service in AGOL that requires a unique id (more user friendly than a GlobalID) for each feature in the service.

How can I accomplish that?

I have tried attribute rules, but they disappear once the service is published.

Arcade can only change field values in the field calculator on the data tab of the item details.

I can only consider AGOL-only solutions, i.e. a script that runs on a schedule on a server and updates the service is not an option.

I will appreciate any ideas.

Thanks

Hanlie

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Actually, now that AGOL Notebooks have introduced notebook scheduling, a script that runs schedule is totally an option, even for AGOL-only users! And a simple field calculate would cost you next to nothing in terms of credits, even if the script ran fairly often.

But to your question, the simple answer is "you can't". I recall hearing at some point that Attribute Rules are part of AGOL's long term plan, but for the time being, you just can't have a field that updates itself. There needs to be some human (or scripted) interaction to update a field.

If you do decide to take the Python approach, we had a script running via an AGOL notebook that did just this sort of thing, making use of the calculate method. Here's a simplified version of it:

from arcgis import GIS
from arcgis.features import FeatureLayer

gis = GIS('https://arcgis.com', 'username', 'pw')
fl = FeatureLayer('url to your layer', gis=gis)

id_sql = "some SQL expression to derive your unique ID"

fl.calculate(
    where = 'uniqueIDfield IS NULL',
    calc_expression = {'field': 'uniqueIDfield', 'sqlExpression': id_sql}
)

And that's it! By specifically targeting rows that have no unique ID calculated yet, each run of the script only has to calculate values for a handful of rows, if any, and the entire process only takes a few seconds. At the current rate of 1.5 credits per hour, you're probably looking at a cost of 0.005 credits per execution.

- Josh Carlson
Kendall County GIS

View solution in original post

3 Replies
jcarlson
MVP Esteemed Contributor

Actually, now that AGOL Notebooks have introduced notebook scheduling, a script that runs schedule is totally an option, even for AGOL-only users! And a simple field calculate would cost you next to nothing in terms of credits, even if the script ran fairly often.

But to your question, the simple answer is "you can't". I recall hearing at some point that Attribute Rules are part of AGOL's long term plan, but for the time being, you just can't have a field that updates itself. There needs to be some human (or scripted) interaction to update a field.

If you do decide to take the Python approach, we had a script running via an AGOL notebook that did just this sort of thing, making use of the calculate method. Here's a simplified version of it:

from arcgis import GIS
from arcgis.features import FeatureLayer

gis = GIS('https://arcgis.com', 'username', 'pw')
fl = FeatureLayer('url to your layer', gis=gis)

id_sql = "some SQL expression to derive your unique ID"

fl.calculate(
    where = 'uniqueIDfield IS NULL',
    calc_expression = {'field': 'uniqueIDfield', 'sqlExpression': id_sql}
)

And that's it! By specifically targeting rows that have no unique ID calculated yet, each run of the script only has to calculate values for a handful of rows, if any, and the entire process only takes a few seconds. At the current rate of 1.5 credits per hour, you're probably looking at a cost of 0.005 credits per execution.

- Josh Carlson
Kendall County GIS
HanliePetoors
New Contributor III

Hi Josh,

This sounds like a great solution, thank you very much!

Regards

Hanlie

0 Kudos
KristalWalsh
Occasional Contributor II

Thank you, Josh! This is what I've been looking for. Just have to use the right search words because I've been looking for a solution for a while. Kristal

Kristal Walsh, Florida Fish and Wildlife
Office of Conservation Planning
0 Kudos