ValidationError: Model requires an object_id

01-10-2020 02:48 PM
Occasional Contributor

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.)



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) ##

workerManager = workforce.managers.WorkerManager(project) ##
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

assignments = []
for feature in features:
            assignment_type="Update Assignment",

4 Replies
Occasional Contributor III

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.  



0 Kudos
Occasional Contributor

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. 

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


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")
    my_edits = my_features[0]
    my_edits.attributes['<field>'] = "an_attribute"
    update_result = assignments_layer.edit_features(updates=[my_edits]) ##
0 Kudos
by Anonymous User
Not applicable


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 ="<query>")
for assignment in assignment:
    assignment.worker = worker
    assignment.status = "assigned"
    assignment.assigned_date = datetime.datetime.utcnow()  

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.

0 Kudos
Occasional Contributor

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.

0 Kudos