ValidationError: Model requires an object_id

179
4
01-10-2020 02:48 PM
Highlighted
New Contributor III

Most pressing question:

I'm working with the ArcGIS Python API for Workforce and basing my code off this example.

When I use `batch_add` the code works and it adds an assignment.

However, I do not want to add an assignment, I want to update an existing record from "unassigned" to "assigned" so I want to use 'batch_update'.  But when I use that I get an error which says, "arcgis.apps.workforce.exceptions.ValidationError: Model requires an object_id"

Does anyone know what that means or how to fix it?

Less pressing question:

For the 'workforce.Assignment' parameters, I get an error saying "arcgis.apps.workforce.exceptions.ValidationError: Assignment cannot have an empty location".  But the documentation on Assignment says location is Optional.  Does anyone know why it's required?  (This only happens when using 'batch_add' since 'batch_update' doesn't work.)

Thanks!

## https://github.com/Esri/workforce-scripts/blob/master/notebooks/dev_summit_2019/Step%203%20-%20Add%20Assignments%20From%20Existing%20Layer.ipynb

import arcpy
from datetime import datetime
import pandas as pd
from arcgis.gis import GIS
from arcgis.apps import workforce

param = arcpy.GetParameterAsText(0)

gis = GIS("<agol>""<user>""<pw>"## Credentials for dispatcher
item = gis.content.get("<itemid>")

project = workforce.Project(item) ## https://developers.arcgis.com/python/api-reference/arcgis.apps.workforce.html#project

workerManager = workforce.managers.WorkerManager(project) ## https://developers.arcgis.com/python/api-reference/arcgis.apps.workforce.managers.html#arcgis.apps.workforce.managers.WorkerManager
worker = workerManager.get(user_id="<user>")

layer = gis.content.get("<itemid>").layers[0
features = layer.query(where="field_name = " + str(param), out_sr=3857).features

## https://developers.arcgis.com/python/api-reference/arcgis.apps.workforce.html#assignment
assignments = []
for feature in features:
    assignments.append(
        workforce.Assignment(
            project=project,
            geometry=feature.geometry,
            assigned_date=datetime.now(),
            location=feature.attributes["location"], 
            description=feature.attributes["description"],
            priority=1,
            assignment_type="Update Assignment",
            status=1,
            worker=worker
        )
    )

project.assignments.batch_update(assignments)
4 Replies
Highlighted
Occasional Contributor II

Hi Daniel 

I am actually trying to accomplish the very same thing and i was wondering if you came up with a solution? I looked at the api documentation but it gives very little information. the documentation here refers to using batch methods but nothing specific to what i require either.  

Thanks 

Paul 

Reply
0 Kudos
Highlighted
New Contributor III

Hi Paul, sorry for the late response. I just got back from vacation and have to re-think through what all I was doing.

Since our workflow switched to a different direction, I ended up doing the `batch_add` function and then running through the update process I found here. https://developers.arcgis.com/python/guide/editing-features/#Updating-features 

I hope that helps. I have not tried Aaron's solution.

project.assignments.batch_add(assignments)

my_features = assignments_layer.query(where="<query>").features

if(len(my_features) == 0😞
    arcpy.AddError("Nothing found")
elif(len(my_features) > 1😞
    arcpy.AddError("More than one record found")
else:
    my_edits = my_features[0]
    my_edits.attributes['<field>'] = "an_attribute"
    update_result = assignments_layer.edit_features(updates=[my_edits]) ## https://developers.arcgis.com/python/guide/editing-features/#Updating-features
Reply
0 Kudos
Highlighted
Esri Regular Contributor

Hi,

The "ValidationError: Model requires an object_id" error is because you are calling batch_update on new assignment objects that haven't been inserted into the underlying feature layer. You cannot update a feature that doesn't exist.

The recommended approach would be to do this:

worker = project.workers.get(user_id="<user>")
assignments = project.assignments.search(where="<query>")
for assignment in assignment:
    assignment.worker = worker
    assignment.status = "assigned"
assignment.assigned_date = datetime.datetime.utcnow() 
project.assignments.batch_update(assignments)

An example notebook showing how to assign workers is available here 

Additionally all of the manager objects can be directly accessed from the project object (e.g. project.assignments will return the AssignmentManager).


For your second question regarding "location". This attribute is required/expected based on the Workforce information model. This is why you get the validation message. The Python API allows you to create an assignment object without specifying the location so that you could set the property at a later time before creating the assignment.

Reply
0 Kudos
Highlighted
New Contributor III

Thanks Aaron!  If I get some time I will try this and see how it works. Our workflow for our current project changed up so I had to go a slightly different direction. Since this project is still a work in progress, I may still need to use `batch_update` but I'll have to cross that bridge when I get there.

Reply
0 Kudos