Arcade & Calculate field with text and incrementing number with a starting number and an interval

3493
20
12-15-2021 04:09 AM
AGP
by
Occasional Contributor

I have Python calculate field expression (.cal file) that does this but I'm trying to migrate it to Arcade.

It's not working. The number part is not incrementing and I guess it has to do with me not being able of defining 'rec' as a global variable.

This is the Python expression. How do I migrate the 'global rec' line to Arcade?

#Params
val_counter = 2498
prefix = 'PP_'
interval = 1
# -------------------------------------------
# Function
rec=0
def SetIDcode(val_counter, prefix, interval):
  global rec
  if (rec == 0):
    rec = val_counter
  else:
    rec += interval
  return (prefix+ str(rec))

 

0 Kudos
20 Replies
jcarlson
MVP Esteemed Contributor

Here's the equivalent of your function in Arcade:

 

var rec = 0
function SetIDcode(val_counter, prefix, interval){
    if (rec == 0){
        rec = val_counter
    } else {
        rec += interval
    }
    return prefix + rec
}

 

Here's me testing it with the sample parameters you gave:

jcarlson_0-1639575923687.png

Can you explain a bit more about how you use this, though? Is the rec variable always going to be 0, or are there other parts of the expression that alter it?

- Josh Carlson
Kendall County GIS
0 Kudos
AGP
by
Occasional Contributor

The rec variable is 0 for the first feature, so the returned valued is prefix + val_counter (PP_2498).

But then the rec varaible should get the value in val_counter for the second feature returning prefix + val_counter + interval (PP_2499) and incrementing by interval for each next feature (PP_2500, 2501,...).

Your function (mine was like yours) returns PP_2498 for every feature.

0 Kudos
jcarlson
MVP Esteemed Contributor

I see. That will only work for an iterative loop, not in the Field Calculate profile. When writing a field calculation in Arcade, you have to imagine that you're already in the loop, as the expression will be evaluated for every feature.

In Arcade, we can access and work with the other values in the layer, so you've got some options. Is the code stored only as a string, or is there some way to access the numeric val_counter without the prefix?

Also, what values are in this field right now? Are they null? Does the val_counter correspond to the number of records in the layer?

- Josh Carlson
Kendall County GIS
0 Kudos
AGP
by
Occasional Contributor

The user inputs the val_counter value according to their needs, as well as the prefix and interval values.

I guess interval will always be 1 but they are given the option to decide.

0 Kudos
jcarlson
MVP Esteemed Contributor

Are you sure the Arcade field calculator is the right option for this? You may be better off my making your python script into a GP tool.

- Josh Carlson
Kendall County GIS
0 Kudos
AGP
by
Occasional Contributor

The python field calculator is a good option actually. We wanted to use the existing tools in ArcGIS Pro as much as possible and also try to use Arcade as much as possible, but I see python is still the way to go for some processing. We are still evaluating all the geoprocessing needs of our users and maybe will consider using a custom python toolbox. We'll see.

0 Kudos
jcarlson
MVP Esteemed Contributor

Could you build this into a model, perhaps? Then you could still be using a field calculation with the expression baked in, but expose the layer and field as user-specified parameters.

- Josh Carlson
Kendall County GIS
0 Kudos
JoeBorgione
MVP Emeritus

What if you use an attribute rule that grabs a the next value from a geodatabase sequence?

That should just about do it....
0 Kudos
AGP
by
Occasional Contributor

I thought of the possibility of using Batch calculation rules so users would execute the rules whenever they need it. We should just publish our feature services with the validation capability enabled and create a database sequence (if the db admin approves).

The problem here is that the user decides the layer and field they will populate so attribute rules are not an option. Using a calculate field expression is more convenient here but we wanted to use Arcade instead of Python.

 

0 Kudos