Calculate Function Won't Update with New Values

1846
16
Jump to solution
02-18-2022 09:42 AM
by Anonymous User
Not applicable

Hello,

I am using Enterprise 10.9.1.  I have created a Field Map App and using Collector on desktop.  When a new complaint is entered in the app, I would like for the total count of complaints to automatically update. But it doesn't. 

In the mapviewer/Enterprise, I created a new field and used the calculate function to count all complaints.  When I enter the calculate function it works great - thanks to this forum. But when I add a NEW complaint in the app, the total count doesn't update. I have to go back to map viewer and re do the calculate function. 

I assumed this would update automatically, but after reading about SQL and the calculate function it states "one of the most common calculations you perform is to derive a new numeric value based on existing values in your feature layer." I think existing values is the key word. I want it to update on new values. 

Any suggestions. It was suggested to use python as a script, but since I am using map viewer in the Enterprise that is not an option. Plus only a beginner at python.

Has anyone else come across this? Any idea what to do? Thank you!

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

You're so close! Just a minor tweak and you'll have it.

In the return, you only need to set the attribute "total" to the variable "total". In other words:

return {
attributes: {
total: total
}
}

And then put "{expression/total}" in the line item template down below the code section.

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
16 Replies
jcarlson
MVP Esteemed Contributor

Can you clarify: is the "complaint count" an attribute on your features? And if there are 20 complaints, is that field being set to "20" for all of them?

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

Hello,

Yes "complaint count" (total number of complaints) is an attribute on my hosted feature layer.  Ideally, it adds the complaints every time a complaint is entered. But in reality, it only adds them when I re enter the SQL expression in the calculate field in the "complaint count" attribute.

AnnettePoole1_0-1645207321420.png

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Got it. And what' the context in which you need this number? Since this is an in-row calculation, most any of the Arcade profiles should be able to handle it, even the Symbology profile.

var sum_fields = [
    $feature.some_field,
    $feature.another_field,
    ect...
]

var total = 0

for (var s in sum_fields){
    If (!IsEmpty(sum_fields[s])){
        total += 1
    }
}

return total

 

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

The calculation in SQL works fine, just doesn't update when a NEW complaint is added. Plus it appears I cannot use Arcade as an option.  Note it states that Arcade cannot be used because Sync enabled or does not have editing enabled.  Both layers I unchecked Sync enabled and both are editable. So I am not sure why its not allowing me to use Arcade. At any rate, the SQL expression works except for NEW values.

 

AnnettePoole1_0-1645208710532.png

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Sorry, I mean why does this number need to be a static field? Where are you using its value where an expression couldn't be used instead?

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

Great questions.  I don't want it to be static, I want it to be dynamic if I understand what you are asking.  I created an app for use in Collector and Field Maps.  I wanted the user to be able to go to the address, open complaints and see a total number. 

I didn't wan the user to have to count them add enter it themselves, hence the calculate field. 

I am creating using the calculate field in enterprise map viewer so expressions aren't an option. 

A few options, maybe I could bring the layer to ArcPRO and create the expression. OR just show the total number of complaints on a dashboard and create the expression in the dashboard, although it would be nice for them to be able to see it in the app as well. 

Are you saying if I use an expression instead of SQL I would be able to dynamically update? Thank you.

AnnettePoole1_0-1645217219932.png

 

0 Kudos
jcarlson
MVP Esteemed Contributor

To be clear, the field will not be dynamic.

But if it would be okay for the "total complaint count" to be in the popup or the form's title, you could easily use Arcade in those places with the expression I posted above, and the expression evaluates when the feature is clicked or the form is loaded.

- Josh Carlson
Kendall County GIS
by Anonymous User
Not applicable

Here's what I did. And of course any help is appreciated with item 3 below.

1. I know this wasn't the intention, but I wanted to try it. I brought the layer into ArcPRO to test Arcade expression above. It worked. 

2.  I then tried it in the map viewer and I got this. 

AnnettePoole1_0-1645223480879.png

3. Then I tried in the dashboard Arcade, but this is what I got. I did change the field names to the correct field names, hence the $datapoint. I don't know enough to know what is wrong. Any  help is appreciated.

AnnettePoole1_1-1645223963576.png

Thank you.

0 Kudos
jcarlson
MVP Esteemed Contributor

So, in a dashboard's Advanced Formatting profile, your expression will have to go above the return section.

// Your expression

var total = 0

... etc ...

// The return. Unless you need to change the background / text color based on some attribute, you can leave those off.

return {
   attributes: {
     total: total
   }
}

 

Then in your list item's formatting, reference your custom attribute as {expression/total} and you'll see it populate in the list.

jcarlson_0-1645225490819.png

 

- Josh Carlson
Kendall County GIS
0 Kudos