0
Add a custom field to a project
One script reply has been approved by the moderators Verified

Custom fields are associated with projects by way of custom field settings. This method creates a setting for the project.

Created by hugo697 610 days ago Viewed 22467 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 610 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Add a custom field to a project
6
 * Custom fields are associated with projects by way of custom field settings.  This method creates a setting for the project.
7
 */
8
export async function main(
9
  auth: Asana,
10
  project_gid: string,
11
  opt_pretty: string | undefined,
12
  body: {
13
    data?: {
14
      custom_field: string;
15
      insert_after?: string;
16
      insert_before?: string;
17
      is_important?: boolean;
18
      [k: string]: unknown;
19
    };
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://app.asana.com/api/1.0/projects/${project_gid}/addCustomFieldSetting`
25
  );
26
  for (const [k, v] of [["opt_pretty", opt_pretty]]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Content-Type": "application/json",
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: JSON.stringify(body),
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45