Set Custom Field Value
One script reply has been approved by the moderators Verified

Add data to a Custom field on a task.

You'll need to know the task_id of the task you want to update, and the universal unique identifier (UUID) field_id of the Custom Field you want to set.

You can use Get Accessible Custom Fields or the Get Task endpoint to find the field_id.

Created by hugo697 168 days ago
Submitted by hugo697 Bun
Verified 168 days ago
1
//native
2
type Clickup = {
3
  token: string;
4
};
5
/**
6
 * Set Custom Field Value
7
 * Add data to a Custom field on a task. \
8
 \
9
You'll need to know the `task_id` of the task you want to update, and the universal unique identifier (UUID) `field_id` of the Custom Field you want to set. \
10
 \
11
You can use [Get Accessible Custom Fields](ref:getaccessiblecustomfields) or the [Get Task](ref:gettask) endpoint to find the `field_id`.
12
 */
13
export async function main(
14
  auth: Clickup,
15
  task_id: string,
16
  field_id: string,
17
  custom_task_ids: string | undefined,
18
  team_id: string | undefined,
19
  body:
20
    | { value: string }
21
    | { value: string }
22
    | { value: string }
23
    | { value: string }
24
    | { value?: number; value_options?: { time: false | true } }
25
    | { value: string }
26
    | { value: number }
27
    | { value: number }
28
    | { value: { add?: string[]; rem?: string[] } }
29
    | { value: { add?: number[]; rem?: number[] } }
30
    | { value: number }
31
    | { value: { current: number } }
32
    | { value: string[] }
33
    | {
34
        value: {
35
          location?: { lat?: number; lng?: number };
36
          formatted_address?: string;
37
        };
38
      },
39
) {
40
  const url = new URL(
41
    `https://api.clickup.com/api/v2/task/${task_id}/field/${field_id}`,
42
  );
43
  for (const [k, v] of [
44
    ["custom_task_ids", custom_task_ids],
45
    ["team_id", team_id],
46
  ]) {
47
    if (v !== undefined && v !== "" && k !== undefined) {
48
      url.searchParams.append(k, v);
49
    }
50
  }
51
  const response = await fetch(url, {
52
    method: "POST",
53
    headers: {
54
      "Content-Type": "application/json",
55
      Authorization: auth.token,
56
    },
57
    body: JSON.stringify(body),
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65