d3wannabe
d3wannabe

Reputation: 1317

Update custom_fields with Python in asana API

I've been trying update custom_fields per the latest version of Asana's API, very similarly to this post but with a later version of the API (e.g. I need to use update_task method). I can update fields at the top level of a task, but the custom_fields object is proving much more challenging to update. For example, I have many custom fields, and am trying to update a test field called "Update" and just set the text_value to "Hello"...

import asana
asanaPAT = 'myToken'
client = asana.Client.access_token(asanaPAT)
result = client.tasks.get_tasks({'project': 'myProjectID'}, opt_pretty=True)#, iterator_type=None)    
for index, result in enumerate(result):

    complete_task = client.tasks.find_by_id(result["gid"])
   
    task_name = complete_task['name'] 
    task_id = complete_task['gid']
    custom_fields = complete_task['custom_fields']
   
    #I can easily update top-level fields like 'name' and 'completed'...
    #result = client.tasks.update_task(task_id, {'name': task_name + '(new)'}, opt_pretty=True)
    #result = client.tasks.update_task(task_id, {'completed': False}, opt_pretty=True)
    for custom_fieldsRow in custom_fields:
        if custom_fieldsRow['name'] == "Updated":
            #custom_fieldsRow['text_value'] = 'Hello'
    #finished loop through individual custom fields, so update on the level of the task...
    #client.tasks.update_task(task_id, {custom_fields}, opt_pretty=True)
    
    manualCustomField = {'data': { 'custom_fields': {'gid': 'theGIDOfCustomField', 'text_value': 'Hello'} }} 
    resultFromUpdate = client.tasks.update_task(task_id, manualCustomField, opt_pretty=True)

As you can see above, I started off trying to loop through the custom_fields and make changes to the specific field before updating later. But now I'm even trying to manually set the custom_field data (last line of my code), but it does nothing (no error, but doesn't change my task). I'm completely out of ideas to troubleshoot this so appreciate any feedback on where I'm going wrong.

Upvotes: 0

Views: 584

Answers (1)

d3wannabe
d3wannabe

Reputation: 1317

Apologies, I figured out my mistake, I just needed my penultimate line to read...

manualCustomField = { 'custom_fields': {'theGIDOfCustomField':'Hello'} } 

Kinda a strange way to do that in the API (not specifically stating which field you'll update or which id you're using) if you ask me, but now it finally works.

Upvotes: 2

Related Questions