Create an enum option

Creates an enum option and adds it to this custom field’s list of enum options. A custom field can have at most 500 enum options (including disabled options). By default new enum options are inserted at the end of a custom field’s list. Locked custom fields can only have enum options added by the user who locked the field. Returns the full record of the newly created enum option.

Script asana Verified

by hugo697 · 10/31/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Create an enum option
6
 * Creates an enum option and adds it to this custom field’s list of enum options. A custom field can have at most 500 enum options (including disabled options). By default new enum options are inserted at the end of a custom field’s list.
7
Locked custom fields can only have enum options added by the user who locked the field.
8
Returns the full record of the newly created enum option.
9
 */
10
export async function main(
11
  auth: Asana,
12
  custom_field_gid: string,
13
  opt_pretty: string | undefined,
14
  opt_fields: string | undefined,
15
  limit: string | undefined,
16
  offset: string | undefined,
17
  body: {
18
    data?: ({ gid?: string; resource_type?: string; [k: string]: unknown } & {
19
      color?: string;
20
      enabled?: boolean;
21
      name?: string;
22
      [k: string]: unknown;
23
    }) & {
24
      insert_after?: string;
25
      insert_before?: string;
26
      [k: string]: unknown;
27
    };
28
    [k: string]: unknown;
29
  }
30
) {
31
  const url = new URL(
32
    `https://app.asana.com/api/1.0/custom_fields/${custom_field_gid}/enum_options`
33
  );
34
  for (const [k, v] of [
35
    ["opt_pretty", opt_pretty],
36
    ["opt_fields", opt_fields],
37
    ["limit", limit],
38
    ["offset", offset],
39
  ]) {
40
    if (v !== undefined && v !== "") {
41
      url.searchParams.append(k, v);
42
    }
43
  }
44
  const response = await fetch(url, {
45
    method: "POST",
46
    headers: {
47
      "Content-Type": "application/json",
48
      Authorization: "Bearer " + auth.token,
49
    },
50
    body: JSON.stringify(body),
51
  });
52
  if (!response.ok) {
53
    const text = await response.text();
54
    throw new Error(`${response.status} ${text}`);
55
  }
56
  return await response.json();
57
}
58