How to copy "Completed" and "Canceled" Assignments into an "archive" feature layer in AGOL?

1376
4
Jump to solution
10-15-2018 07:38 PM
by Anonymous User
Not applicable

Hopefully this is the right place to post, although this is a code-based question. Any help is welcomed!

I'm automating our workforce app so that at the end of each day, the "completed" assignments and the "canceled" assignments are copied into a feature layer "archive_assignments" and then deleted from the workforce project.

The main reason we want to do this is because all of the "completed" and "canceled" assignments stay on the Collector map unless you manually delete them. We will eventually have hundreds of assignments created and can't keep the map cluttered, but we also don't want to lose that information. 

The archive/delete script provided with workforce is a bit advanced for me, and since I know Python quite well (at least for Pro, not AGOL)  I figured I'd try to write a simple script myself that would do the same thing. 

I'm finding it really difficult to do a very simple task. Is there an easier way to do this? Attached is the code I tried so far in jupyter notebook (edited a bit for confidentiality) 

I know ".copy_features" isn't a valid attribute for the feature layer, so I'm looking for a replacement/ correct code

Thanks in advance! 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi

Thanks for the tip on converting to features - I knew I was missing something!

I did need to add another bit of code to actually add the completed features to the archive layer. I had to change from Feature Layer Collection to Feature Layer to be able to use .edit_features() command. I'll attach the code that finally worked for me. Thanks again!

View solution in original post

0 Kudos
4 Replies
by Anonymous User
Not applicable

Hi Karen,

I think  you are most of the way there. Although I haven't tested it, I believe you want to use:

# query assignments
completed_assignments = project.assignments.search(where='status=3')

# convert to features
completed_features = [f.feature for f in completed_assignments]

# add features to layer
completed_archive.edit_features(adds=completed_features)

# delete assignments
project.assignments.batch_delete(completed_assignments)
‍‍‍‍

Searching for assignments returns a list of Assignment objects. These need to be converted to Feature objects and then added to the feature layer.

See Assignment Manger and Feature Layer

There are also some Jupyter notebooks here that simpler (but doesn't include archiving).

Hope that helps

0 Kudos
by Anonymous User
Not applicable

Hi

Thanks for the tip on converting to features - I knew I was missing something!

I did need to add another bit of code to actually add the completed features to the archive layer. I had to change from Feature Layer Collection to Feature Layer to be able to use .edit_features() command. I'll attach the code that finally worked for me. Thanks again!

0 Kudos
Kman
by
New Contributor

Hello all, 

This looks like a great solution that could aid others in managing Workforce assignments.  Unfortunately the final version (which includes the Feature Layer Collection to Feature Layer to be able to use .edit_features() command)  of the code was not posted. 

Anyone have a similar sample they would be willing to share?  

0 Kudos
Kman
by
New Contributor

Quick update and big props to AaronPulver for pointing out my error.  Here is version of the code which will allow you to move completed assignments to a separate feature layer (archived) .

# connect to org/portal
gis = GIS(orgURL, username, password)

project = workforce.Project(gis.content.search("type:'Workforce Project' 'name of WF Project'")[0])

#Archive Hosted Feature layer item ID (Note-The index "0" means the first layer in your feature service, so if you service has multiple layers, you may need to change that.)
completed_archive = gis.content.get("d25ed4b313bd499994b4727a6584a22213").layers[0]

#Query for Completed Assignments
completed_assignments = project.assignments.search(where= ' status=3 ' )

#Convert to Features
completed_features = [f.feature for f in completed_assignments]

# add features to layer
completed_archive.edit_features(adds=completed_features)

# delete assignments
project.assignments.batch_delete(completed_assignments)

0 Kudos