Populate UID field in new feature with Object ID value (Arcade)

147
2
Jump to solution
04-03-2024 10:30 AM
Labels (3)
JasonFitzsimmons
Occasional Contributor

We have a feature layer in a web map that is basis of an editor using Experience Builder.   The clients should be able to create new features as well as edit existing features in the app. 

Due to some scripts being used, we need a 'UID' field of unique numeric IDs.

It will work for our purpose if we can grab the value in the Object ID field  and populate the UID field with this value when a new feature is created.

So far I am not finding any Arcade snippets that may work for this. Ideally this value would be written during the feature creation in the app, but then not editable.

Ideas? 









0 Kudos
1 Solution

Accepted Solutions
gis_KIWI4
Occasional Contributor

Hi @JasonFitzsimmons  - 

You could use the ( $editcontext.editType=="INSERT" ) to control the field being calculate during the creation of the record but this will still be problematic if you plan on using Object ID. You cannot use the ObjectID during insert because it doesn't exist. 

Okay there a few other option we could explore. 

1) Calculating ObjectID from the feature layer

Get the max ObjectID from the feature layer and add one to it to get the next object ID.
The edit context type will ensure it only fires when its a new record and since it is a calculated field it will not be editable in form.

if($editcontext.editType=="INSERT")
//do the following only if the edit type is insert
{
var x = FeatureSetByName($datastore, "TEST", ["OBJECTID"], false)
if (count(x) > 0)
return Max(x,"OBJECTID")+1
else 
return 1
}
else 
{
return $feature.UID2
}

gis_KIWI4_0-1712183346051.png

The first record will be different for the reason that there is no OBJECTID present to calculate the 

gis_KIWI4_1-1712183377671.png

2) Use CurrentDateTime to get a unique value

If you need a unique number then you could consider creating one out of the date time. 
Solution 1 relies on reading the feature layer and I think the number of records could affect performance but with this approach, it will be agnostic of the number of records while giving a unique value. (feel free to add miliseconds to the end if you wanted to be 100% sure :D)

if($editcontext.editType=="INSERT")
{
var x = now()
var y = text(x,'YMMDDhhss')
return y
}
else {
  return $feature.UID3
}

gis_KIWI4_2-1712183625011.png

 

3) While the above 2 may do the trick, I think the most robust solution would be using GUID function in arcade. 

https://developers.arcgis.com/arcade/function-reference/text_functions/#guidguidformat---text

Hope this helps

 

View solution in original post

2 Replies
gis_KIWI4
Occasional Contributor

Hi @JasonFitzsimmons  - 

You could use the ( $editcontext.editType=="INSERT" ) to control the field being calculate during the creation of the record but this will still be problematic if you plan on using Object ID. You cannot use the ObjectID during insert because it doesn't exist. 

Okay there a few other option we could explore. 

1) Calculating ObjectID from the feature layer

Get the max ObjectID from the feature layer and add one to it to get the next object ID.
The edit context type will ensure it only fires when its a new record and since it is a calculated field it will not be editable in form.

if($editcontext.editType=="INSERT")
//do the following only if the edit type is insert
{
var x = FeatureSetByName($datastore, "TEST", ["OBJECTID"], false)
if (count(x) > 0)
return Max(x,"OBJECTID")+1
else 
return 1
}
else 
{
return $feature.UID2
}

gis_KIWI4_0-1712183346051.png

The first record will be different for the reason that there is no OBJECTID present to calculate the 

gis_KIWI4_1-1712183377671.png

2) Use CurrentDateTime to get a unique value

If you need a unique number then you could consider creating one out of the date time. 
Solution 1 relies on reading the feature layer and I think the number of records could affect performance but with this approach, it will be agnostic of the number of records while giving a unique value. (feel free to add miliseconds to the end if you wanted to be 100% sure :D)

if($editcontext.editType=="INSERT")
{
var x = now()
var y = text(x,'YMMDDhhss')
return y
}
else {
  return $feature.UID3
}

gis_KIWI4_2-1712183625011.png

 

3) While the above 2 may do the trick, I think the most robust solution would be using GUID function in arcade. 

https://developers.arcgis.com/arcade/function-reference/text_functions/#guidguidformat---text

Hope this helps

 

JasonFitzsimmons
Occasional Contributor

Thanks! we have actually been looking at both of these options.


0 Kudos