Select to view content in your preferred language

Arcade: Auto-populate a field based on another field's value count

1285
14
11-07-2024 09:04 AM
Ed_
by MVP Regular Contributor
MVP Regular Contributor

Good morning folks, hope all is well, so i have a field say attribute1 that has "Yes", "No" and "NA" list values to choose from in the Field Maps survey.

Then i have another field say attribute2 that will be auto-populated such that whenever the user select Yes, Arcade will count it and return "Value 1" the first time. Similarly, the next time the user select "Yes", Arcade will return "Value 2" and so on kind of like a counter.

Desired output:

Attribute1       Attribute2

Yes                   Value 1

Yes                   Value 2

No                    <Null>

No                    <Null>

NA                    <Null>

Yes                    Value 3

 

Arcade expression:

// Store the layer as a variable
var layer = FeatureSetByName($map, "sf")

// Filter the attribute value
var YesVal = Filter(layer, "attribute1" == "Yes")

// Count the number of "Yes" features
var YesCount = Count(YesVal) 

// If the current feature's attribute is "Yes", add 1 to the count
if ($feature.attribute1 == "Yes") {
    return "Value", yesCount + 1;
} else {
    return null;
}

 

Error:

Test execution error: t.charAt is not a function. Verify test data.
Question | Analyze | Visualize
0 Kudos
14 Replies
KenBuja
MVP Esteemed Contributor

Change line 12 to this

return `Value ${yesCount + 1}`

 

Ed_
by MVP Regular Contributor
MVP Regular Contributor

Hi @KenBuja thank you for the quick response 🙂 i am  still getting the same error though

Test execution error: t.charAt is not a function. Verify test data.

// Store the layer as a variable
var layer = FeatureSetByName($map, "sf")

// Filter the attribute value
var YesVal = Filter(layer, "attribute1" == "Yes")

// Count the number of "Yes" features
var YesCount = Count(YesVal) 

// If the current feature's attribute is "Yes", add 1 to the count
if ($feature.attribute1 == "Yes") {
    return `Value ${yesCount + 1}`;
} else {
    return null;
}

 

Question | Analyze | Visualize
0 Kudos
HollyTorpey_LSA
Frequent Contributor

You likely already thought of this and maybe it doesn't matter for your purposes, but this method will result in Attribute2 duplicates if Field Maps users delete features other than the newest. I get around this by adding a field to store the numbers. Then a script on that number field filters for the numbers that meet my criteria, sorts them largest to smallest, and adds 1 to the first. Then the script on my version of the attribute2 field just concatenates the value and the number from the new field.

- Holly
Ed_
by MVP Regular Contributor
MVP Regular Contributor

Hi Holly, thank you for your quick response as well 🙂 can you explain a bit further 

like how do you do this "by adding a field to store the numbers"

Also can you please share a workable code example on "Then a script on that number field filters for the numbers that meet my criteria, sorts them largest to smallest, and adds 1 to the first. Then the script on my version of the attribute2 field just concatenates the value and the number from the new field." based on the Arcade expression i have shared?

Question | Analyze | Visualize
0 Kudos
HollyTorpey_LSA
Frequent Contributor

Sure. The example I'm referring to is a tree data collection template that is used for different projects all the time. We set up a new Field Maps map for each new project with a field calculation for the project number field. Trees are numbered sequentially starting at 1 for each new project, and that number is stored in the tree number field. If I just counted the trees for the project and added one, I'd end up with duplicate tree numbers if users deleted features (if they already had trees 1, 2, and 3, then deleted tree 1, then added a new tree, the new tree would be numbered tree 3 even though I already have a tree 3). So instead I record the numbers in an integer field, sort them descending, and then add 1 to the first number in the sorted set.

Here's the script that does that:

var nextTreeNumber = 1
var projectNumber = upper($feature.ProjectNum)

// If this is an existing feature with a Tree Number, return that
if ($editcontext.editType == 'UPDATE') {
  if(!IsEmpty($feature.TreeNumber)) {
    return $feature.TreeNumber
  }
}

// Otherwise, get tree number values for the whole Arborist Template featureset
// Check that the featureset isn't empty
if (!IsEmpty(first($featureset))) {

  // Query the featureset for trees with the same project number (even if it's null)
  if (isEmpty(projectNumber)) {
    var projectTrees = filter($featureset, "ProjectNum IS NULL")

  } else {
    var projectTrees = filter($featureset, "UPPER(ProjectNum) = @projectNumber")
  }

  if (!IsEmpty(first(projectTrees))) {
    var maxTreeFeature = First(OrderBy(projectTrees, "TreeNumber DESC"))
    
    // if there are no trees with the same project number, return 1
    if (maxTreeFeature.TreeNumber == null) { return nextTreeNumber }

    // if there are trees with the same project number, add 1 to the highest one
    var maxTreeNumber = maxTreeFeature.TreeNumber
    nextTreeNumber = maxTreeNumber + 1
    return nextTreeNumber
  } else { return nextTreeNumber }
} else { return nextTreeNumber }


 

- Holly
Ed_
by MVP Regular Contributor
MVP Regular Contributor

Thanks Holly that's useful but i think it'll be needed for this project but it might be useful for another project in future so thank you so much 🙂

Right now i am still trying to figure out why i am getting that error  

Question | Analyze | Visualize
HollyTorpey_LSA
Frequent Contributor

What happens if you only use one equals sign in the query in your filter parameters? ("attribute1 = 'Yes'")

- Holly
Ed_
by MVP Regular Contributor
MVP Regular Contributor

then it returns an error for the Filter Function Invalid variable assignment

Question | Analyze | Visualize
0 Kudos
Ed_
by MVP Regular Contributor
MVP Regular Contributor

This is the how Attribute1 actually looks like

Ed__0-1731005841638.png

and this is Attribute2

 

Ed__1-1731005897673.png

 

Question | Analyze | Visualize
0 Kudos