Select to view content in your preferred language

How to override values for fields with calculated expressions

1886
19
03-25-2024 01:07 PM
AlixVezina
Esri Regular Contributor
8 19 1,886

You can save time collecting data and automatically calculate attribute values using the Editor widget with "smart" Forms. The Editor widget is available in Map Viewer and app builders such as Experience Builder and Instant AppsThe Forms builder is available in Map Viewer and Field Maps Designer; all you need is an editable feature layer.

If this is your first time, read this blog to get started building Forms.

 

Challenge

The values for fields with calculated expressions cannot be edited or recalculated on demand, while editable fields do not support calculated expressions.

Common use cases for the need to override a calculated value:

  • When the calculated expression fetches values from intersecting features: if multiple features are intersected, an unexpected value can be returned which needs correcting.
  • Calculated values based on other field values: if the other values are incorrect, manual adjustments may be required.
  • When the calculated expression doesn't return any value (blank/null): users may need to enter a value manually.

 

Workaround

Leverage field Logic in Forms to set dynamic behavior for editability based on a condition.

Open this sample Feature Layer in Map Viewer to view an example.

In the provided example, an editable feature layer has a Current Date field for which the value is automatically calculated based on the current date and time. Another field is used to define conditional editability for the Current Date field. If the Current Date field is editable, the date value can be changed. If the Current Date field is not editable, the date value is calculated and cannot be changed.

Override_Calc_exp.gif

 

Follow the steps below to incorporate this field Logic into your editable layers.

 

Step 1 - Update your data schema

Note: This workaround may impact your data model as you may need to add a new field to your layer.

  1. For a new editable layer or an existing layer, add a new field of type String
    • Give the field a name (e.g. Change Date in the sample layer)
    • The field must be Editable
    • The default value for the field should be "No"
  2. Add a List of Values (domain) for the field.
    • Values should be "Yes" and "No"

AlixVezina_0-1711388939928.png

 

Step 2 - Add the calculated field to the Form

  1. In Map Viewer or the Field Maps Designer app, create a Form for the layer.
  2. To the Form, add the field for which a value needs to be calculated (e.g. Current Date in the sample layer).
  3. Under Logic, define the Calculated expression using Arcade.
    In the example below, the expression Now() will automatically populate the current date and time into the Current Date field.

AlixVezina_1-1711389150835.png

 

Step 3 - Add the conditional field to the Form

  1. To the Form, add the field that end-users will use to make the Current Date field editable (Change Date in the sample layer).
  2. Under Formatting, update the Display name for the field (e.g. Toggle to override the date value above)
  3. Change the Input type to Switch.

AlixVezina_1-1711394672338.png

 

Step 4 - Add Logic for the calculated field

  1. For the calculated field (Current Date), under Logic, check the box for Editable and configure a new expression. (e.g. the Current Date field becomes editable if the Change Date field value is "Yes")
  2. Optionally, add an expression to make the field required if it's blank.

AlixVezina_2-1711396222917.png

AlixVezina_1-1711396077567.png

 

__________________

Special thanks to @ChrisFox@DougMorgenthaler and the Field Maps team for sharing this approach.

 

 

You might also like

 

19 Comments
WetherbeeDorshow
New Contributor III

I am getting a permissions issue when trying the link to the sample layer. I am logged in as a creator to my AGOL org.

Thanks

AlixVezina
Esri Regular Contributor

@WetherbeeDorshow Thanks for letting me know, please try accessing again now.

LindsayRaabe_FPCWA
Regular Contributor II

Gonna give this a whirl right now!

LindsayRaabe_FPCWA
Regular Contributor II

Looks like it works a treat! Used it to enable manual override of a calculated location name based on intersecting features. 

Calculated:

LindsayRaabe_FPCWA_0-1711405632605.png

Entered Manually:

LindsayRaabe_FPCWA_1-1711405657198.png

 

Kevin_MacLeod
Occasional Contributor

Great workaround, and I will give it a try, but hopefully out of the box editing for calculated fields will be supported soon, as well.  End users in the field have limited patience for clicking around which is understandable. Every extra click, is a barrier to GIS adoption.

