I have a hosted feature service on ArcGIS Online with some contingent values. I have been trying to change the contingent values by updating the "values:[]" to different numbers, but I must be getting the syntax wrong as I can't push the update through.
import arcpy
from arcgis.gis import GIS
import requests
import json
import arcpy
gis = GIS("pro", use_gen_token=True)
currentUser = gis.users.me
token = gis._con.token
params = {"f":"json", "token": token, "updateDefinition":{"contingentValuesDefinitions":[{"layerID":0,"layerName":"Testing_Round3","geometryType":"esriGeometryPoint","hasSubType":"false","fieldGroups":[{"name":"BestFieldGroup","restrictive":"true","fields":[{"id":0,"name":"FieldOne","fieldType":"esriFieldTypeString"},{"id":1,"name":"FieldTwo","fieldType":"esriFieldTypeString"},{"id":2,"name":"FieldThree","fieldType":"esriFieldTypeString"}],"domains":{"FieldOne":"FieldOne","FieldTwo":"FieldTwo","FieldThree":"FieldThree"},"contingentValues":[{"id":1,"types":[3,3,3],"values":[0,1,2]},{"id":2,"types":[3,3,3],"values":[1,2,0]},{"id":3,"types":[3,3,3],"values":[2,0,1]}]}]}]}}
urlpost = r'https://services8.arcgis.com/BD3gY4Sn5bdx9tEl/arcgis/rest/admin/services/Test_CVs_Round3_gdb/FeatureServer/updateDefinition?token={}'.format(token)
r = requests.post(urlpost, data=params)
rJson = r.json()
print(rJson)
Executing the code in Pro gets a return of :
{'error': {'code': 400, 'message': 'Unable to update feature service definition.', 'details': ["Invalid definition for ''.", "Unexpected character encountered while parsing value: c. Path '', line 0, position 0."]}}
I was able to use the code from the notebook to write a local script and get a couple questions I needed answered that I couldn't find online. Everything is running smoothly on my end, and we are able to easily update our contingent values now without having to download the layer! Thank you so much @CraigCheeseman for the quick reply, you just saved me a ton of time and headache!
Hi @CraigCheeseman, thank you so much for sharing this code! It has saved what would have otherwise been several weeks worth of work. I work for an active travel charity in the UK and we'll be sharing some resources of useful python scripts with the ESRI UK Non-profit group. Would it be OK if we share your script with the ESRI UK Non-profit group, crediting you?
@FrancescaAllen_ absolutely and I am glad others have found it useful. I did dig this out from ESRI so I didn't do this all on my own. Thanks.
Hi @CraigCheeseman ,
I know this post is fairly old but I've been struggling with the same issue. I tried your code adjustment and it went well until the final cell. The domains updated successfully but not the contingent values. I got a similar error as when I tried to adapt the Tree Solution script on my own. I am fairly new to this and am not really sure where I messed up. I am also only updating one layer from one table.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) In [20]: Line 13: for field in t[2]: TypeError: 'FeatureLayer' object is not subscriptable ---------------------------------------------------------------------------