Changing task section

Hello, I am trying to change a certain tasks section. I am using the following body while trying to do so.

"memberships": [{"project": 1131681448247237, "section": 1131889518013274}]

I have triple checked the project ID and the section ID as well but I constantly get the following error.

{"errors":[{"message":"No matching route for request","help":"For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"}]}

Can anyone help me with this, cant figure out what I’m doing wrong?

Hi @Dylan4 and welcome to the forum!

That error indicates not that there is an issue with the IDs you’re passing but that there is a problem with the endpoint or the syntax of the request itself. Your best best for assistance therefore is to post the actual code you’re using to make the request.

@Phil_Seeman I am using the Requests library for python.

 headers = {'Authorization':'Bearer 0/XXXXXXXXXXXXXXXX}
 r2 = requests.post('https://app.asana.com/api/1.0/tasks/'+ str(item), headers=headers, data = {"memberships": [{"project": 1131681448247237, "section": 1131889518013274}]})

str(item) is the tasks ID, I have also tried to hardcode a specific ID into the request endpoint and the result is the same. I used this topic to help me.

The doc says you should use the /addProject endpoint, did you try?

3 Likes

Yeah, that was the issue. Must have missed that part of the documentation. Also had to change to data in the request to;

data = {"project": 1131681448247237, "section": 1131681448247243}

Instead of;

data = {"memberships": [{"project": 1131681448247237, "section": 1131889518013274}]}
2 Likes

@Phil_Seeman + @Bastien_Siebman - not to re-open an old thread, but wanted to have a bit of a conversation about this…

With “sections” now being so critical within Asana, I’m a bit shocked that there’s no way to update a section via the “update task” API endpoint. A section now is essentially like a custom field of sorts.

Is there any way to update a task’s section via the API along with other components (like task status/custom fields/assignee) without using an entirely different API request?

For example:

(simply adding the project/section) within the update task data?

Hi @abass!

Unfortunately no. You can include a memberships property when initially creating as task and that defines the project(s) and section(s) to put that task into, But that’s only on task creation. For updating an existing task, you have to use a separate call/endpoint to move it to a different section (you can use either the POST /tasks/{task_gid}/addProject or POST /sections/{section_gid}/addTask endpoint).

3 Likes

Really appreciate the quick response and information, super helpful!

Gosh, I do hope they make updating it more core to the task update component with time, though I also understand it’s a bit tough as a task may contain multiple projects linked to it. It’s a tough one.

The membership bit is helpful to know re: task creation though!

Thanks Phil!

3 Likes

At the very least, the API documentation should be updated to indicate this. There’s no indication of how you get and set sections in the places you’d expect to learn how. I’m using Ruby and there’s no code shown, you should indicate when things are not yet built or what’s going on with it in docs…

Sorry to reopen this thread. Just want to share my workaround to move a task to a section through the API:

  1. Create a custom enum field( for example: status)
  2. Create a rule: when the status changed to a specific value then it will move the task to the desired section
  3. Update the custom enum field through the update task API (Custom fields)

It’s a hacky solution but it works in my simple case. :smile:

Hi @Gregorius_Kusumo,

Can I ask why you’re taking that approach instead of the simpler solution - just one API call, no rules or custom fields needed - that’s described above here: Changing task section - #7 by Phil_Seeman

Also looking for this feature to be built-in.

If anyone is interested, this can be achieved using the python API, code snippet below for implementation on a specific project:

# Initialise
api_client = authenticate_asana_client()
tasks_api_instance = asana.TasksApi(api_client)
sections_api = asana.SectionsApi(api_client)

section_id_dict = {'Section name': 'SECTION_ID'} # Lookup IDs for section of interest

project_tasks = get_all_asana_project_tasks()

# Loop though tasks in project
for task in project_tasks:
    # Get task ID
    task_gid = task["gid"]

    # Get all details of task
    task_details = tasks_api_instance.get_task(task_gid, {})

    # Extract task name
    full_task_name = task_details["name"]

    # Extract date last modified
    last_modified = task_details['modified_at']
    last_modified = datetime.datetime.fromisoformat(last_modified.replace("Z", "+00:00"))

    # Calculate date 3 months ago
    utc = pytz.UTC  # Get UTC timezone
    three_months_ago = utc.localize(datetime.datetime.utcnow()) - datetime.timedelta(days=3*30)

    # Select only tasks not modified in the past 3 months
    if last_modified < three_months_ago:

        # Loop through projects it is a member of
        for project in task_details['memberships']:

            # Proceed only for the 'Data Requests' project
            if project['project']['name'] == 'Data Requests': # Select Data Requests
                section_name = project['section']['name'] 
                section_ID = project['section']['gid']

                # If section is request, move to icebox
                if section_name == 'Requests':
                    new_section_name = 'Icebox (Old requests)'
                    new_section_id = section_id_dict[new_section_name]
                    move_task_to_section(sections_api, task_details, 
                                                                 new_section_id,new_section_name)
    

def get_all_asana_project_tasks(
    access_token="TOKEN",
    project_id="PID",
):
    """
    Given asana API access token and a project ID, return all tasks as a list of dicts.
    """

    # Configure and authorise asana api
    api_client = authenticate_asana_client(access_token)
    tasks_api_instance = asana.TasksApi(api_client)

    # Get all tasks from project
    # Note project ID can be found in its URL
    task_response = tasks_api_instance.get_tasks_for_project(project_id, {})

    # Convert response into a list
    all_tasks = []
    for t in task_response:
        all_tasks.append(t)

    return all_tasks

def move_task_to_section(sections_api, task_details, new_section_id,new_section_name):
    """Move a task to a new section."""
    opts = {
            'body': {
                'data': {
                    'task': task_details.get('gid')
                }
            }
        }
    sections_api.add_task_for_section(new_section_id, opts)
    print(f"Moved task: {task_details['name']} to {new_section_name}")