LindsayRaabe_FPCWA
Regular Contributor II

To anyone implementing this, don't forget to also update your feature templates in Field Maps Designer to include the new fields and their default values - this caught me out as I did it in the WebMap smart form editor (which isn't at parity yet with Field Maps Designer). 

LindsayRaabe_FPCWA_0-1711665701076.png

 

JCooley_ClackamasSWCD
New Contributor III

I'm not sure if this approach is a good idea.  I tried to get this to work in one of our web maps that we use in Field Maps.  After I implemented the steps outlined in the article to add this functionality, the map viewer started to act buggy.  If I try to edit a point the edit pan gives me the spinning circle.  The map viewer will still let me minimize the edit tool pan but the pop-ups stop working.  I can expand the edit pan, but I get the spinning wheel.

I suspect that the conditional statement that prevents existing date and time values from being overwritten automatically is the root cause.  See the following Arcade statements for details.

// Set date and time to now if the field is empty.  This prevents field users from changing the date if they //need to edit a previous observation point in Field Maps.

if (!IsEmpty($feature.ObservationDate)){
  return
}
Else {
  now()
}
 
//Arcade statement using the toggle switch to make the calculated ObservationDate field editable in    //field maps 
DomainName($feature, "OverrideDate") == "Yes"
JCooley_ClackamasSWCD
New Contributor III

Quick update, 

After doing some more testing I figured out that it was the conditional part of the Arcade code that was causing the Map Viewer to get buggy.

//I removed this code from the field calculation and the problem when away.
if (!IsEmpty($feature.ObservationDate)){
  return
}
Else {
  now()
}
//end
 
I tried using  iif instead and the Map Viewer had the same issue.
 
//iif code 
Var obsvDate = iif(IsEmpty($feature.ObservationDate),now(),$feature.ObservationDate)

Return obsvDate
//end
 
The map viewer works just fine if I just use now() to calculate the date field.  I don't want to automatically overwright the date field when I'm editing another field.  Is there any work around that allows conditional statements to be used in the calculation code? 
 
 
LindsayRaabe_FPCWA
Regular Contributor II

I assume it's got something to do with calculating a field if it's empty, but then it's not empty so kid of a circular logic problem. Are there any other fields that get completed when the observation date is done that you could reference instead? Like an Observers Name? Or if the EditDate matches/doesn't match the CreationDate?

AlixVezina
Esri Regular Contributor

@JCooley_ClackamasSWCD  

Regarding your question: "I don't want to automatically overwrite the date field when I'm editing another field.  Is there any workaround that allows conditional statements to be used in the calculation code?"

Yes, you can use the $editcontext.editType Profile Variable to determine if the value is calculated only on create or update. Form Calculation | ArcGIS Arcade | ArcGIS Developers

AlixVezina_0-1712673435157.png

Some application examples in here: Go Beyond the Smart Editor using Smart Forms (esri.com)

AlixVezina_1-1712673483509.png

 

Amanda__Huber
MVP Regular Contributor

Cross posting this question here too, with hopes someone has a creative idea for a workaround until Esri resolves this. 

Unfortunately the workaround doesn't quite work with our workflow, there's no use case for a toggle override.

Our workflow- We have three types of observations. These observations are typically sequential in order based on the bird's nesting time-frame and all fields are editable
-but- 
Sometimes volunteers don't get to a nest until observation 2 or 3 in which we need the prior observations's fields to to be calculated to our default (use the calculated expression). 

Amanda__Huber_0-1715714553710.png

 

I don't see a good way to fit a toggle into this scenario with our workflow. Please let me know if you have any ideas. 

 

That being said, I think there are many use cases for this to be an enhancement that Esri implements as it's quite silly that calculations and editability can't both be in a form

 

Thanks, 

Amanda Huber

https://community.esri.com/t5/arcgis-online-questions/arcgis-online-map-viewer-forms-logic-issue/m-p... 

LukeGilner1
Occasional Contributor

