Allow Default Value to be removed or changed in Hosted Feature Service Field View

9601
20
11-01-2019 10:09 AM
Status: Open
by Anonymous User
Not applicable

At this time, if you set up a Default Value for a field upon creation of a Hosted Feature Service, you have to delete and re-create the field to remove the default value.

Please allow the functionality to re-configure the Default Value of a field post hoc.

20 Comments
tiztrain

@Louis-PhilippeRodier you're a legend! It worked

Louis-PhilippeRodier

@tiztrain, I know! It's nice when things work out!

ColinKelly97

@Louis-PhilippeRodier How did you achieve this in map viewer classic? I want to make NC the default value of this layer called scan layer, specifically the Status field of the layer. I followed your instruction via map viewer classic and went into the properties but do not see an option to assign NC as a default value. 

ColinKelly97_2-1691611080884.png

 

ColinKelly97_0-1691610917980.pngColinKelly97_1-1691610984435.png

 

JosephAlberts

The easiest way I've found to update the default value (or coded domains) is using the arcgis Python API. Something like this, exported from an ArcGIS hosted notebook (split into cells as you see fit).

This is a generic/pared-down example where I was setting the default value for a specific field with a string-type coded value in a table. You could do this for one or more fields in a layer too.

Note on coded domains: If you created the domain ("list") through ArcGIS Online, the name will have a GUID in it. To find the full name, take a look at the JSON for the service URL of the layer/table you're using.

Edit: I forgot to account for updating the template as well. This addition will also loop through any views of the hosted feature layer and update their templates for the field.

Edit 2: Corrected missing update_dict declaration. Thanks @AndyFairbairn!

 

 

 

# Import required libraries
from arcgis.gis import *
from arcgis.features import FeatureLayer, FeatureLayerCollection
from IPython.display import display

# Set GIS and display current user
gis = GIS("home")
user = gis.users.me

print("Current User: \n")
user

## Get Data Collection Layer
item_collection_layer = gis.content.get('your_item_ID')
flc_data_collection = FeatureLayerCollection(item_collection_layer.url, gis)
flc_data_collection

### Review layers
print('Layers: \n')
for index_val, lyr in enumerate(flc_data_collection.layers):
    name = lyr.properties.name
    print(index_val, '-', name)

### Review tables
print('Tables: \n')
for index_val, tbl in enumerate(flc_data_collection.tables):
    print(index_val, '-', tbl.properties.name)

### Assign table of interest
tbl_to_update = flc_data_collection.tables[0]
print('tbl_to_update set to:', tbl_to_update.properties.name)

## Field definition(s) for updating
## Make sure your type and other parameters are correct for your field.
field_to_update = {
      "name" : "field_name", 
      "type" : "esriFieldTypeString", 
      "alias" : "Field Alias", 
      "sqlType" : "sqlTypeOther", 
      "length" : 256, 
      "nullable" : True, 
      "editable" : True, 
      "domain" : 
      {
        "type" : "codedValue", 
        "name" : "domain_name_from_json_view_of_table", 
        "codedValues" : [
          {
            "name" : "Coded Value 1", 
            "code" : "coded_value_1"
          }, 
          {
            "name" : "Coded Value 2", 
            "code" : "coded_value_2"
          }
        ]
      }, 
      "defaultValue" : "coded_value_1"
    }

## Update field(s)
## If you need to update multiple fields at once, define them all above and pass them all into the list for `update_dict`

update_dict = {"fields": [field_to_update]}

tbl_to_update.manager.update_definition(update_dict)

## Template definition(s) for updating

template_tbl_to_update = {
    "name" : "Table Name", 
    "description" : "", 
    "drawingTool" : "esriFeatureEditToolNone", 
    "prototype" : {
        "attributes" : {
            "field_name" : "coded_value_1", 
            "notes" : None
        }
    }
}

## Update template(s)
source_item = item_collection_layer
view_items = source_item.related_items(rel_type="Service2Service", direction="forward")

list_tbl_to_update = [tbl_to_update]

for view_item in view_items:
    list_tbl_to_update.append(FeatureLayerCollection(view_item.url, gis).tables[0])

print('Tables to update:')

for tbl in list_tbl_to_update:
    print(tbl, '(' + tbl.properties.name + ')')

### Update
update_dict = {"templates": [template_tbl_to_update]}

for tbl in tbl_to_update:
    update = tbl.manager.update_definition(update_dict)
    display(tbl)
    print('Status:', update)
    print('===' * 10)

 

 

 

 

AndyFairbairn

Hi Joseph, thanks for this approach it's a really useful suggestion for me.

I think you might be missing a line after your template dictionary? Something like:

update_dict = {"templates": [template_tbl_to_update]}

Or something like

 

## Template definition(s) for updating

template_to_update = {
    "name" : "Template Name", 
    "description" : "", 
    "drawingTool" : "esriFeatureEditToolNone", 
    "prototype" : {
        "attributes" : {
            "field_name" : "coded_value_1", 
            "notes" : None
        }
    }
}

update_dict = {"templates": [template_to_update]}
JosephAlberts

Yup, you're absolutely right @AndyFairbairn! Thanks for the catch.

That's what I get for trying to slice up a larger notebook with just the relevant pieces. 🙃

L77
by

I would also like to be able to change the default value just by clicking on a drop down menu in ArcGIS Online.

For my use case the default value is the current year, so it was 2023 and I needed to make it 2024.

As a work around I created a new year column in ArcGIS Online, pulled the hosted feature layer into ArcPro, field calculated over the values from the old year column to the new year column, then deleted the old year column. Next I set the default value to 2024.

Then I had to go into map viewer, web app builder and field maps and reconfigure the pop-up windows and filters to all use the new year column.

This took about an hour to make all of these changes and to check that everything was working. I also had our field staff remove their offline areas in field maps and re download them, so it took their time too.

It would be nice in 2025 to be able to just change the default value to 2025 using a drop down menu in the field settings screen. Then I wouldn't have to reconfigure all of my apps.

Louis-PhilippeRodier
 

@ColinKelly97 , it seems that you are using the default value for you symbology, hence the non availability to change the default value. If you use display location only, you won't have that problem.

 
Louis-PhilippeRodier

@L77, do you mean that the default is also the symbology? Cause if you have drop down menus in the feature layer, you should see the drop down menu in the default option.

LouisPhilippeRodier_0-1706053529396.png

 

L77
by

@Louis-PhilippeRodier 

I am not using the year as the symbology. I am using the type and status as a two part symbology:

L77_1-1706115237576.png

 

The year column is auto-populated so the worker doesn't have to enter it, I use the year field so they can filter by year in the web app builder app using the filter widget, and in field maps:

L77_2-1706115385175.png

 

I am hoping in the future we can just change the default value here: 

L77_0-1706114981463.png

Is your screenshot from the symbology properties?