Updating task "Assigned to" field to current user when app is launched

Like the title says I’m trying to update the task “Assigned to” field to current user when app is launched. but have only been able to update it to the “shotgun support” user. How do I call the current user id?

In file:
X:\pipeline\app_config\release\extensions\projects\sgtk\toolkit_testing\config\hooks\tk-multi-launchapp\before_app_launch.py

    if self.parent.context.task:
        task_id = self.parent.context.task['id']
        data = {
            'sg_status_list' : 'ip',
            'task_assignees' : [{'type':'HumanUser', 'id' : 24}]
        }
        self.parent.shotgun.update('Task', task_id, data)
2 Likes

Hey Duy!

If you have context (which you’ll have to have in order for your if statement to be true), then you can get the user from the context (as documented here).

So, ostensibly, you could modify the tasks_assignees block from your code above, so that it looks like this:

    if self.parent.context.task:
        task_id = self.parent.context.task['id']
        data = {
            'sg_status_list' : 'ip',
            'task_assignees' : [{'type':'HumanUser', 'id' : self.parent.context.user['id']}]
        }
        self.parent.shotgun.update('Task', task_id, data)

and that should do the trick. Hope that helps!

2 Likes

It works(!), but also writes over existing “assigned to” users. I will send you pastries if you can make it append.

3 Likes

I was going to send you down a path of getting the current value of the field, appending your current user to that list, then updating with the full list.

But, as it turns out, the API has a much smarter solution: you can add the multi_entity_update_modes param to your update() call , specifying whether to set, add, or remove.

So, now your code looks like this:

    if self.parent.context.task:
        task_id = self.parent.context.task['id']
        data = {
            'sg_status_list' : 'ip',
            'task_assignees' : [ {
                'type':'HumanUser', 
                'id' : self.parent.context.user['id'] } ]
        }
        self.parent.shotgun.update(
            'Task', 
            task_id, 
            data, 
            multi_entity_update_modes={'task_assignees' : 'add'} )

(The only addition is the multi_entity_update_modes param; I just changed the line wrapping.)

Please send pastries to @Michael.Kessler; he’s the one that told me about this solution :blush:.

5 Likes

Bravo! Works so good you guys! Hey, send me an addy of your office or do you guys work remote? Tasty treats incoming.

3 Likes

This thread is making me hungry.

1 Like

Hi Duy,

That’s very kind of you, but many of us do work in a distributed fashion. But trust me, the :heart: is felt in this thread!

3 Likes

Thanks I appreciate y’all!

2 Likes

Can he not upload the pastries as a new distributed pastry config?
It should then automatically download to your desk when you refresh Shotgun Desktop right?

5 Likes

Yes, but the pastries end up much smaller once downloaded

miketeavee

3 Likes