@Amanda__Huber not sure if this work around will help you in your workflow, but this is what I've done to make fields calculated and editable (I found somewhere in the community, but can't remember who to credit).

In the Editable expression, I set the field to be editable when the field is not empty.  So, the calculated expression runs and is editable, but if the field is empty it is no longer editable (which shouldn't happen if the calculated expression works correctly).  Maybe this code example will show it better than I can explain it.

if($feature.LastInspectionDate == null || IsEmpty($feature.LastInspectionDate)) {
  return false
} else {
  return true
}
 
It works well for new features, but when you're editing a feature with existing data the calculated expression doesn't run, unless you hit the X to delete the existing data.
LindsayRaabe_FPCWA
Regular Contributor II

@LukeGilner1 I can definitely see use cases for this sort of logic in our workflows. I've already experimented with similar logic around creator name and current username. But being able to fill out the field automatically and then permit users to update it manually might be just the ticket I need for something I'm working on now. 

WetherbeeDorshow
New Contributor III

As an Arcade novice, I am wondering if someone would provide the exact syntax for the following scenario:

Feature layer has three fields: CurrentDate, ChangeDate (Yes/No Domain as above), and Remarks. For an existing feature, I want to update only the Remarks field and leave the CurrentDate to the value that was set on initial creation (during insert). 

What would the calculated expression be in this case. I would still want to use the Yes/No toggle for the Editable expression on insert.

Thanks

LindsayRaabe_FPCWA
Regular Contributor II

@WetherbeeDorshow 

So you would have a calculated expression against the CurrentDate field (unless you only want manual entry to permit backdating in the first instance. The below will return the date and time if the field is empty (feature creation). If it is not empty (editing) then it will retain the existing value:

 

var cd = iif(isempty($feature.CurrentDate),$feature.CurrentDate,Now())
return cd

 

Then use the below conditional statement for editing against the CurrentDate field (which you can create without needing to write arcade in the expression builder (though it formats it slightly differently): 

 

$feature.ChangeDate == "Yes"

 

 

LindsayRaabe_FPCWA_0-1717457353588.png

LindsayRaabe_FPCWA_0-1717458027846.png 

LindsayRaabe_FPCWA_1-1717458056886.png

It is behaving a little unexpectedly in that when you change an edited values toggle back to "No", it reverts the CurrentDate field to todays date, but that might be fine for you (leaving the switch as "Yes"). 

Curious to see if anyone else has another solution. I can't seem to make it work around the issue above without the default date calculation failing to run until the toggle is switched to Yes then back to No when creating a feature. GIF below shows intended functionality plus bug at the end. 

LindsayRaabe_FPCWA_1-1717467055503.gif

 

AlixVezina
Esri Regular Contributor

Hi @WetherbeeDorshow,

This took some playing around and picking some smart people's brains, but I think I have a solution that works and gets around the issue outlined by @LindsayRaabe_FPCWA  above.

2024-06-14_17-04-32.gif

Here's a sample web map that includes an example with the solution applied:

View Sample

You can save a copy of that web map to your Content and explore the Form and the way I configured the expressions.

Here's the Calculated expression for the Current Date field: 

AlixVezina_0-1718406407531.png

The logic for setting the Current Date as editable based on the Change Date field value is the same as described in the current blog post.

 

LindsayRaabe_FPCWA
Regular Contributor II

@AlixVezina Nice work. Thats definitely using Arcade in a way that I've not yet tried. It's capable of a lot more than my skills are up to trying yet! Will be filing this one in the "tips to remember" folder for sure!

WetherbeeDorshow
New Contributor III

@AlixVezina Thanks for this update. 

AlixVezina
Esri Regular Contributor

@WetherbeeDorshow Apologies, should be accessible now!

About the Author
Alix Vézina is a senior product engineer on Esri's ArcGIS Solutions Web Development team, where she has successfully delivered industry web mapping tools since 2019. Her previous work includes assisting in the digital GIS transformation of a property and land-management organization in New Zealand and coauthoring ArcGIS learning resources for the Public Safety sector. In her free time, Alix enjoys hiking with her puppy in the Colorado foothills.