Select to view content in your preferred language

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

1337
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
DougBrowning
MVP Esteemed Contributor

You are using YesCount at the beginning but in the loop you have yesCount.  That could be it.

But as posted this will all break if someone deletes a record in the middle.  

KenBuja
MVP Esteemed Contributor

Since that field has Domains, are you using the same value for Label and the Code?

Ed_
by MVP Regular Contributor
MVP Regular Contributor

Hi @KenBuja , thank you for pointing that out so yes the domain values and labels are the same as you can see below

Ed__0-1731014777656.png


However, as if this moment they are no Yes values in the attribute it self, right now they are all Not rated

Ed__1-1731014819255.png

 

Question | Analyze | Visualize
0 Kudos
HollyTorpey_LSA
Frequent Contributor

So your line 5 now looks like this? 

 

var YesVal = Filter(layer, "attribute1 = 'Yes'")

 

 

And as @KenBuja said, if your code and label are different, you could be getting an empty featureset from that filter. Maybe wrap that count expression with an if statement checking if your featureset is empty.

 

if (!isEmpty(first(YesVal))) {
  var YesCount = Count(YesVal) 
} else YesVal = 0 

 

- Holly
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @Ed_ ,

Try the solution below. My assumption is you it may be something to do with the layer name but it is hard to say what the issue is.

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

// Filter the attribute value and count values
var FilterField = "attribute1"
var FilterValue = 'YES'
var YesCount = Count( Filter( layer , FilterField + ' = @FilterValue' ) )

// If the current feature's attribute is "Yes", add 1 to the count
iif ($feature.attribute1 == "Yes" , yesCount + 1 , null )

 Also, is your problem similar to the one found on stack overflow. Because if it is, then that would help anyone else here point you in the right direction.

Similar sounding issue