0

Create Task

by
Published Oct 31, 2023

Create a task in a workspace, optionally in projects or as a subtask of `parent`. Dates: `due_on`/`start_on` as YYYY-MM-DD, or `due_at` as an ISO 8601 date-time (set one of due_on/due_at). `custom_fields` maps custom field gids to values, e.g. {"1234": "High"}; enum fields take the enum option gid.

Script asana Verified

The script

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

3
export type DynSelect_workspace = string
4

5
// Resolver: the workspaces and organizations the token can access.
6
export async function workspace(auth: RT.Asana) {
7
  const response = await fetch(
8
    "https://app.asana.com/api/1.0/workspaces?limit=100",
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { data } = await response.json()
20
  return data.map((w: { gid: string; name: string }) => ({
21
    value: w.gid,
22
    label: w.name,
23
  }))
24
}
25

26
/**
27
 * Create Task
28
 * Create a task in a workspace, optionally in projects or as a subtask of `parent`. Dates: `due_on`/`start_on` as YYYY-MM-DD, or `due_at` as an ISO 8601 date-time (set one of due_on/due_at). `custom_fields` maps custom field gids to values, e.g. {"1234": "High"}; enum fields take the enum option gid.
29
 */
30
export async function main(
31
  auth: RT.Asana,
32
  workspace: DynSelect_workspace,
33
  name: string,
34
  notes: string | undefined,
35
  html_notes: string | undefined,
36
  assignee: string | undefined,
37
  projects: string[] | undefined,
38
  parent: string | undefined,
39
  due_on: string | undefined,
40
  due_at: string | undefined,
41
  start_on: string | undefined,
42
  tags: string[] | undefined,
43
  followers: string[] | undefined,
44
  assignee_section: string | undefined,
45
  completed: boolean | undefined,
46
  custom_fields: { [key: string]: any } | undefined
47
) {
48
  const data = {
49
    workspace,
50
    name,
51
    notes: notes || undefined,
52
    html_notes: html_notes || undefined,
53
    assignee: assignee || undefined,
54
    projects: projects?.length ? projects : undefined,
55
    parent: parent || undefined,
56
    due_on: due_on || undefined,
57
    due_at: due_at || undefined,
58
    start_on: start_on || undefined,
59
    tags: tags?.length ? tags : undefined,
60
    followers: followers?.length ? followers : undefined,
61
    assignee_section: assignee_section || undefined,
62
    completed: completed || undefined,
63
    custom_fields:
64
      custom_fields && Object.keys(custom_fields).length
65
        ? custom_fields
66
        : undefined,
67
  }
68

69
  const response = await fetch("https://app.asana.com/api/1.0/tasks", {
70
    method: "POST",
71
    headers: {
72
      Authorization: `Bearer ${auth.token}`,
73
      "Content-Type": "application/json",
74
      Accept: "application/json",
75
    },
76
    body: JSON.stringify({ data }),
77
  })
78

79
  if (!response.ok) {
80
    throw new Error(`${response.status} ${await response.text()}`)
81
  }
82

83
  return await response.json()
84
}
85

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