0

Add Task to Section

by
Published Oct 31, 2023

Move a task into a section of one of its projects (or add it to that project). Without insert_before/insert_after it goes to the top of the section.

Script asana Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
/**
4
 * Add Task to Section
5
 * Move a task into a section of one of its projects (or add it to that project). Without insert_before/insert_after it goes to the top of the section.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  section_gid: string,
10
  task: string,
11
  insert_before: string | undefined,
12
  insert_after: string | undefined
13
) {
14
  const response = await fetch(
15
    `https://app.asana.com/api/1.0/sections/${section_gid}/addTask`,
16
    {
17
      method: "POST",
18
      headers: {
19
        Authorization: `Bearer ${auth.token}`,
20
        "Content-Type": "application/json",
21
        Accept: "application/json",
22
      },
23
      body: JSON.stringify({
24
        data: {
25
          task,
26
          insert_before: insert_before || undefined,
27
          insert_after: insert_after || undefined,
28
        },
29
      }),
30
    }
31
  )
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  return await response.json()
38
}
39

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 532 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Add task to section
    6
     * Add a task to a specific, existing section. This will remove the task from other sections of the project.
    7
    
    
    8
    The task will be inserted at the top of a section unless an insert_before or insert_after parameter is declared.
    9
    
    
    10
    This does not work for separators (tasks with the resource_subtype of section).
    11
     */
    12
    export async function main(
    13
      auth: Asana,
    14
      section_gid: string,
    15
      opt_pretty: string | undefined,
    16
      opt_fields: string | undefined,
    17
      body: {
    18
        data?: {
    19
          insert_after?: string;
    20
          insert_before?: string;
    21
          task: string;
    22
          [k: string]: unknown;
    23
        };
    24
        [k: string]: unknown;
    25
      }
    26
    ) {
    27
      const url = new URL(
    28
        `https://app.asana.com/api/1.0/sections/${section_gid}/addTask`
    29
      );
    30
      for (const [k, v] of [
    31
        ["opt_pretty", opt_pretty],
    32
        ["opt_fields", opt_fields],
    33
      ]) {
    34
        if (v !== undefined && v !== "") {
    35
          url.searchParams.append(k, v);
    36
        }
    37
      }
    38
      const response = await fetch(url, {
    39
        method: "POST",
    40
        headers: {
    41
          "Content-Type": "application/json",
    42
          Authorization: "Bearer " + auth.token,
    43
        },
    44
        body: JSON.stringify(body),
    45
      });
    46
      if (!response.ok) {
    47
        const text = await response.text();
    48
        throw new Error(`${response.status} ${text}`);
    49
      }
    50
      return await response.json();
    51
    }
    52