0

Update Task

by
Published Oct 31, 2023

Update a task. Only the fields you set are changed. `completed` defaults to `keep`; use `clear_assignee` or `clear_dates` to unassign the task or remove its start and due dates. `custom_fields` maps custom field gids to values (enum fields take the enum option gid, null clears a field).

Script asana Verified

The script

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

3
/**
4
 * Update Task
5
 * Update a task. Only the fields you set are changed. `completed` defaults to `keep`; use `clear_assignee` or `clear_dates` to unassign the task or remove its start and due dates. `custom_fields` maps custom field gids to values (enum fields take the enum option gid, null clears a field).
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  task_gid: string,
10
  name: string | undefined,
11
  notes: string | undefined,
12
  html_notes: string | undefined,
13
  assignee: string | undefined,
14
  due_on: string | undefined,
15
  due_at: string | undefined,
16
  start_on: string | undefined,
17
  completed: "keep" | "complete" | "incomplete" = "keep",
18
  clear_assignee: boolean | undefined,
19
  clear_dates: boolean | undefined,
20
  custom_fields: { [key: string]: any } | undefined
21
) {
22
  const data: { [key: string]: any } = {
23
    name: name || undefined,
24
    notes: notes || undefined,
25
    html_notes: html_notes || undefined,
26
    assignee: assignee || undefined,
27
    due_on: due_on || undefined,
28
    due_at: due_at || undefined,
29
    start_on: start_on || undefined,
30
    custom_fields:
31
      custom_fields && Object.keys(custom_fields).length
32
        ? custom_fields
33
        : undefined,
34
  }
35
  if (completed === "complete") data.completed = true
36
  if (completed === "incomplete") data.completed = false
37
  if (clear_assignee) data.assignee = null
38
  if (clear_dates) {
39
    data.due_on = null
40
    data.due_at = null
41
    data.start_on = null
42
  }
43

44
  const response = await fetch(
45
    `https://app.asana.com/api/1.0/tasks/${task_gid}`,
46
    {
47
      method: "PUT",
48
      headers: {
49
        Authorization: `Bearer ${auth.token}`,
50
        "Content-Type": "application/json",
51
        Accept: "application/json",
52
      },
53
      body: JSON.stringify({ data }),
54
    }
55
  )
56

57
  if (!response.ok) {
58
    throw new Error(`${response.status} ${await response.text()}`)
59
  }
60

61
  return await response.json()
62
}
63

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Update a task
    6
     * A specific, existing task can be updated by making a PUT request on the
    7
    URL for that task. Only the fields provided in the `data` block will be
    8
    updated; any unspecified fields will remain unchanged.
    9
    
    
    10
    When using this method, it is best to specify only those fields you wish
    11
    to change, or else you may overwrite changes made by another user since
    12
    you last retrieved the task.
    13
    
    
    14
    Returns the complete updated task record.
    15
     */
    16
    export async function main(
    17
      auth: Asana,
    18
      task_gid: string,
    19
      opt_pretty: string | undefined,
    20
      opt_fields: string | undefined,
    21
      body: {
    22
        data?: (({ gid?: string; resource_type?: string; [k: string]: unknown } & {
    23
          name?: string;
    24
          resource_subtype?: "default_task" | "milestone" | "section" | "approval";
    25
          [k: string]: unknown;
    26
        }) & {
    27
          actual_time_minutes?: number;
    28
          approval_status?:
    29
            | "pending"
    30
            | "approved"
    31
            | "rejected"
    32
            | "changes_requested";
    33
          assignee_status?: "today" | "upcoming" | "later" | "new" | "inbox";
    34
          completed?: boolean;
    35
          completed_at?: string;
    36
          completed_by?: {
    37
            gid?: string;
    38
            resource_type?: string;
    39
            [k: string]: unknown;
    40
          } & { name?: string; [k: string]: unknown };
    41
          created_at?: string;
    42
          dependencies?: {
    43
            gid?: string;
    44
            resource_type?: string;
    45
            [k: string]: unknown;
    46
          }[];
    47
          dependents?: {
    48
            gid?: string;
    49
            resource_type?: string;
    50
            [k: string]: unknown;
    51
          }[];
    52
          due_at?: string;
    53
          due_on?: string;
    54
          external?: { data?: string; gid?: string; [k: string]: unknown };
    55
          hearted?: boolean;
    56
          hearts?: {
    57
            gid?: string;
    58
            user?: {
    59
              gid?: string;
    60
              resource_type?: string;
    61
              [k: string]: unknown;
    62
            } & { name?: string; [k: string]: unknown };
    63
            [k: string]: unknown;
    64
          }[];
    65
          html_notes?: string;
    66
          is_rendered_as_separator?: boolean;
    67
          liked?: boolean;
    68
          likes?: {
    69
            gid?: string;
    70
            user?: {
    71
              gid?: string;
    72
              resource_type?: string;
    73
              [k: string]: unknown;
    74
            } & { name?: string; [k: string]: unknown };
    75
            [k: string]: unknown;
    76
          }[];
    77
          memberships?: {
    78
            project?: {
    79
              gid?: string;
    80
              resource_type?: string;
    81
              [k: string]: unknown;
    82
            } & { name?: string; [k: string]: unknown };
    83
            section?: {
    84
              gid?: string;
    85
              resource_type?: string;
    86
              [k: string]: unknown;
    87
            } & { name?: string; [k: string]: unknown };
    88
            [k: string]: unknown;
    89
          }[];
    90
          modified_at?: string;
    91
          name?: string;
    92
          notes?: string;
    93
          num_hearts?: number;
    94
          num_likes?: number;
    95
          num_subtasks?: number;
    96
          start_at?: string;
    97
          start_on?: string;
    98
          [k: string]: unknown;
    99
        }) & {
    100
          assignee?: string;
    101
          assignee_section?: string;
    102
          custom_fields?: { [k: string]: string };
    103
          followers?: string[];
    104
          parent?: string;
    105
          projects?: string[];
    106
          tags?: string[];
    107
          workspace?: string;
    108
          [k: string]: unknown;
    109
        };
    110
        [k: string]: unknown;
    111
      }
    112
    ) {
    113
      const url = new URL(`https://app.asana.com/api/1.0/tasks/${task_gid}`);
    114
      for (const [k, v] of [
    115
        ["opt_pretty", opt_pretty],
    116
        ["opt_fields", opt_fields],
    117
      ]) {
    118
        if (v !== undefined && v !== "") {
    119
          url.searchParams.append(k, v);
    120
        }
    121
      }
    122
      const response = await fetch(url, {
    123
        method: "PUT",
    124
        headers: {
    125
          "Content-Type": "application/json",
    126
          Authorization: "Bearer " + auth.token,
    127
        },
    128
        body: JSON.stringify(body),
    129
      });
    130
      if (!response.ok) {
    131
        const text = await response.text();
    132
        throw new Error(`${response.status} ${text}`);
    133
      }
    134
      return await response.json();
    135
    }
    136