How to override values for fields with calculated expressions

235
10
03-25-2024 01:10 PM
ShareUser
Esri Community Manager
0 10 235

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

 

10 Comments
ShareUser
Esri Community Manager

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

ShareUser
Esri Community Manager

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

ShareUser
Esri Community Manager

Gonna give this a whirl right now!

ShareUser
Esri Community Manager

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

 

ShareUser
Esri Community Manager

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.

ShareUser
Esri Community Manager

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

 

ShareUser
Esri Community Manager

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"
ShareUser
Esri Community Manager

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? 
 
 
ShareUser
Esri Community Manager

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?

ShareUser
Esri Community Manager

@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