Select to view content in your preferred language

Autonumbering arcade expression works in the Map Viewer but not in Field Maps

2224
15
Jump to solution
07-19-2023 04:25 PM
HollyTorpey_LSA
Occasional Contributor III

I have written this script to autonumber trees our arborists are surveying: 

// if this feature already has a Tree Number, return that
if(!IsEmpty($feature.TreeNumber)) { 
  return $feature.TreeNumber 
} else {

  // get the feature with the greatest Tree Number for the current project in the featureset
  var projectNumber = $feature.ProjectNum
  var Tree_features = Filter($featureset, "(TreeNumber IS NOT NULL) AND (ProjectNum = @projectNumber)")
  var max_Tree_feature = First(OrderBy(Tree_features, "TreeNumber DESC"))

  // for an empty featureset, this will be null, so return a 1
  if(max_Tree_feature == null) { return 1 }

  // calculate the max tree number for this project and return the next tree number for the current feature
  var max_TreeNumber = max_Tree_feature.TreeNumber
  var next_TreeNumber = max_TreeNumber + 1
  return next_TreeNumber
}

The script works great in the map viewer. If I add a new feature in the particular project I'm working in, it autonumbers the new feature 10 because there are already nine other features. However, if I open the same map in Field Maps and add a new feature, it autonumbers every new feature 1 regardless of the other tree numbers already in the project.
 
Map Viewer:
HollyTorpey_LSA_0-1689808836116.png

 

Field Maps mobile app:
HollyTorpey_LSA_1-1689809074136.png
 
The quick fix is to hard code the project number. We already have to update the Arcade expression that calculates the Project Number field each time we set up a Field Maps map with this layer for a new project, so we could also pop into this script and enter the project number here too. But I don't want to do that because it's an extra step for us to remember to do and because typing in (or pasting in) the project number twice is just an opportunity to introduce error. Plus I just want to know why this works in the Map Viewer but not in the Field Maps mobile app.
 
Thanks in advance for any help you can provide!
- Holly
0 Kudos
15 Replies
RhettZufelt
MVP Notable Contributor

OK, if I'm understanding you correctly, you have a separate Field Maps map for each project, and, when you add a feature, the project number is calculated in the form.  So, ALL features added with 'this' map will have the same project number.  Let's say it's '1234'.

// if this feature already has a Tree Number, return that
if(!IsEmpty($feature.TreeNumber)) { 
  return $feature.TreeNumber 
}
else {
var trees = FeatureSetByName($datastore,"TreeTest",['TreeNumber'],false)
var Tree_features = Filter(trees, "ProjectNum = '1234'")
var numarray = []
for (var n in Tree_features){
    var nn = Number(n.TreeNumber)
    Push(numarray, nn)
}
var maxnum = Max(numarray)
var newnum = maxnum + 1

return newnum
}

If this is the case, the above code is doing exactly has you hoped, and it works in both Map Viewer and Field Maps.  You would then just change the '1234' to the project number for each respective map. 

If I'm still not understanding correctly, I would need to know a little more specifics of how and WHEN the project number gets assigned to help further. 

R_

0 Kudos
HollyTorpey_LSA
Occasional Contributor III

Thanks, Rhett. I appreciate your help. Unfortunately, that's what mine already does (probably more clunkily) when I insert the project number into it.

I was trying to avoid having to add the project number to the tree number script because each time we prepare a new Field Maps map (sometimes we do multiple Field Maps per day), we already update the Arcade script for the Project Number field, and I would like to avoid having to also update the script for the Tree Number field.

Since we already entered the project number in the Project Number field, I would like to just pull that value rather than having to type the same thing in two different Arcade scripts for the same form. The issue is not that it's terribly difficult to have to do that, but it's just an opportunity for the person setting up the Field Map to forget to update the tree number field, or to accidentally enter a slightly different project number in each place. Both errors result in the tree numbers not autopopulating correctly. (I think I am making it more confusing here... the last paragraph of my original post probably explains it more succinctly).

- Holly
0 Kudos
RhettZufelt
MVP Notable Contributor

Well, whether you are entering the project number in the field, or if it is being calculated, you need some way for that to get populated.  If it is populated, then this script will find the highest tree number for that project and populate the tree number field.

// if this feature already has a Tree Number, return that
if(!IsEmpty($feature.TreeNumber)) { 
  return $feature.TreeNumber 
}
else {
var trees = FeatureSetByName($datastore,"TreeTest",['TreeNumber'],false)
var projectNumber = $feature.ProjectNum
var Tree_features = Filter(trees, "ProjectNum = @projectNumber")
var numarray = []
for (var n in Tree_features){
    var nn = Number(n.TreeNumber)
    Push(numarray, nn)
}
var maxnum = Max(numarray)
var newnum = maxnum + 1

return newnum
}

This works if the project number is typed in manually, calculated by the form, picked from a dropdown list, or populated with a template, etc.  So, depending on how may projects you have, there may be a less cumbersome way to deal with it than editing the arcade all the time.

Could make a separate map for each project, create a domain so there is a dropdown list to pick the project number in the form, (can always have them type it in), or, could create templates so there is a button for each project that has projectNum default value.  That way, when you add a new feature with it, the correct project number is entered, then it has enough info to find/update the tree number.

By what ever method used to assign the project number, this should work fine 'once' the project number is in the field so the form has access to it.

R_

0 Kudos
HollyTorpey_LSA
Occasional Contributor III

This does work in both the map viewer and in Field Maps, so thank you very much!

But I still don't understand why this one works in Field Maps and mine doesn't... Is the key difference that you are using FeatureSetByName to get the featureset to filter rather than just filtering $featureset directly?

- Holly
0 Kudos
RhettZufelt
MVP Notable Contributor

When I saw the reference saying it wasn't possible because the form only had access to the current feature, I didn't really spend time picking apart your code, but offered an example that I already had that I knew was working.

However, if I replace my code with $featureset instead, it still works fine in both.

Turns out, that for some reason, you line 12 is evaluating to Null in Field Maps, so returns the value of 1.  Not sure why it works in Map Viewer, but, as you have seen, it does.

So, I added a console statement to return the max_Tree_feature and saw that it is returning a dictionary string:

RhettZufelt_0-1690575287173.png

So I tested by just extracting the TreeNumber value from the string to test for null (line 12) and it worked.

So, after all that, if I would have just said to replace line 12 with:

 

  if(max_Tree_feature.TreeNumber == null) { return 1 }

 

Then your original code works as expected in both map viewer and Field Maps.

R_

HollyTorpey_LSA
Occasional Contributor III

Good grief! Thank you for figuring that out. I obviously need to use the console.

I really appreciate all the time you put into answering this question! Thank you so much.

- Holly
0 Kudos