0

Update Custom Field item on Card

by
Published Oct 30, 2023

Setting, updating, and removing the value for a Custom Field on a card. For more details on updating custom fields check out the Getting Started With Custom Fields

Script trello Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Trello = {
2
  key: string;
3
  token: string;
4
};
5
/**
6
 * Update Custom Field item on Card
7
 * Setting, updating, and removing the value for a Custom Field on a card. For more details on updating custom fields check out the Getting Started With Custom Fields
8
 */
9
export async function main(
10
  auth: Trello,
11
  idCard: string,
12
  idCustomField: string,
13
  body:
14
    | {
15
        value?: {
16
          text?: string;
17
          checked?: boolean;
18
          date?: string;
19
          number?: number;
20
          [k: string]: unknown;
21
        };
22
        [k: string]: unknown;
23
      }
24
    | { idValue?: string; [k: string]: unknown }
25
) {
26
  const url = new URL(
27
    `https://api.trello.com/1/cards/${idCard}/customField/${idCustomField}/item`
28
  );
29
  for (const [k, v] of [
30
    ["key", auth.key],
31
    ["token", auth.token],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "PUT",
39
    headers: {
40
      "Content-Type": "application/json",
41
      Authorization: undefined,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.text();
50
}
51