Select to view content in your preferred language

Input parameter fails in web tool

95
0
Friday
Labels (1)
HanliePetoors
Frequent Contributor

Hi,

I have published a Python script tool as a web tool to ArcGIS Enterprise 11.3 from ArcGIS Pro 3.6.4. The script runs seamlessly in ArcGIS Pro.

But when I have published it and I run it from the REST endpoint I get the error: {'code': 500, 'message': "Object ID '[5]' is not valid.", 'details': []}. I get this error even when I hard code the value for source_objectid, so I don't think the problem is with the parameter.

Can anyone see what is causing this error?

This is the script; it's not the most robust script ever but I'm just trying to get it working first.

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
import uuid
import arcpy
# 1. Connect to the Portal
gis = GIS("home")
# --- CONFIGURATION ---
# Replace with your Feature Service Item ID
fs_item_id = "ac9428xxxxx69a470a767ec"
source_objectid = int(arcpy.GetParameter(0))

# Access the Feature Service
fs_item = gis.content.get(fs_item_id)
flc = FeatureLayerCollection.fromitem(fs_item)
# Identify layers/tables (Adjust indices [0], [1] based on your service structure)
parent_layer = flc.layers[0]       # Clearing tasks
related_table = flc.tables[0]      # Clearing task species (1:M relationship)
# Query the Source Parent Feature
# We need the attributes AND the GlobalID to establish relationships
source_features = parent_layer.query(object_ids=[source_objectid], out_fields="*")
source_feat = source_features.features[0]
# 3. Query Related Records for the Source Feature
relationship_id = "0" # task -> species
related_records = parent_layer.query_related_records(
    object_ids=str(source_objectid),
    relationship_id=relationship_id,
    out_fields="*"
)
# Extract the list of related features (handle case where no relations exist)
# Initialize empty list
source_related = []
# Check if related records exist in the dictionary
if related_records and 'relatedRecordGroups' in related_records:
    # The API returns a list of groups, one for each queried ObjectID
    groups = related_records['relatedRecordGroups']
    if len(groups) > 0:
        # Extract the 'relatedRecords' list from the first group (corresponding to your single OID query)
        # This returns a list of dictionaries representing the features
        source_related = groups[0]['relatedRecords']
# 4. Generate the Custom UUID (uniquerowid)
# Generates a random UUID.
# If 'uniquerowid' is a GUID field type, wrap in braces {}. If Text, braces are optional but safe.
new_unique_row_id = "{" + str(uuid.uuid4()) + "}"
# 5. Prepare New Parent Feature
new_attrs = dict(source_feat.attributes)
# Reset system fields (let DB handle OBJECTID, GlobalID, dates)
fields_to_reset = ['objectid', 'globalid', 'created_user', 'created_date', 'edited_user', 'edited_date']
for field in fields_to_reset:
    if field in new_attrs:
        del new_attrs[field]
# CRITICAL: Set the custom uniquerowid field to the generated UUID
# Replace 'uniquerowid' with the exact field name if different
new_attrs['uniquerowid'] = new_unique_row_id
new_parent = {
    "attributes": new_attrs,
    "geometry": source_feat.geometry
}
# 6. Add the New Parent
add_result = parent_layer.edit_features(adds=[new_parent])
new_objectid = add_result['addResults'][0]['objectId']
# (Optional) Verify the parent was created with the correct ID
# print(add_result)
# 7. Prepare Related (Child) Records
new_children = []
for child_dict in source_related:
    # Create a SHALLOW COPY of the attributes dictionary
    # This ensures you don't modify the original source data
    child_attrs = child_dict['attributes'].copy()
    # Reset system fields
    fields_to_reset = ['objectid', 'globalid', 'created_user', 'created_date', 'edited_user', 'edited_date', 'last_edited_user', 'last_edited_date']
    for field in fields_to_reset:
        if field in child_attrs:
            del child_attrs[field]
    # Assign the custom Foreign Key (using the UUID generated earlier)
    child_attrs['parentrowid'] = new_unique_row_id
    # Construct the feature dictionary
    new_feature = {
        "attributes": child_attrs
    }
    # Add geometry only if it exists in the source
    if 'geometry' in child_dict:
        new_feature["geometry"] = child_dict['geometry']
    new_children.append(new_feature)
if new_children:
    result = related_table.edit_features(adds=new_children)
    print(result)
arcpy.AddMessage("All done")

 Thanks

Hanlie

0 Kudos
0 Replies