Skip to main content
Solved

Edit coded value domains for a feature layer in AGOL using PythonCaller?

  • April 28, 2021
  • 9 replies
  • 192 views

ileung_cnw
Contributor
Forum|alt.badge.img+3

I am receiving this error below when I try to retrieve the getJSON variable:

Python Exception <Exception>: Unable to update feature service layer definition.
Object reference not set to an instance of an object.
(Error Code: 400)
Error encountered while calling function `processFeature'
PythonCaller_3 (PythonFactory): PythonFactory failed to process feature
PythonCaller_3 (PythonFactory): A fatal error has occurred. Check the logfile above for details
A fatal error has occurred. Check the logfile above for details
JSONFormatter (JSONFormatterFactory):

This is the code in PythonCaller (simplified for readability):

import fme
import fmeobjects
from arcgis.gis import GIS
 
def processFeature(feature):
   getJSON = feature.getAttribute('JSON')
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

I know getJSON is giving me issues because if I replace lyr.manager.update_definition(getJSON) with lyr.manager.update_definition({ "fields" : [ { "name" : "FIELDNAME", "domain" : { "type" : "codedValue", "name" : "DOMAINNAME" : [ { "name" : "TEST", "code" : "TEST" }, { "name" : "TEST1", "code" : "TEST1" }, { "name" : "TEST2", "code" : "TEST2" } ] } } ] } then it works. 

 

Essentially, I like to create a JSON object in an attribute using JSONTemplater and put it into PythonCaller so I don't have to hardcode the JSON object. 

 

Thanks in advance!

Best answer by david_r

getAttribute() returns a string, where as update_definition() seems to expect a dict.

Possible workaround:

import fmeobjects
from arcgis.gis import GIS
import json
 
def processFeature(feature):
   # parse str to dict
   getJSON = json.loads(feature.getAttribute('JSON'))
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

 

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

9 replies

ileung_cnw
Contributor
Forum|alt.badge.img+3
  • Author
  • Contributor
  • April 29, 2021

Alternatively, I was able to edit coded value domains using HTTPCaller. This now allows me to update new coded value domains to an existing feature class from data collected in Survey123.

 

Below are some of the tips that got me through this issue:

 

Screenshot of my HTTPCaller parameters for reference:

httpcaller


david_r
Celebrity
  • Best Answer
  • April 29, 2021

getAttribute() returns a string, where as update_definition() seems to expect a dict.

Possible workaround:

import fmeobjects
from arcgis.gis import GIS
import json
 
def processFeature(feature):
   # parse str to dict
   getJSON = json.loads(feature.getAttribute('JSON'))
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

 


ileung_cnw
Contributor
Forum|alt.badge.img+3
  • Author
  • Contributor
  • April 29, 2021

getAttribute() returns a string, where as update_definition() seems to expect a dict.

Possible workaround:

import fmeobjects
from arcgis.gis import GIS
import json
 
def processFeature(feature):
   # parse str to dict
   getJSON = json.loads(feature.getAttribute('JSON'))
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

 

Thanks for the quick reply! Parsing JSON str to dictionary resolved the issue. The API reference documented this but now I understand how to parse it. Good to know, thank you! 👍 


ileung_cnw
Contributor
Forum|alt.badge.img+3
  • Author
  • Contributor
  • April 29, 2021

Alternatively, I was able to edit coded value domains using HTTPCaller. This now allows me to update new coded value domains to an existing feature class from data collected in Survey123.

 

Below are some of the tips that got me through this issue:

 

Screenshot of my HTTPCaller parameters for reference:

httpcaller

I compared using PythonCaller and HTTPCaller and realized HTTPCaller finishes ~3x faster.


Forum|alt.badge.img
  • June 30, 2021

getAttribute() returns a string, where as update_definition() seems to expect a dict.

Possible workaround:

import fmeobjects
from arcgis.gis import GIS
import json
 
def processFeature(feature):
   # parse str to dict
   getJSON = json.loads(feature.getAttribute('JSON'))
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

 

This worked like a charm, but somehow I get an error when trying to import json. 

Python Exception <ModuleNotFoundError>: No module named 'ujson'

Does anyone have a clue what is happening here? 


david_r
Celebrity
  • June 30, 2021

This worked like a charm, but somehow I get an error when trying to import json.

Python Exception <ModuleNotFoundError>: No module named 'ujson'

Does anyone have a clue what is happening here?

Sounds like a problem with a third-party module.

Have you tried setting different Python interpreters in your workspace?


Forum|alt.badge.img
  • June 30, 2021

This worked like a charm, but somehow I get an error when trying to import json.

Python Exception <ModuleNotFoundError>: No module named 'ujson'

Does anyone have a clue what is happening here?

Thanks David, it looks like that indeed; Have to look into the extra module; case closed for now.


vhruska
Contributor
Forum|alt.badge.img+6
  • Contributor
  • October 13, 2022

Alternatively, I was able to edit coded value domains using HTTPCaller. This now allows me to update new coded value domains to an existing feature class from data collected in Survey123.

 

Below are some of the tips that got me through this issue:

 

Screenshot of my HTTPCaller parameters for reference:

httpcaller

Hello! I'm trying to accomplish the same task in Survey 123 but I'm getting hung up pulling down the initial domain values. Do you have an example workspace you may be able to share?


evieatsafe
Safer
  • Safer
  • October 14, 2022

Hello! I'm trying to accomplish the same task in Survey 123 but I'm getting hung up pulling down the initial domain values. Do you have an example workspace you may be able to share?

Hi @vhruska​ I would post a new question on the community and refer to this thread as this question is quite old. Hope this helps.