0

Reorder a custom field's enum

by
Published Oct 31, 2023

Moves a particular enum option to be either before or after another specified enum option in the custom field. Locked custom fields can only be reordered by the user who locked the field.

Script asana Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Reorder a custom field's enum
6
 * Moves a particular enum option to be either before or after another specified enum option in the custom field.
7
Locked custom fields can only be reordered by the user who locked the field.
8
 */
9
export async function main(
10
  auth: Asana,
11
  custom_field_gid: string,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  body: {
15
    data?: {
16
      after_enum_option?: string;
17
      before_enum_option?: string;
18
      enum_option: string;
19
      [k: string]: unknown;
20
    };
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(
25
    `https://app.asana.com/api/1.0/custom_fields/${custom_field_gid}/enum_options/insert`
26
  );
27
  for (const [k, v] of [
28
    ["opt_pretty", opt_pretty],
29
    ["opt_fields", opt_fields],
30
  ]) {
31
    if (v !== undefined && v !== "") {
32
      url.searchParams.append(k, v);
33
    }
34
  }
35
  const response = await fetch(url, {
36
    method: "POST",
37
    headers: {
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49